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

@@ -31,6 +31,20 @@ public class Route implements Comparable<Route> {
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() {
return entries;
}
@@ -195,6 +209,18 @@ public class Route implements Comparable<Route> {
}
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(){
@@ -295,5 +321,8 @@ public class Route implements Comparable<Route> {
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;
}
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;
}
}

View File

@@ -5,8 +5,8 @@ import org.jetbrains.annotations.NotNull;
import java.util.Objects;
public class Order implements Comparable<Order> {
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<Order> {
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<Order> {
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;
}
}

View File

@@ -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;
}
}

View File

@@ -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() {