From 5d7ffaca6a7fb45b6b4e7a52012690f5f4e10571 Mon Sep 17 00:00:00 2001 From: iMoHax Date: Thu, 19 Nov 2015 15:36:58 +0300 Subject: [PATCH] refactoring clone/copy methods --- .../java/ru/trader/model/MarketModel.java | 8 +++- .../java/ru/trader/model/MissionModel.java | 24 ++++++++-- .../java/ru/trader/model/RouteEntryModel.java | 13 ++++++ .../main/java/ru/trader/analysis/Route.java | 29 ++++++++++++ .../java/ru/trader/analysis/RouteEntry.java | 23 ++++++++++ core/src/main/java/ru/trader/core/Order.java | 14 +++++- .../src/main/java/ru/trader/core/Profile.java | 45 ++++++++++--------- core/src/main/java/ru/trader/core/Ship.java | 17 ++++--- 8 files changed, 137 insertions(+), 36 deletions(-) diff --git a/client/src/main/java/ru/trader/model/MarketModel.java b/client/src/main/java/ru/trader/model/MarketModel.java index 0bf0ca0..df746ff 100644 --- a/client/src/main/java/ru/trader/model/MarketModel.java +++ b/client/src/main/java/ru/trader/model/MarketModel.java @@ -185,7 +185,7 @@ public class MarketModel { public void getOrders(SystemModel from, StationModel stationFrom, SystemModel to, StationModel stationTo, double balance, Consumer> result) { ProgressController progress = new ProgressController(Screeners.getMainScreen(), Localization.getString("analyzer.orders.title")); - Profile profile = ModelFabric.get(MainController.getProfile()).copy(); + Profile profile = Profile.clone(ModelFabric.get(MainController.getProfile())); profile.setBalance(balance); OrdersSearchTask task = new OrdersSearchTask(this, ModelFabric.get(from), ModelFabric.get(stationFrom), ModelFabric.get(to), ModelFabric.get(stationTo), @@ -206,9 +206,13 @@ public class MarketModel { getOrders(ModelFabric.NONE_SYSTEM, ModelFabric.NONE_STATION, ModelFabric.NONE_SYSTEM, ModelFabric.NONE_STATION, balance, result); } + public void getRoutes(StationModel stationFrom, StationModel stationTo, double balance, CrawlerSpecificator specificator, Consumer> result) { + getRoutes(stationFrom.getSystem(), stationFrom, stationTo.getSystem(), stationTo, balance, specificator, result); + } + public void getRoutes(SystemModel from, StationModel stationFrom, SystemModel to, StationModel stationTo, double balance, CrawlerSpecificator specificator, Consumer> result) { ProgressController progress = new ProgressController(Screeners.getMainScreen(), Localization.getString("analyzer.routes.title")); - Profile profile = ModelFabric.get(MainController.getProfile()).copy(); + Profile profile = Profile.clone(ModelFabric.get(MainController.getProfile())); profile.setBalance(balance); RoutesSearchTask task = new RoutesSearchTask(this, ModelFabric.get(from), ModelFabric.get(stationFrom), ModelFabric.get(to), ModelFabric.get(stationTo), diff --git a/client/src/main/java/ru/trader/model/MissionModel.java b/client/src/main/java/ru/trader/model/MissionModel.java index 63eb9b2..5524095 100644 --- a/client/src/main/java/ru/trader/model/MissionModel.java +++ b/client/src/main/java/ru/trader/model/MissionModel.java @@ -40,6 +40,16 @@ public class MissionModel { } } + protected MissionModel(MissionModel mission, boolean includeReserves){ + this.target = mission.target; + this.item = mission.item; + this.count = mission.count; + this.profit = mission.profit; + this.offer = mission.offer; + this.need = mission.need; + this.reserves = includeReserves ? mission.reserves : null; + } + public StationModel getTarget() { return target; } @@ -89,7 +99,11 @@ public class MissionModel { public void toSpecification(CrawlerSpecificator specificator){ if (isSupply()){ - specificator.buy(offer); + if (isCompleted()){ + specificator.add(ModelFabric.get(target), true); + } else { + specificator.buy(offer); + } } else if (isCourier() || isDelivery()){ specificator.add(ModelFabric.get(target), true); @@ -143,9 +157,11 @@ public class MissionModel { return Objects.hash(target, item, count); } + public static MissionModel copy(MissionModel mission){ + return new MissionModel(mission, false); + } - - public MissionModel getCopy(){ - return new MissionModel(target, item, count, profit); + public static MissionModel clone(MissionModel mission){ + return mission != null ? new MissionModel(mission, true) : null; } } diff --git a/client/src/main/java/ru/trader/model/RouteEntryModel.java b/client/src/main/java/ru/trader/model/RouteEntryModel.java index 81233af..ffe3407 100644 --- a/client/src/main/java/ru/trader/model/RouteEntryModel.java +++ b/client/src/main/java/ru/trader/model/RouteEntryModel.java @@ -134,4 +134,17 @@ public class RouteEntryModel { orders.clear(); orders.addAll(BindingsHelper.observableList(entry.getOrders(), market.getModeler()::get)); } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof RouteEntryModel)) return false; + RouteEntryModel that = (RouteEntryModel) o; + return entry.equals(that.entry); + } + + @Override + public int hashCode() { + return entry.hashCode(); + } } diff --git a/core/src/main/java/ru/trader/analysis/Route.java b/core/src/main/java/ru/trader/analysis/Route.java index 76cea03..096f485 100644 --- a/core/src/main/java/ru/trader/analysis/Route.java +++ b/core/src/main/java/ru/trader/analysis/Route.java @@ -31,6 +31,20 @@ public class Route implements Comparable { updateStats(); } + protected Route(Route route) { + this.profit = route.profit; + this.balance = route.balance; + this.distance = route.distance; + this.fuel = route.fuel; + this.time = route.time; + this.lands = route.lands; + this.refills = route.refills; + this.cargo = route.cargo; + this.entries = new ArrayList<>(route.entries.size()); + route.entries.forEach(e -> this.entries.add(RouteEntry.clone(e))); + } + + public List getEntries() { return entries; } @@ -195,6 +209,18 @@ public class Route implements Comparable { } iterator.remove(); } + updateStats(); + } + + public void dropTo(int index){ + for (ListIterator iterator = entries.listIterator(entries.size()); iterator.hasPrevious(); ) { + if (iterator.previousIndex() == index){ + break; + } + iterator.previous(); + iterator.remove(); + } + updateStats(); } void updateStats(){ @@ -295,5 +321,8 @@ public class Route implements Comparable { return new Route(entry); } + public static Route clone(Route route){ + return route != null ? new Route(route) : null; + } } diff --git a/core/src/main/java/ru/trader/analysis/RouteEntry.java b/core/src/main/java/ru/trader/analysis/RouteEntry.java index 58d6b6f..0c9a017 100644 --- a/core/src/main/java/ru/trader/analysis/RouteEntry.java +++ b/core/src/main/java/ru/trader/analysis/RouteEntry.java @@ -29,6 +29,19 @@ public class RouteEntry { reserved = 0; } + protected RouteEntry(RouteEntry entry){ + this.vendor = entry.vendor; + this.fuel = entry.fuel; + this.land = entry.land; + this.refill = entry.refill; + this.profit = entry.profit; + this.time = entry.time; + this.fulltime = entry.fulltime; + this.reserved = entry.reserved; + this.orders = new ArrayList<>(entry.orders.size()); + entry.orders.forEach(o -> this.orders.add(new OrderWrapper(o))); + } + public Vendor getVendor() { return vendor; } @@ -224,8 +237,18 @@ public class RouteEntry { this.max = order.getCount(); } + private OrderWrapper(OrderWrapper orderWrapper){ + super(orderWrapper); + this.fixed = orderWrapper.fixed; + this.max = orderWrapper.max; + } + public void reset(){ setCount(max); } } + + public static RouteEntry clone(RouteEntry entry){ + return entry != null ? new RouteEntry(entry) : null; + } } diff --git a/core/src/main/java/ru/trader/core/Order.java b/core/src/main/java/ru/trader/core/Order.java index 52782c4..7d89c3d 100644 --- a/core/src/main/java/ru/trader/core/Order.java +++ b/core/src/main/java/ru/trader/core/Order.java @@ -5,8 +5,8 @@ import org.jetbrains.annotations.NotNull; import java.util.Objects; public class Order implements Comparable { - private Offer sell; - private Offer buy; + private final Offer sell; + private final Offer buy; private double profit; private long count; @@ -27,6 +27,12 @@ public class Order implements Comparable { this.profit = (buy.getPrice() - sell.getPrice()) * count; } + protected Order(Order order){ + this(order.sell, order.buy); + this.profit = order.profit; + this.count = order.count; + } + public Offer getSell() { return sell; } @@ -143,4 +149,8 @@ public class Order implements Comparable { if (Double.isInfinite(balance)) return Math.min(limit, Math.min(supply, demand)); return (long) Math.min(limit, Math.min(Math.min(supply, demand), Math.floor(balance/sell.getPrice()))); } + + public static Order clone(Order order){ + return order != null ? new Order(order) : null; + } } diff --git a/core/src/main/java/ru/trader/core/Profile.java b/core/src/main/java/ru/trader/core/Profile.java index 115ef40..3fbddab 100644 --- a/core/src/main/java/ru/trader/core/Profile.java +++ b/core/src/main/java/ru/trader/core/Profile.java @@ -39,6 +39,26 @@ public class Profile { pathPriority = PATH_PRIORITY.FAST; } + protected Profile(Profile profile){ + this.name = profile.name; + this.balance = profile.balance; + this.docked = profile.docked; + this.jumps = profile.jumps; + this.lands = profile.lands; + this.refill = profile.refill; + this.routesCount = profile.routesCount; + this.distanceTime = profile.distanceTime; + this.jumpTime = profile.jumpTime; + this.landingTime = profile.landingTime; + this.takeoffTime = profile.takeoffTime; + this.rechargeTime = profile.rechargeTime; + this.fuelPrice = profile.fuelPrice; + this.pathPriority = profile.pathPriority; + this.system = profile.system; + this.station = profile.station; + this.ship = Ship.clone(profile.ship); + } + public String getName() { return name; } @@ -226,25 +246,8 @@ public class Profile { values.setProperty("profile.search.times.recharge", String.valueOf(rechargeTime)); ship.writeTo(values); } - - public Profile copy(){ - Profile res = new Profile(ship); - res.name = this.name; - res.balance = this.balance; - res.system = this.system; - res.station = this.station; - res.docked = this.docked; - res.jumps = this.jumps; - res.lands = this.lands; - res.refill = this.refill; - res.routesCount = this.routesCount; - res.distanceTime = this.distanceTime; - res.jumpTime = this.jumpTime; - res.landingTime = this.landingTime; - res.takeoffTime = this.takeoffTime; - res.rechargeTime = this.rechargeTime; - res.fuelPrice = this.fuelPrice; - res.pathPriority = this.pathPriority; - return res; - } + + public static Profile clone(Profile profile){ + return profile != null ? new Profile(profile) : null; + } } diff --git a/core/src/main/java/ru/trader/core/Ship.java b/core/src/main/java/ru/trader/core/Ship.java index dbdf99a..efd424c 100644 --- a/core/src/main/java/ru/trader/core/Ship.java +++ b/core/src/main/java/ru/trader/core/Ship.java @@ -20,13 +20,16 @@ public class Ship { this.engine = new Engine(2, 'E'); } - public static Ship copyOf(Ship other){ - Ship copy = new Ship(); - copy.mass = other.mass; - copy.cargo = other.cargo; - copy.tank = other.tank; - copy.engine = other.getEngine(); - return copy; + protected Ship(Ship ship){ + this.mass = ship.mass; + this.cargo = ship.cargo; + this.tank = ship.tank; + this.engine = ship.getEngine(); + + } + + public static Ship clone(Ship ship){ + return ship != null ? new Ship(ship) : ship; } public int getCargo() {