Commit 0d7b6b85 authored by mey's avatar mey
Browse files

DerivedFlowMap.java: added constructor with initial content

    updated FlowFromPotentialsMap and FlowFromFlowMaps for new constructor
    updated test with MapChanger rename
renamed MapChanger to MapContent
parent 1441e7ec
......@@ -67,6 +67,20 @@ public abstract class DerivedFlowMap<T extends PathfindingMap> extends AbstractD
flowMapGrid = new ObjectGrid2D(width, height, NEUTRAL);
}
/**
* Constructs a new {@link DerivedFlowMap} with given map as its first map
* and other content.
*
* @param firstMap
* the first underlying map
* @param otherContent
* the other content
*/
public DerivedFlowMap(T firstMap, MapContent<T> otherContent) {
this(firstMap.getWidth(), firstMap.getHeight());
changeStructure(otherContent);
}
/**
* Adds a map to derive directions from. If the added map is a
* {@link MapChangeNotifier}, a listener is added to the map so that changes
......@@ -77,7 +91,7 @@ public abstract class DerivedFlowMap<T extends PathfindingMap> extends AbstractD
* <p>
* <b>NOTE:</b> This is a structural change and triggers a forced update of
* all locations which is expansive. Use
* {@link #changeStructure(MapChanger)} to chain several structural changes
* {@link #changeStructure(MapContent)} to chain several structural changes
* and trigger the update only once.
*
* @param map
......@@ -93,7 +107,7 @@ public abstract class DerivedFlowMap<T extends PathfindingMap> extends AbstractD
* <p>
* <b>NOTE:</b> This is a structural change and triggers a forced update of
* all locations which is expansive. Use
* {@link #changeStructure(MapChanger)} to chain several structural changes
* {@link #changeStructure(MapContent)} to chain several structural changes
* and trigger the update only once.
* <p>
* <b>NOTE:</b> Each instance of a map can only be associated with one
......@@ -155,7 +169,7 @@ public abstract class DerivedFlowMap<T extends PathfindingMap> extends AbstractD
* <p>
* <b>NOTE:</b> This is a structural change and triggers a forced update of
* all locations which is expansive. Use
* {@link #changeStructure(MapChanger)} to chain several structural changes
* {@link #changeStructure(MapContent)} to chain several structural changes
* and trigger the update only once.
*
* @param name
......@@ -178,7 +192,7 @@ public abstract class DerivedFlowMap<T extends PathfindingMap> extends AbstractD
* <p>
* <b>NOTE:</b> This is a structural change and triggers a forced update of
* all locations which is expansive. Use
* {@link #changeStructure(MapChanger)} to chain several structural changes
* {@link #changeStructure(MapContent)} to chain several structural changes
* and trigger the update only once.
*
* @param map
......@@ -271,22 +285,22 @@ public abstract class DerivedFlowMap<T extends PathfindingMap> extends AbstractD
}
/**
* Makes several structural changes with a {@link MapChanger} and trigger the
* Makes several structural changes with a {@link MapContent} and trigger the
* expensive update only once.
*
* @param changer
* @param content
* @return a {@link Map} with each added pathfinding map linked to its name
* it was associated with
*/
public Map<T, String> changeStructure(MapChanger<T> changer) {
public Map<T, String> changeStructure(MapContent<T> content) {
Map<T, String> names = new HashMap<>();
for (T map : changer.mapsToAdd) {
for (T map : content.mapsToAdd) {
names.put(map, addMapInternal(map));
}
for (T map : changer.mapsToRemove) {
for (T map : content.mapsToRemove) {
removeMapInternal(map);
}
weights.putAll(changer.weightsToPut);
weights.putAll(content.weightsToPut);
forceUpdateAll();
return Collections.unmodifiableMap(names);
......
......@@ -35,19 +35,23 @@ public class FlowFromFlowsMap extends DerivedFlowMap<FlowMap> {
* Constructs a new {@code CombinedFlowMap} with given flow map as its first
* underlying map.
*
* @param underlyingMap
* @param firstMap
* the first underlying map
* @param other
* the other content
*/
public FlowFromFlowsMap(FlowMap underlyingMap) {
super(underlyingMap.getWidth(), underlyingMap.getHeight());
public FlowFromFlowsMap(FlowMap firstMap, MapContent<FlowMap> other) {
super(firstMap.getWidth(), firstMap.getHeight());
// speedup if grid-backed
if (underlyingMap instanceof GridBackedFlowMap) {
addMapInternal(underlyingMap);
getMapGrid().setTo(((GridBackedFlowMap) underlyingMap).getMapGrid());
if (firstMap instanceof GridBackedFlowMap) {
addMapInternal(firstMap);
getMapGrid().setTo(((GridBackedFlowMap) firstMap).getMapGrid());
} else {
addMap(underlyingMap);
addMap(firstMap);
}
changeStructure(other);
}
/**
......
......@@ -39,12 +39,25 @@ public class FlowFromPotentialsMap extends DerivedFlowMap<PotentialMap> {
* Constructs a new {@code FlowFromPotentialMap} with given potential map as
* its first underlying map.
*
* @param underlyingMap
* {@link PotentialMap} to derive directions from.
* @param firstMap
* the first underlying map
*/
public FlowFromPotentialsMap(PotentialMap underlyingMap) {
this(underlyingMap.getWidth(), underlyingMap.getHeight());
addMap(underlyingMap);
public FlowFromPotentialsMap(PotentialMap firstMap) {
this(firstMap.getWidth(), firstMap.getHeight());
addMap(firstMap);
}
/**
* Constructs a new {@link FlowFromPotentialsMap} with given map as its
* first map and other content.
*
* @param firstMap
* the first underlying map
* @param otherContent
* the other content
*/
public FlowFromPotentialsMap(PotentialMap firstMap, MapContent<PotentialMap> otherContent) {
super(firstMap, otherContent);
}
/**
......
......@@ -9,13 +9,13 @@ import java.util.Map;
* Builder-like class to make structural changes to a {@link DerivedFlowMap}
* and trigger the expensive update only once.
*
* @see DerivedFlowMap#changeStructure(MapChanger)
* @see DerivedFlowMap#changeStructure(MapContent)
* @author mey
* @param <T>
* the type of pathfinding maps used
*
*/
public class MapChanger<T extends PathfindingMap> {
public class MapContent<T extends PathfindingMap> {
final Collection<T> mapsToAdd = new ArrayList<>();
final Map<T, Double> weightsToPut = new HashMap<>();
final Collection<T> mapsToRemove = new ArrayList<>();
......@@ -27,7 +27,7 @@ public class MapChanger<T extends PathfindingMap> {
* the map to add
* @return this object
*/
public MapChanger<T> addMap(T map) {
public MapContent<T> addMap(T map) {
return addMap(map, DerivedFlowMap.NEUTRAL_WEIGHT);
}
......@@ -40,7 +40,7 @@ public class MapChanger<T extends PathfindingMap> {
* the weight to associate it with
* @return this object
*/
public MapChanger<T> addMap(T map, double weight) {
public MapContent<T> addMap(T map, double weight) {
weightsToPut.put(map, weight);
mapsToAdd.add(map);
return this;
......@@ -53,7 +53,7 @@ public class MapChanger<T extends PathfindingMap> {
* the map to be removed
* @return this object
*/
public MapChanger<T> removeMap(T map) {
public MapContent<T> removeMap(T map) {
mapsToRemove.add(map);
return this;
}
......@@ -65,7 +65,7 @@ public class MapChanger<T extends PathfindingMap> {
* @param weight
* @return this object
*/
public MapChanger<T> setWeight(T map, double weight) {
public MapContent<T> setWeight(T map, double weight) {
weightsToPut.put(map, weight);
return this;
}
......
......@@ -120,13 +120,13 @@ public class DerivedFlowMapTest {
@Test
public void changeStructure() {
Map<PathfindingMap, String> names = map.changeStructure(new MapChanger<>().addMap(dynamicMap, WEIGHT_VALUE));
Map<PathfindingMap, String> names = map.changeStructure(new MapContent<>().addMap(dynamicMap, WEIGHT_VALUE));
assertThat(map.getUnderlyingMaps(), contains((PathfindingMap) dynamicMap));
assertThat(map.getWeight(dynamicMap), is(WEIGHT_VALUE));
assertThat(names.keySet(), contains((PathfindingMap) dynamicMap));
assertTrue(map.wasComputeDirectionCalled());
map.changeStructure(new MapChanger<>().removeMap(dynamicMap));
map.changeStructure(new MapContent<>().removeMap(dynamicMap));
assertThat(map.getUnderlyingMaps(), is(empty()));
assertTrue(map.wasComputeDirectionCalled());
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment