diff --git a/nbproject/project.properties b/nbproject/project.properties index 86abed6..1a73840 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -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= diff --git a/src/opensesim/util/Scollection.java b/src/opensesim/util/Scollection.java new file mode 100644 index 0000000..8033dc7 --- /dev/null +++ b/src/opensesim/util/Scollection.java @@ -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 { + + HashSet byObj = new HashSet<>(); + HashMap byKey = new HashMap<>(); + + public void add(KEY key, OBJ obj) { + byObj.add(obj); + byKey.put(key, obj); + } + + public Collection getCollection(){ + return Collections.unmodifiableCollection(byObj); + } + + public OBJ get(KEY key){ + return byKey.get(key); + } +} diff --git a/src/opensesim/world/AbstractAsset.java b/src/opensesim/world/AbstractAsset.java index e14d1e2..aeffa82 100644 --- a/src/opensesim/world/AbstractAsset.java +++ b/src/opensesim/world/AbstractAsset.java @@ -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 cls, String symbol) throws Exception { AbstractAsset a = cls.newInstance(); diff --git a/src/opensesim/world/Account.java b/src/opensesim/world/Account.java index 0fcbd55..cf62edd 100644 --- a/src/opensesim/world/Account.java +++ b/src/opensesim/world/Account.java @@ -45,7 +45,7 @@ public class Account { Exchange exchange; public Map 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){ - - } + } diff --git a/src/opensesim/world/AssetPair.java b/src/opensesim/world/AssetPair.java index 5c67233..20fd3cb 100644 --- a/src/opensesim/world/AssetPair.java +++ b/src/opensesim/world/AssetPair.java @@ -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; diff --git a/src/opensesim/world/GodWorld.java b/src/opensesim/world/GodWorld.java index 4429789..fdfba89 100644 --- a/src/opensesim/world/GodWorld.java +++ b/src/opensesim/world/GodWorld.java @@ -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 assetsById = new HashSet<>(); + /* HashSet assetsById = new HashSet<>(); HashMap assetsBySymbol = new HashMap<>(); - +*/ + + Scollection assets = new Scollection<>(); + IDGenerator assetIdGenerator = new IDGenerator(); IDGenerator orderIdGenerator = new IDGenerator(); HashSet assetPairs = new HashSet<>(); - ArrayList exchanges = new ArrayList<>(); + //ArrayList exchanges = new ArrayList<>(); + + + Scollection 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 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 getAssetPairsCollection() { @@ -170,7 +177,7 @@ public class GodWorld implements GetJson, World { } public Collection 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); + } + } diff --git a/src/opensesim/world/RealWorld.java b/src/opensesim/world/RealWorld.java index dcfbfd2..6806e45 100644 --- a/src/opensesim/world/RealWorld.java +++ b/src/opensesim/world/RealWorld.java @@ -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 getAssetCollection() { + return godworld.getAssetCollection(); + } + + @Override + public AbstractAsset getAssetBySymbol(String symbol) { + return godworld.getAssetBySymbol(symbol); + } } diff --git a/src/opensesim/world/World.java b/src/opensesim/world/World.java index 7a13ffe..b5c3390 100644 --- a/src/opensesim/world/World.java +++ b/src/opensesim/world/World.java @@ -25,10 +25,13 @@ */ package opensesim.world; +import java.util.Collection; + /** * * @author 7u83 <7u83@mail.ru> */ public interface World { - + public Collection getAssetCollection(); + public AbstractAsset getAssetBySymbol(String symbol); } diff --git a/test/opensesim/world/ExchangeTest.java b/test/opensesim/world/ExchangeTest.java index a618871..500afa8 100644 --- a/test/opensesim/world/ExchangeTest.java +++ b/test/opensesim/world/ExchangeTest.java @@ -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. diff --git a/test/opensesim/world/OrderTest.java b/test/opensesim/world/OrderTest.java index 68cdae0..b8217a7 100644 --- a/test/opensesim/world/OrderTest.java +++ b/test/opensesim/world/OrderTest.java @@ -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