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

formatted, organized imports

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