Commit 15c8e7b3 authored by mey's avatar mey
Browse files

formatted, organized imports

parent 2b72204a
...@@ -23,11 +23,11 @@ import sim.util.Properties; ...@@ -23,11 +23,11 @@ import sim.util.Properties;
* @param <K> * @param <K>
* type of enum * type of enum
* @param * @param
* <Q> * <Q>
* type of {@link Quantity} used * type of {@link Quantity} used
*/ */
public class EnumToAmountMap<K extends Enum<K>, Q extends Quantity> extends EnumMap<K, Amount<Q>> public class EnumToAmountMap<K extends Enum<K>, Q extends Quantity> extends EnumMap<K, Amount<Q>>
implements ProvidesInspector { implements ProvidesInspector {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
/** The runtime type of enum used. */ /** The runtime type of enum used. */
...@@ -46,7 +46,7 @@ public class EnumToAmountMap<K extends Enum<K>, Q extends Quantity> extends Enum ...@@ -46,7 +46,7 @@ public class EnumToAmountMap<K extends Enum<K>, Q extends Quantity> extends Enum
* @param enumType * @param enumType
*/ */
public EnumToAmountMap(Class<K> enumType) { public EnumToAmountMap(Class<K> enumType) {
this(enumType, null); this(enumType, null);
} }
/** /**
...@@ -59,11 +59,11 @@ public class EnumToAmountMap<K extends Enum<K>, Q extends Quantity> extends Enum ...@@ -59,11 +59,11 @@ public class EnumToAmountMap<K extends Enum<K>, Q extends Quantity> extends Enum
* @param displayUnit * @param displayUnit
*/ */
public EnumToAmountMap(Class<K> enumType, Unit<Q> storeUnit, Unit<Q> displayUnit) { public EnumToAmountMap(Class<K> enumType, Unit<Q> storeUnit, Unit<Q> displayUnit) {
super(enumType); super(enumType);
this.enumType = enumType; this.enumType = enumType;
this.storeUnit = storeUnit; this.storeUnit = storeUnit;
this.displayUnit = displayUnit; this.displayUnit = displayUnit;
} }
/** /**
...@@ -74,7 +74,7 @@ public class EnumToAmountMap<K extends Enum<K>, Q extends Quantity> extends Enum ...@@ -74,7 +74,7 @@ public class EnumToAmountMap<K extends Enum<K>, Q extends Quantity> extends Enum
* @param storeUnit * @param storeUnit
*/ */
public EnumToAmountMap(Class<K> enumType, Unit<Q> storeUnit) { public EnumToAmountMap(Class<K> enumType, Unit<Q> storeUnit) {
this(enumType, storeUnit, storeUnit); this(enumType, storeUnit, storeUnit);
} }
/** /**
...@@ -88,7 +88,7 @@ public class EnumToAmountMap<K extends Enum<K>, Q extends Quantity> extends Enum ...@@ -88,7 +88,7 @@ public class EnumToAmountMap<K extends Enum<K>, Q extends Quantity> extends Enum
* @return previously associated value with that constant * @return previously associated value with that constant
*/ */
protected Amount<Q> put(K enumConstant, double valueInDisplayUnit) { protected Amount<Q> put(K enumConstant, double valueInDisplayUnit) {
return put(enumConstant, Amount.valueOf(valueInDisplayUnit, getDisplayUnit())); return put(enumConstant, Amount.valueOf(valueInDisplayUnit, getDisplayUnit()));
} }
/** /**
...@@ -102,7 +102,7 @@ public class EnumToAmountMap<K extends Enum<K>, Q extends Quantity> extends Enum ...@@ -102,7 +102,7 @@ public class EnumToAmountMap<K extends Enum<K>, Q extends Quantity> extends Enum
* @return previously associated value with that constant * @return previously associated value with that constant
*/ */
protected Amount<Q> put(K enumConstant, long valueInDisplayUnit) { protected Amount<Q> put(K enumConstant, long valueInDisplayUnit) {
return put(enumConstant, Amount.valueOf(valueInDisplayUnit, getDisplayUnit())); return put(enumConstant, Amount.valueOf(valueInDisplayUnit, getDisplayUnit()));
} }
/** /**
...@@ -111,7 +111,7 @@ public class EnumToAmountMap<K extends Enum<K>, Q extends Quantity> extends Enum ...@@ -111,7 +111,7 @@ public class EnumToAmountMap<K extends Enum<K>, Q extends Quantity> extends Enum
* @return the {@link Unit} amounts are converted when stored in the map * @return the {@link Unit} amounts are converted when stored in the map
*/ */
protected final Unit<Q> getStoreUnit() { protected final Unit<Q> getStoreUnit() {
return storeUnit; return storeUnit;
} }
/** /**
...@@ -122,7 +122,7 @@ public class EnumToAmountMap<K extends Enum<K>, Q extends Quantity> extends Enum ...@@ -122,7 +122,7 @@ public class EnumToAmountMap<K extends Enum<K>, Q extends Quantity> extends Enum
* {@link Inspector} * {@link Inspector}
*/ */
protected final Unit<Q> getDisplayUnit() { protected final Unit<Q> getDisplayUnit() {
return displayUnit; return displayUnit;
} }
/** /**
...@@ -130,60 +130,60 @@ public class EnumToAmountMap<K extends Enum<K>, Q extends Quantity> extends Enum ...@@ -130,60 +130,60 @@ public class EnumToAmountMap<K extends Enum<K>, Q extends Quantity> extends Enum
*/ */
@Override @Override
public Amount<Q> put(K key, Amount<Q> value) { public Amount<Q> put(K key, Amount<Q> value) {
return super.put(key, storeUnit != null ? value.to(storeUnit) : value); return super.put(key, storeUnit != null ? value.to(storeUnit) : value);
} }
@Override @Override
public Inspector provideInspector(GUIState state, String name) { public Inspector provideInspector(GUIState state, String name) {
SimpleInspector inspector = new SimpleInspector(new MyProperties(), state, name); SimpleInspector inspector = new SimpleInspector(new MyProperties(), state, name);
inspector.setTitle(getClass().getSimpleName()); inspector.setTitle(getClass().getSimpleName());
return inspector; return inspector;
} }
private class MyProperties extends Properties { private class MyProperties extends Properties {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private final K[] enumConstants = enumType.getEnumConstants(); private final K[] enumConstants = enumType.getEnumConstants();
@Override @Override
public boolean isVolatile() { public boolean isVolatile() {
return false; return false;
} }
@Override @Override
public int numProperties() { public int numProperties() {
return size(); return size();
} }
@Override @Override
public Object getValue(int index) { public Object getValue(int index) {
return get(enumConstants[index]).to(displayUnit).toString(); return get(enumConstants[index]).to(displayUnit).toString();
} }
@Override @Override
public boolean isReadWrite(int index) { public boolean isReadWrite(int index) {
if (index < 0 || index > numProperties()) { if (index < 0 || index > numProperties()) {
return false; return false;
} }
return true; return true;
} }
@Override @Override
public String getName(int index) { public String getName(int index) {
return enumConstants[index].name(); return enumConstants[index].name();
} }
@Override @Override
public Class<?> getType(int index) { public Class<?> getType(int index) {
return String.class; return String.class;
} }
@Override @Override
protected Object _setValue(int index, Object value) { protected Object _setValue(int index, Object value) {
Amount<Q> amount = AmountUtil.parseAmount(value.toString(), displayUnit); Amount<Q> amount = AmountUtil.parseAmount(value.toString(), displayUnit);
put(enumConstants[index], amount.to(storeUnit)); put(enumConstants[index], amount.to(storeUnit));
return amount; return amount;
} }
} }
} }
\ No newline at end of file
...@@ -24,12 +24,12 @@ import sim.util.Proxiable; ...@@ -24,12 +24,12 @@ import sim.util.Proxiable;
* *
* @author mey * @author mey
* @param * @param
* <Q> * <Q>
* the stored {@link Quantity} * the stored {@link Quantity}
* *
*/ */
public abstract class AbstractLimitedStoragePipeline<Q extends Quantity> public abstract class AbstractLimitedStoragePipeline<Q extends Quantity>
implements StoragePipeline<Q>, LimitedStorage<Q>, Proxiable { implements StoragePipeline<Q>, LimitedStorage<Q>, Proxiable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private final LimitedStorage<Q> sum; private final LimitedStorage<Q> sum;
...@@ -42,21 +42,21 @@ public abstract class AbstractLimitedStoragePipeline<Q extends Quantity> ...@@ -42,21 +42,21 @@ public abstract class AbstractLimitedStoragePipeline<Q extends Quantity>
* for this {@link StoragePipeline} * for this {@link StoragePipeline}
*/ */
public AbstractLimitedStoragePipeline(LimitedStorage<Q> sum) { public AbstractLimitedStoragePipeline(LimitedStorage<Q> sum) {
this.sum = sum; this.sum = sum;
syncQueue(); syncQueue();
} }
/** Clears queue and add an amount equal to sum. */ /** Clears queue and add an amount equal to sum. */
private void syncQueue() { private void syncQueue() {
queue.clear(); queue.clear();
Amount<Q> amount = sum.getAmount(); Amount<Q> amount = sum.getAmount();
// if there is an amount in sum, add it to pipeline // if there is an amount in sum, add it to pipeline
if (amount.getEstimatedValue() > 0) { if (amount.getEstimatedValue() > 0) {
queue.offer(createDelayedStorage(amount)); queue.offer(createDelayedStorage(amount));
} else if (amount.getEstimatedValue() < 0) { } else if (amount.getEstimatedValue() < 0) {
throw new IllegalArgumentException("Negative amounts are not supported."); throw new IllegalArgumentException("Negative amounts are not supported.");
} }
} }
/** /**
...@@ -70,12 +70,12 @@ public abstract class AbstractLimitedStoragePipeline<Q extends Quantity> ...@@ -70,12 +70,12 @@ public abstract class AbstractLimitedStoragePipeline<Q extends Quantity>
@Override @Override
public boolean atLowerLimit() { public boolean atLowerLimit() {
return sum.atLowerLimit(); return sum.atLowerLimit();
} }
@Override @Override
public boolean atUpperLimit() { public boolean atUpperLimit() {
return sum.atUpperLimit(); return sum.atUpperLimit();
} }
/** /**
...@@ -85,46 +85,46 @@ public abstract class AbstractLimitedStoragePipeline<Q extends Quantity> ...@@ -85,46 +85,46 @@ public abstract class AbstractLimitedStoragePipeline<Q extends Quantity>
*/ */
@Override @Override
public Amount<Q> drainExpired() { public Amount<Q> drainExpired() {
Amount<Q> returnAmount = AmountUtil.zero(sum.getAmount()); Amount<Q> returnAmount = AmountUtil.zero(sum.getAmount());
/* /*
* elements that cannot be removed due to sum and need to be put back * elements that cannot be removed due to sum and need to be put back
* into queue * into queue
*/ */
Collection<DelayedStorage<Q>> holdBack = new ArrayList<>(); Collection<DelayedStorage<Q>> holdBack = new ArrayList<>();
while (true) { while (true) {
DelayedStorage<Q> head = queue.poll(); DelayedStorage<Q> head = queue.poll();
if (head != null) { if (head != null) {
Amount<Q> amount = head.getAmount(); Amount<Q> amount = head.getAmount();
// subtract amount of this storage from sum // subtract amount of this storage from sum
Amount<Q> required = sum.store(amount.opposite()); Amount<Q> required = sum.store(amount.opposite());
// if amount could be subtracted: // if amount could be subtracted:
if (required != null) { if (required != null) {
// sum the amount received from storage (it is negative) // sum the amount received from storage (it is negative)
returnAmount = returnAmount.minus(required); returnAmount = returnAmount.minus(required);
} else { } else {
holdBack.add(head); holdBack.add(head);
} }
} else { } else {
// no expired elements // no expired elements
break; break;
} }
} }
queue.addAll(holdBack); queue.addAll(holdBack);
// clear to prevent ever increasing numeric error // clear to prevent ever increasing numeric error
if (queue.isEmpty()) { if (queue.isEmpty()) {
clear(); clear();
} }
return returnAmount; return returnAmount;
} }
@Override @Override
public Collection<? extends Storage<Q>> getContent() { public Collection<? extends Storage<Q>> getContent() {
return Collections.unmodifiableCollection(queue); return Collections.unmodifiableCollection(queue);
} }
/** /**
...@@ -133,10 +133,10 @@ public abstract class AbstractLimitedStoragePipeline<Q extends Quantity> ...@@ -133,10 +133,10 @@ public abstract class AbstractLimitedStoragePipeline<Q extends Quantity>
*/ */
@Override @Override
public ChangeResult<Q> add(Amount<Q> amount) { public ChangeResult<Q> add(Amount<Q> amount) {
ChangeResult<Q> result = sum.add(amount); ChangeResult<Q> result = sum.add(amount);
addToPipeline(result.getStored()); addToPipeline(result.getStored());
return result; return result;
} }
/** /**
...@@ -146,49 +146,49 @@ public abstract class AbstractLimitedStoragePipeline<Q extends Quantity> ...@@ -146,49 +146,49 @@ public abstract class AbstractLimitedStoragePipeline<Q extends Quantity>
@Override @Override
public Amount<Q> store(Amount<Q> amount) { public Amount<Q> store(Amount<Q> amount) {
Amount<Q> required = sum.store(amount); Amount<Q> required = sum.store(amount);
if (required != null) { if (required != null) {
addToPipeline(amount); addToPipeline(amount);
} }
return required; return required;
} }
private void addToPipeline(Amount<Q> amount) { private void addToPipeline(Amount<Q> amount) {
if (amount.getEstimatedValue() < 0) { if (amount.getEstimatedValue() < 0) {
throw new IllegalArgumentException(amount + " cannot be added, must be positive."); throw new IllegalArgumentException(amount + " cannot be added, must be positive.");
} }
// do not add storage for zero amounts, e.g. storage is already at limit // do not add storage for zero amounts, e.g. storage is already at limit
if (amount.getEstimatedValue() > 0) { if (amount.getEstimatedValue() > 0) {
queue.add(createDelayedStorage(amount)); queue.add(createDelayedStorage(amount));
} }
} }
@Override @Override
public Amount<Q> clear() { public Amount<Q> clear() {
Amount<Q> clearAmount = sum.clear(); Amount<Q> clearAmount = sum.clear();
syncQueue(); syncQueue();
return clearAmount; return clearAmount;
} }
@Override @Override
public Amount<Q> getAmount() { public Amount<Q> getAmount() {
return sum.getAmount(); return sum.getAmount();
} }
@Override @Override
public double doubleValue() { public double doubleValue() {
return sum.doubleValue(); return sum.doubleValue();
} }
@Override @Override
public Object propertiesProxy() { public Object propertiesProxy() {
return new MyPropertiesProxy(); return new MyPropertiesProxy();
} }
@Override @Override
public String toString() { public String toString() {
return getClass().getSimpleName() + "[sum=" + sum + "]"; return getClass().getSimpleName() + "[sum=" + sum + "]";
} }
/** /**
...@@ -197,23 +197,23 @@ public abstract class AbstractLimitedStoragePipeline<Q extends Quantity> ...@@ -197,23 +197,23 @@ public abstract class AbstractLimitedStoragePipeline<Q extends Quantity>
* *
* @author mey * @author mey
* @param * @param
* <Q> * <Q>
* the stored {@link Quantity} * the stored {@link Quantity}
* *
*/ */
public static abstract class DelayedStorage<Q extends Quantity> extends BaseStorage<Q> implements Delayed { public static abstract class DelayedStorage<Q extends Quantity> extends BaseStorage<Q> implements Delayed {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public DelayedStorage(Amount<Q> amount) { public DelayedStorage(Amount<Q> amount) {
super(amount); super(amount);
} }
@Override @Override
public int compareTo(Delayed o) { public int compareTo(Delayed o) {
// from TimerQueue#DelayedTimer // from TimerQueue#DelayedTimer
long diff = getDelay(TimeUnit.NANOSECONDS) - o.getDelay(TimeUnit.NANOSECONDS); long diff = getDelay(TimeUnit.NANOSECONDS) - o.getDelay(TimeUnit.NANOSECONDS);
return (diff == 0) ? 0 : ((diff < 0) ? -1 : 1); return (diff == 0) ? 0 : ((diff < 0) ? -1 : 1);
} }
} }
/** /**
...@@ -227,44 +227,44 @@ public abstract class AbstractLimitedStoragePipeline<Q extends Quantity> ...@@ -227,44 +227,44 @@ public abstract class AbstractLimitedStoragePipeline<Q extends Quantity>
* type of elements held in the queue * type of elements held in the queue
*/ */
private static class SerializableDelayQueue<E extends Delayed> extends PriorityQueue<E> { private static class SerializableDelayQueue<E extends Delayed> extends PriorityQueue<E> {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
/** /**
* Retrieves and removes the head of this queue, or returns * Retrieves and removes the head of this queue, or returns
* <tt>null</tt> if this queue has no elements with an expired delay. * <tt>null</tt> if this queue has no elements with an expired delay.
* *
* @return the head of this queue, or <tt>null</tt> if this queue has no * @return the head of this queue, or <tt>null</tt> if this queue has no
* elements with an expired delay * elements with an expired delay
* @see DelayQueue#poll() * @see DelayQueue#poll()
*/ */
@Override @Override
public E poll() { public E poll() {
E first = peek(); E first = peek();
if (first == null || first.getDelay(TimeUnit.NANOSECONDS) > 0) { if (first == null || first.getDelay(TimeUnit.NANOSECONDS) > 0) {
return null; return null;
} else { } else {
return super.poll(); return super.poll();