Introduced Scollection

This commit is contained in:
7u83 2018-12-08 12:17:53 +01:00
parent 6d24eb67b9
commit 8dbe6cfe6d
10 changed files with 116 additions and 40 deletions

View File

@ -1,4 +1,4 @@
#Sat, 08 Dec 2018 11:08:46 +0100
#Sat, 08 Dec 2018 12:17:24 +0100
annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.processors.list=

View File

@ -0,0 +1,55 @@
/*
* Copyright (c) 2018, 7u83 <7u83@mail.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package opensesim.util;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class Scollection<KEY, OBJ> {
HashSet<OBJ> byObj = new HashSet<>();
HashMap<KEY, OBJ> byKey = new HashMap<>();
public void add(KEY key, OBJ obj) {
byObj.add(obj);
byKey.put(key, obj);
}
public Collection<OBJ> getCollection(){
return Collections.unmodifiableCollection(byObj);
}
public OBJ get(KEY key){
return byKey.get(key);
}
}

View File

@ -73,7 +73,7 @@ public abstract class AbstractAsset implements GetJson {
return decimals;
}
public void setDescription(String description) {
protected void setDescription(String description) {
this.description = description;
}
@ -105,7 +105,7 @@ public abstract class AbstractAsset implements GetJson {
public static final String JSON_DECIMALS = "decimals";
public static final int DECIMALS_DEFAULT = 2;
public static void rename(GodWorld world, AbstractAsset a, String symbol) throws Exception {
/* public static void rename(GodWorld world, AbstractAsset a, String symbol) throws Exception {
if (world.assetsBySymbol.get(symbol) != null) {
throw new java.lang.Exception("Can't rename asset symbol. Symbol '" + symbol + "' is already in use.");
}
@ -115,7 +115,7 @@ public abstract class AbstractAsset implements GetJson {
world.assetsBySymbol.put(a.getSymbol(), a);
}
*/
/* public static AbstractAsset create(World world, Class<AbstractAsset> cls, String symbol) throws Exception {
AbstractAsset a = cls.newInstance();

View File

@ -45,7 +45,7 @@ public class Account {
Exchange exchange;
public Map<Asset,Double> getAssets() {
return Collections.unmodifiableMap(assets);
return Collections.unmodifiableMap(assets);
}
public Trader getOwner() {
@ -60,8 +60,6 @@ public class Account {
assets = new ConcurrentHashMap<>();
}
Account(long masterKey){
}
}

View File

@ -59,7 +59,7 @@ public class AssetPair {
this.currency = currency;
}
public AssetPair(GodWorld world, String asset, String currency){
public AssetPair(World world, String asset, String currency){
this.asset = world.getAssetBySymbol(asset);
this.currency = world.getAssetBySymbol(asset);
}
@ -85,6 +85,7 @@ public class AssetPair {
@Override
public boolean equals(Object o) {
AssetPair ap = (AssetPair)o;
if (ap.asset==asset && ap.currency==currency)
return true;

View File

@ -35,6 +35,7 @@ import java.util.HashSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import opensesim.sesim.interfaces.GetJson;
import opensesim.util.Scollection;
import opensesim.util.SeSimException;
import opensesim.util.idgenerator.IDGenerator;
import org.json.JSONArray;
@ -57,15 +58,22 @@ public class GodWorld implements GetJson, World {
}
HashSet<AbstractAsset> assetsById = new HashSet<>();
/* HashSet<AbstractAsset> assetsById = new HashSet<>();
HashMap<String, AbstractAsset> assetsBySymbol = new HashMap<>();
*/
Scollection <String,AbstractAsset> assets = new Scollection<>();
IDGenerator assetIdGenerator = new IDGenerator();
IDGenerator orderIdGenerator = new IDGenerator();
HashSet<AssetPair> assetPairs = new HashSet<>();
ArrayList<Exchange> exchanges = new ArrayList<>();
//ArrayList<Exchange> exchanges = new ArrayList<>();
Scollection <String, Exchange> exchanges = new Scollection<>();
/**
* Create a World object.
@ -100,8 +108,8 @@ public class GodWorld implements GetJson, World {
if (a == null) {
continue;
}
assetsById.add(a);
assetsBySymbol.put(a.getSymbol(), a);
assets.add(a.getSymbol(), a);
}
}
@ -124,10 +132,10 @@ public class GodWorld implements GetJson, World {
putJson(cfg);
}
public boolean checkMasterKey(long masterkey) {
/* public boolean checkMasterKey(long masterkey) {
return masterkey == this.masterkey;
}
*/
public AbstractAsset createAsset(JSONObject cfg) throws SeSimException {
AbstractAsset a;
String class_name;
@ -148,21 +156,20 @@ public class GodWorld implements GetJson, World {
return null;
}
if (this.assetsBySymbol.get(a.getSymbol()) != null) {
if (this.assets.get(a.getSymbol()) != null) {
throw new SeSimException("Already defined");
}
this.assetsById.add(a);
this.assetsBySymbol.put(a.getSymbol(), a);
assets.add(a.getSymbol(), a);
return a;
}
public Collection<AbstractAsset> getAssetCollection() {
return Collections.unmodifiableCollection(assetsById);
return assets.getCollection(); //Collections.unmodifiableCollection(assetsById);
}
public AbstractAsset getAssetBySymbol(String symbol) {
return this.assetsBySymbol.get(symbol);
return this.assets.get(symbol);
}
public Collection<AssetPair> getAssetPairsCollection() {
@ -170,7 +177,7 @@ public class GodWorld implements GetJson, World {
}
public Collection<Exchange> getExchangeCollection() {
return Collections.unmodifiableCollection(exchanges);
return exchanges.getCollection();
}
public void add(AssetPair pair) {
@ -269,4 +276,9 @@ public class GodWorld implements GetJson, World {
}
}
public World getWorld(){
return new RealWorld(this);
}
}

View File

@ -25,26 +25,27 @@
*/
package opensesim.world;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import opensesim.sesim.interfaces.GetJson;
import opensesim.util.idgenerator.IDGenerator;
import opensesim.util.SeSimException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class RealWorld implements World{
private GodWorld godworld;
RealWorld(GodWorld godworld){
this.godworld=godworld;
}
@Override
public Collection<AbstractAsset> getAssetCollection() {
return godworld.getAssetCollection();
}
@Override
public AbstractAsset getAssetBySymbol(String symbol) {
return godworld.getAssetBySymbol(symbol);
}
}

View File

@ -25,10 +25,13 @@
*/
package opensesim.world;
import java.util.Collection;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public interface World {
public Collection<AbstractAsset> getAssetCollection();
public AbstractAsset getAssetBySymbol(String symbol);
}

View File

@ -66,7 +66,7 @@ public class ExchangeTest {
public void testCreateAccount() {
System.out.println("createAccount");
WorldAdm worldadm = new opensesim.world.WorldAdm(new JSONObject(
GodWorld godworld = new opensesim.world.GodWorld(new JSONObject(
"{ assets: [ "
+ "{"
+ "symbol: EUR,"
@ -85,8 +85,14 @@ public class ExchangeTest {
+ "}"
));
World world = godworld.getWorld();
AssetPair ap = new AssetPair(world,"EUR","AAPL");
Exchange instance = new Exchange(null, (JSONObject) null);
Account expResult = null;
Account result = instance.createAccount();
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.

View File

@ -51,11 +51,11 @@ public class OrderTest {
public static void tearDownClass() {
}
RealWorld world;
GodWorld world;
@Before
public void setUp() {
world = new RealWorld(new JSONObject("{}"));
world = new GodWorld(new JSONObject("{}"));
}
@After