Archived
0

refactoring clone/copy methods

This commit is contained in:
iMoHax
2015-11-19 15:36:58 +03:00
parent a9e80ec693
commit 5d7ffaca6a
8 changed files with 137 additions and 36 deletions

View File

@@ -185,7 +185,7 @@ public class MarketModel {
public void getOrders(SystemModel from, StationModel stationFrom, SystemModel to, StationModel stationTo, double balance, Consumer<ObservableList<OrderModel>> result) { public void getOrders(SystemModel from, StationModel stationFrom, SystemModel to, StationModel stationTo, double balance, Consumer<ObservableList<OrderModel>> result) {
ProgressController progress = new ProgressController(Screeners.getMainScreen(), Localization.getString("analyzer.orders.title")); 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); profile.setBalance(balance);
OrdersSearchTask task = new OrdersSearchTask(this, OrdersSearchTask task = new OrdersSearchTask(this,
ModelFabric.get(from), ModelFabric.get(stationFrom), ModelFabric.get(to), ModelFabric.get(stationTo), 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); 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<ObservableList<RouteModel>> 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<ObservableList<RouteModel>> result) { public void getRoutes(SystemModel from, StationModel stationFrom, SystemModel to, StationModel stationTo, double balance, CrawlerSpecificator specificator, Consumer<ObservableList<RouteModel>> result) {
ProgressController progress = new ProgressController(Screeners.getMainScreen(), Localization.getString("analyzer.routes.title")); 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); profile.setBalance(balance);
RoutesSearchTask task = new RoutesSearchTask(this, RoutesSearchTask task = new RoutesSearchTask(this,
ModelFabric.get(from), ModelFabric.get(stationFrom), ModelFabric.get(to), ModelFabric.get(stationTo), ModelFabric.get(from), ModelFabric.get(stationFrom), ModelFabric.get(to), ModelFabric.get(stationTo),

View File

@@ -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() { public StationModel getTarget() {
return target; return target;
} }
@@ -89,7 +99,11 @@ public class MissionModel {
public void toSpecification(CrawlerSpecificator specificator){ public void toSpecification(CrawlerSpecificator specificator){
if (isSupply()){ if (isSupply()){
if (isCompleted()){
specificator.add(ModelFabric.get(target), true);
} else {
specificator.buy(offer); specificator.buy(offer);
}
} else } else
if (isCourier() || isDelivery()){ if (isCourier() || isDelivery()){
specificator.add(ModelFabric.get(target), true); specificator.add(ModelFabric.get(target), true);
@@ -143,9 +157,11 @@ public class MissionModel {
return Objects.hash(target, item, count); return Objects.hash(target, item, count);
} }
public static MissionModel copy(MissionModel mission){
return new MissionModel(mission, false);
}
public static MissionModel clone(MissionModel mission){
public MissionModel getCopy(){ return mission != null ? new MissionModel(mission, true) : null;
return new MissionModel(target, item, count, profit);
} }
} }

View File

@@ -134,4 +134,17 @@ public class RouteEntryModel {
orders.clear(); orders.clear();
orders.addAll(BindingsHelper.observableList(entry.getOrders(), market.getModeler()::get)); 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();
}
} }

View File

@@ -31,6 +31,20 @@ public class Route implements Comparable<Route> {
updateStats(); 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<RouteEntry> getEntries() { public List<RouteEntry> getEntries() {
return entries; return entries;
} }
@@ -195,6 +209,18 @@ public class Route implements Comparable<Route> {
} }
iterator.remove(); iterator.remove();
} }
updateStats();
}
public void dropTo(int index){
for (ListIterator<RouteEntry> iterator = entries.listIterator(entries.size()); iterator.hasPrevious(); ) {
if (iterator.previousIndex() == index){
break;
}
iterator.previous();
iterator.remove();
}
updateStats();
} }
void updateStats(){ void updateStats(){
@@ -295,5 +321,8 @@ public class Route implements Comparable<Route> {
return new Route(entry); return new Route(entry);
} }
public static Route clone(Route route){
return route != null ? new Route(route) : null;
}
} }

View File

@@ -29,6 +29,19 @@ public class RouteEntry {
reserved = 0; 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() { public Vendor getVendor() {
return vendor; return vendor;
} }
@@ -224,8 +237,18 @@ public class RouteEntry {
this.max = order.getCount(); this.max = order.getCount();
} }
private OrderWrapper(OrderWrapper orderWrapper){
super(orderWrapper);
this.fixed = orderWrapper.fixed;
this.max = orderWrapper.max;
}
public void reset(){ public void reset(){
setCount(max); setCount(max);
} }
} }
public static RouteEntry clone(RouteEntry entry){
return entry != null ? new RouteEntry(entry) : null;
}
} }

View File

@@ -5,8 +5,8 @@ import org.jetbrains.annotations.NotNull;
import java.util.Objects; import java.util.Objects;
public class Order implements Comparable<Order> { public class Order implements Comparable<Order> {
private Offer sell; private final Offer sell;
private Offer buy; private final Offer buy;
private double profit; private double profit;
private long count; private long count;
@@ -27,6 +27,12 @@ public class Order implements Comparable<Order> {
this.profit = (buy.getPrice() - sell.getPrice()) * count; 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() { public Offer getSell() {
return sell; return sell;
} }
@@ -143,4 +149,8 @@ public class Order implements Comparable<Order> {
if (Double.isInfinite(balance)) return Math.min(limit, Math.min(supply, demand)); 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()))); 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;
}
} }

View File

@@ -39,6 +39,26 @@ public class Profile {
pathPriority = PATH_PRIORITY.FAST; 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() { public String getName() {
return name; return name;
} }
@@ -227,24 +247,7 @@ public class Profile {
ship.writeTo(values); ship.writeTo(values);
} }
public Profile copy(){ public static Profile clone(Profile profile){
Profile res = new Profile(ship); return profile != null ? new Profile(profile) : null;
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;
} }
} }

View File

@@ -20,13 +20,16 @@ public class Ship {
this.engine = new Engine(2, 'E'); this.engine = new Engine(2, 'E');
} }
public static Ship copyOf(Ship other){ protected Ship(Ship ship){
Ship copy = new Ship(); this.mass = ship.mass;
copy.mass = other.mass; this.cargo = ship.cargo;
copy.cargo = other.cargo; this.tank = ship.tank;
copy.tank = other.tank; this.engine = ship.getEngine();
copy.engine = other.getEngine();
return copy; }
public static Ship clone(Ship ship){
return ship != null ? new Ship(ship) : ship;
} }
public int getCargo() { public int getCargo() {