Commit 3e2f52a8 authored by mey's avatar mey
Browse files

formatted, organized imports

parent 3110fc66
......@@ -40,7 +40,7 @@ abstract class AbstractDerivedMap<T extends PathfindingMap> extends AbstractDyna
* height of map
*/
public AbstractDerivedMap(int width, int height) {
super(width, height);
super(width, height);
}
/**
......@@ -60,7 +60,7 @@ abstract class AbstractDerivedMap<T extends PathfindingMap> extends AbstractDyna
* @return the name which the map was associated with
*/
public String addMap(T map) {
return addMap(map, NEUTRAL_WEIGHT);
return addMap(map, NEUTRAL_WEIGHT);
}
/**
......@@ -81,9 +81,9 @@ abstract class AbstractDerivedMap<T extends PathfindingMap> extends AbstractDyna
* @return the name which the map was associated with
*/
public String addMap(T map, double weight) {
String name = addMapInternal(map, weight);
forceUpdateAll();
return name;
String name = addMapInternal(map, weight);
forceUpdateAll();
return name;
}
/**
......@@ -101,29 +101,29 @@ abstract class AbstractDerivedMap<T extends PathfindingMap> extends AbstractDyna
* @return the name which the map was associated with
*/
final String addMapInternal(T map, double weight) {
if (map.getWidth() != getWidth() || map.getHeight() != getHeight()) {
throw new IllegalArgumentException("Expected: is <" + getWidth() + ", " + getHeight() + ">\n" + "but: was <"
+ map.getWidth() + ", " + map.getHeight() + ">");
}
if (map == this) {
throw new IllegalArgumentException("Cannot add itself as an underlying map.");
}
if (map instanceof MapChangeNotifier) {
((MapChangeNotifier) map).addListener(this);
}
if (map.getWidth() != getWidth() || map.getHeight() != getHeight()) {
throw new IllegalArgumentException("Expected: is <" + getWidth() + ", " + getHeight() + ">\n" + "but: was <"
+ map.getWidth() + ", " + map.getHeight() + ">");
}
if (map == this) {
throw new IllegalArgumentException("Cannot add itself as an underlying map.");
}
if (map instanceof MapChangeNotifier) {
((MapChangeNotifier) map).addListener(this);
}
String name;
if (map instanceof NamedMap) {
name = ((NamedMap) map).getName();
}
// if map is not named: use simple class name and hash code
else {
name = map.getClass().getSimpleName() + "@" + Integer.toHexString(map.hashCode());
}
String name;
if (map instanceof NamedMap) {
name = ((NamedMap) map).getName();
}
// if map is not named: use simple class name and hash code
else {
name = map.getClass().getSimpleName() + "@" + Integer.toHexString(map.hashCode());
}
underlyingMaps.put(name, map);
weights.put(map, weight);
return name;
underlyingMaps.put(name, map);
weights.put(map, weight);
return name;
}
/**
......@@ -141,11 +141,11 @@ abstract class AbstractDerivedMap<T extends PathfindingMap> extends AbstractDyna
* removed
*/
public T removeMap(String name) {
T map = underlyingMaps.get(name);
if (removeMap(map)) {
return map;
}
return null;
T map = underlyingMaps.get(name);
if (removeMap(map)) {
return map;
}
return null;
}
/**
......@@ -162,11 +162,11 @@ abstract class AbstractDerivedMap<T extends PathfindingMap> extends AbstractDyna
* @return <code>true</code> if the map could be removed
*/
public boolean removeMap(Object map) {
if (removeMapInternal(map)) {
forceUpdateAll();
return true;
}
return false;
if (removeMapInternal(map)) {
forceUpdateAll();
return true;
}
return false;
}
/**
......@@ -179,25 +179,25 @@ abstract class AbstractDerivedMap<T extends PathfindingMap> extends AbstractDyna
* @return <code>true</code> if the map could be removed
*/
private boolean removeMapInternal(Object map) {
if (underlyingMaps.values().remove(map)) {
if (map instanceof MapChangeNotifier) {
((MapChangeNotifier) map).removeListener(this);
}
weights.remove(map);
return true;
}
return false;
if (underlyingMaps.values().remove(map)) {
if (map instanceof MapChangeNotifier) {
((MapChangeNotifier) map).removeListener(this);
}
weights.remove(map);
return true;
}
return false;
}
/** Removes all underlying maps. */
public void clear() {
for (Iterator<T> iterator = underlyingMaps.values().iterator(); iterator.hasNext();) {
T map = iterator.next();
if (removeMapInternal(map)) {
iterator.remove();
}
}
forceUpdateAll();
for (Iterator<T> iterator = underlyingMaps.values().iterator(); iterator.hasNext();) {
T map = iterator.next();
if (removeMapInternal(map)) {
iterator.remove();
}
}
forceUpdateAll();
}
/**
......@@ -207,7 +207,7 @@ abstract class AbstractDerivedMap<T extends PathfindingMap> extends AbstractDyna
* @return the underlying map with the given name
*/
public T getUnderlyingMap(String name) {
return underlyingMaps.get(name);
return underlyingMaps.get(name);
}
/**
......@@ -220,10 +220,10 @@ abstract class AbstractDerivedMap<T extends PathfindingMap> extends AbstractDyna
* @return weight that was associated with the map before
*/
public final double setWeight(T map, double weight) {
Double oldWeight = weights.put(map, weight);
forceUpdateAll();
Double oldWeight = weights.put(map, weight);
forceUpdateAll();
return oldWeight;
return oldWeight;
}
/**
......@@ -236,35 +236,35 @@ abstract class AbstractDerivedMap<T extends PathfindingMap> extends AbstractDyna
* @return weight that was associated with the map before
*/
public final double setWeight(String name, double weight) {
if (underlyingMaps.containsKey(name)) {
return setWeight(underlyingMaps.get(name), weight);
}
throw new IllegalArgumentException(name + " is not associated with an underlying map.");
if (underlyingMaps.containsKey(name)) {
return setWeight(underlyingMaps.get(name), weight);
}
throw new IllegalArgumentException(name + " is not associated with an underlying map.");
}
@Override
public Map<T, String> applyChanges(Changes<T> changes) {
if (getWidth() != changes.getWidth() || getHeight() != changes.getHeight()) {
throw new IllegalArgumentException(
"Dimensions from " + changes + " must match " + getWidth() + ", " + getHeight() + ".");
}
if (getWidth() != changes.getWidth() || getHeight() != changes.getHeight()) {
throw new IllegalArgumentException(
"Dimensions from " + changes + " must match " + getWidth() + ", " + getHeight() + ".");
}
Map<T, String> names = new HashMap<>();
for (T map : changes.getMapsToAdd()) {
names.put(map, addMapInternal(map, NEUTRAL_WEIGHT));
}
for (T map : changes.getMapsToRemove()) {
removeMapInternal(map);
}
weights.putAll(changes.getWeightsToPut());
forceUpdateAll();
Map<T, String> names = new HashMap<>();
for (T map : changes.getMapsToAdd()) {
names.put(map, addMapInternal(map, NEUTRAL_WEIGHT));
}
for (T map : changes.getMapsToRemove()) {
removeMapInternal(map);
}
weights.putAll(changes.getWeightsToPut());
forceUpdateAll();
return Collections.unmodifiableMap(names);
return Collections.unmodifiableMap(names);
}
@Override
public Changes<T> content() {
return new Changes<>(getWidth(), getHeight(), underlyingMaps.values(), weights, Collections.<T> emptyList());
return new Changes<>(getWidth(), getHeight(), underlyingMaps.values(), weights, Collections.<T> emptyList());
}
/**
......@@ -275,7 +275,7 @@ abstract class AbstractDerivedMap<T extends PathfindingMap> extends AbstractDyna
* @return weight factor for {@code map}
*/
protected double getWeight(T map) {
return weights.get(map);
return weights.get(map);
}
/**
......@@ -284,22 +284,22 @@ abstract class AbstractDerivedMap<T extends PathfindingMap> extends AbstractDyna
* @return pathfinding maps
*/
protected final Collection<T> getUnderlyingMaps() {
return underlyingMaps.values();
return underlyingMaps.values();
}
@Override
public void updateIfDirty(int x, int y) {
// update underlying maps before updating itself
for (T map : underlyingMaps.values()) {
if (map instanceof DynamicMap) {
((DynamicMap) map).updateIfDirty(x, y);
}
}
super.updateIfDirty(x, y);
// update underlying maps before updating itself
for (T map : underlyingMaps.values()) {
if (map instanceof DynamicMap) {
((DynamicMap) map).updateIfDirty(x, y);
}
}
super.updateIfDirty(x, y);
}
@Override
public String toString() {
return super.toString() + underlyingMaps.keySet();
return super.toString() + underlyingMaps.keySet();
}
}
\ No newline at end of file
......@@ -48,11 +48,11 @@ abstract class AbstractDynamicMap extends BasicMapChangeNotifier implements Name
* vertical extend from position when marking dirty
*/
public AbstractDynamicMap(int width, int height, int xExtend, int yExtend) {
this.width = width;
this.height = height;
this.xExtend = xExtend;
this.yExtend = yExtend;
Int2DCache.adjustCacheSize(width, height);
this.width = width;
this.height = height;
this.xExtend = xExtend;
this.yExtend = yExtend;
Int2DCache.adjustCacheSize(width, height);
}
/**
......@@ -63,57 +63,57 @@ abstract class AbstractDynamicMap extends BasicMapChangeNotifier implements Name
* @param height
*/
public AbstractDynamicMap(int width, int height) {
this(width, height, 0, 0);
this(width, height, 0, 0);
}
@Override
public void markDirty(int x, int y) {
int xMin = Math.max(0, x - xExtend);
int xMax = Math.min(getWidth(), x + xExtend + 1);
int yMin = Math.max(0, y - yExtend);
int yMax = Math.min(getHeight(), y + xExtend + 1);
for (int i = xMin; i < xMax; i++) {
for (int j = yMin; j < yMax; j++) {
dirtySet.add(Int2DCache.get(i, j));
}
}
int xMin = Math.max(0, x - xExtend);
int xMax = Math.min(getWidth(), x + xExtend + 1);
int yMin = Math.max(0, y - yExtend);
int yMax = Math.min(getHeight(), y + xExtend + 1);
for (int i = xMin; i < xMax; i++) {
for (int j = yMin; j < yMax; j++) {
dirtySet.add(Int2DCache.get(i, j));
}
}
}
@Override
public void forceUpdate(int x, int y) {
updateCleanNotify(Int2DCache.get(x, y));
updateCleanNotify(Int2DCache.get(x, y));
}
@Override
public final void forceUpdateAll() {
for (int x = 0; x < getWidth(); x++) {
for (int y = 0; y < getHeight(); y++) {
Int2D location = Int2DCache.get(x, y);
dirtySet.remove(location);
update(location.x, location.y);
}
}
notifyListenersAll();
for (int x = 0; x < getWidth(); x++) {
for (int y = 0; y < getHeight(); y++) {
Int2D location = Int2DCache.get(x, y);
dirtySet.remove(location);
update(location.x, location.y);
}
}
notifyListenersAll();
}
@Override
public void updateIfDirty(int x, int y) {
// if requested value is dated: it needs to be updated
Int2D location = Int2DCache.get(x, y);
if (dirtySet.contains(location)) {
updateCleanNotify(location);
}
// if requested value is dated: it needs to be updated
Int2D location = Int2DCache.get(x, y);
if (dirtySet.contains(location)) {
updateCleanNotify(location);
}
}
@Override
public final void updateIfDirtyAll() {
for (Iterator<Int2D> iterator = dirtySet.iterator(); iterator.hasNext();) {
Int2D location = iterator.next();
iterator.remove();
update(location.x, location.y);
notifyListeners(location.x, location.y);
}
for (Iterator<Int2D> iterator = dirtySet.iterator(); iterator.hasNext();) {
Int2D location = iterator.next();
iterator.remove();
update(location.x, location.y);
notifyListeners(location.x, location.y);
}
}
/**
......@@ -122,9 +122,9 @@ abstract class AbstractDynamicMap extends BasicMapChangeNotifier implements Name
* @param location
*/
private void updateCleanNotify(Int2D location) {
dirtySet.remove(location);
update(location.x, location.y);
notifyListeners(location.x, location.y);
dirtySet.remove(location);
update(location.x, location.y);
notifyListeners(location.x, location.y);
}
/**
......@@ -133,7 +133,7 @@ abstract class AbstractDynamicMap extends BasicMapChangeNotifier implements Name
* @return <code>true</code> if location is marked dirty
*/
boolean isDirty(int x, int y) {
return dirtySet.contains(Int2DCache.get(x, y));
return dirtySet.contains(Int2DCache.get(x, y));
}
/**
......@@ -147,7 +147,7 @@ abstract class AbstractDynamicMap extends BasicMapChangeNotifier implements Name
@Override
public String getName() {
return name;
return name;
}
/**
......@@ -155,22 +155,22 @@ abstract class AbstractDynamicMap extends BasicMapChangeNotifier implements Name
* the name of this pathfinding map
*/
public void setName(String name) {
this.name = name;
this.name = name;
}
@Override
public int getWidth() {
return width;
return width;
}
@Override
public int getHeight() {
return height;
return height;
}
@Override
public String toString() {
return getName();
return getName();
}
}
\ No newline at end of file
......@@ -25,17 +25,17 @@ class BasicMapChangeNotifier implements MapChangeNotifier, Serializable {
@Override
public final void addListener(DynamicMap listener) {
dynamicMaps.add(listener);
dynamicMaps.add(listener);
}
@Override
public final void removeListener(Object listener) {
dynamicMaps.remove(listener);
dynamicMaps.remove(listener);
}
@Override
public void setUpdateMode(UpdateMode mode) {
updateMode = mode;
updateMode = mode;
}
/**
......@@ -49,24 +49,24 @@ class BasicMapChangeNotifier implements MapChangeNotifier, Serializable {
* y-coordinate of changed location
*/
protected final void notifyListeners(int x, int y) {
for (DynamicMap listener : dynamicMaps) {
switch (updateMode) {
case LAZY:
listener.markDirty(x, y);
break;
case EAGER:
listener.forceUpdate(x, y);
break;
default:
throw new UnsupportedOperationException(updateMode + " not yet implemented.");
}
}
for (DynamicMap listener : dynamicMaps) {
switch (updateMode) {
case LAZY:
listener.markDirty(x, y);
break;
case EAGER:
listener.forceUpdate(x, y);
break;
default:
throw new UnsupportedOperationException(updateMode + " not yet implemented.");
}
}
}
/** Notifies listeners about a change of all locations. */
protected final void notifyListenersAll() {
for (DynamicMap listener : dynamicMaps) {
listener.forceUpdateAll();
}
for (DynamicMap listener : dynamicMaps) {
listener.forceUpdateAll();
}
}
}
\ No newline at end of file
......@@ -33,7 +33,7 @@ import sim.util.Double2D;
* the type of underlying pathfinding maps
*/
abstract class DerivedFlowMap<T extends PathfindingMap> extends AbstractDerivedMap<T>
implements GridBackedFlowMap, ProvidesInspector {
implements GridBackedFlowMap, ProvidesInspector {
private static final long serialVersionUID = 1L;
/** Grid containing a flow direction for every location. */
......@@ -48,9 +48,9 @@ abstract class DerivedFlowMap<T extends PathfindingMap> extends AbstractDerivedM
* height of map
*/
public DerivedFlowMap(int width, int height) {
super(width, height);
// no underlying maps yet, initialize all locations to neutral direction
flowMapGrid = new ObjectGrid2D(width, height, NEUTRAL);
super(width, height);
// no underlying maps yet, initialize all locations to neutral direction
flowMapGrid = new ObjectGrid2D(width, height, NEUTRAL);
}
/**
......@@ -61,8 +61,8 @@ abstract class DerivedFlowMap<T extends PathfindingMap> extends AbstractDerivedM
* the initial content
*/
public DerivedFlowMap(Changes<T> content) {
this(content.getWidth(), content.getHeight());
applyChanges(content);
this(content.getWidth(), content.getHeight());
applyChanges(content);
}
/**
......@@ -84,12 +84,12 @@ abstract class DerivedFlowMap<T extends PathfindingMap> extends AbstractDerivedM
*/
@Override
public final ObjectGrid2D getMapGrid() {
return flowMapGrid;
return flowMapGrid;
}
@Override
protected void update(int x, int y) {
getMapGrid().set(x, y, computeDirection(x, y));
getMapGrid().set(x, y, computeDirection(x, y));
}
/**
......@@ -98,17 +98,17 @@ abstract class DerivedFlowMap<T extends PathfindingMap> extends AbstractDerivedM
*/
@Override
public final Double2D obtainDirection(int x, int y) {
updateIfDirty(x, y);
return (Double2D) getMapGrid().get(x, y);
updateIfDirty(x, y);
return (Double2D) getMapGrid().get(x, y);
}
@Override
public Inspector provideInspector(GUIState state, String name) {
FlowMapInspector flowMapInspector = new FlowMapInspector(state, this);
CombinedInspector combinedInspector = new CombinedInspector(flowMapInspector,
Inspector.getInspector(getUnderlyingMaps(), state, name));
combinedInspector.setTitle(toString());
return combinedInspector;
FlowMapInspector flowMapInspector = new FlowMapInspector(state, this);
CombinedInspector combinedInspector = new CombinedInspector(flowMapInspector,
Inspector.getInspector(getUnderlyingMaps(), state, name));
combinedInspector.setTitle(toString());
return combinedInspector;
}
/**
......@@ -119,12 +119,12 @@ abstract class DerivedFlowMap<T extends PathfindingMap> extends AbstractDerivedM
*/
@Override
public FieldPortrayable<ObjectGrid2D> providePortrayable() {
return new FieldPortrayable<ObjectGrid2D>() {
return new FieldPortrayable<ObjectGrid2D>() {
@Override
public ObjectGrid2D getField() {
return getMapGrid();
}
};
@Override
public ObjectGrid2D getField() {
return getMapGrid();
}
};
}