From: <aki...@us...> - 2011-10-19 06:39:05
|
Revision: 9067 http://gridarta.svn.sourceforge.net/gridarta/?rev=9067&view=rev Author: akirschbaum Date: 2011-10-19 06:38:58 +0000 (Wed, 19 Oct 2011) Log Message: ----------- Merge duplicated code. Modified Paths: -------------- trunk/model/src/app/net/sf/gridarta/model/exitconnector/ExitMatcher.java trunk/model/src/app/net/sf/gridarta/model/mapmodel/DefaultMapModel.java trunk/model/src/app/net/sf/gridarta/model/mapmodel/MapSquareIterator.java trunk/model/src/app/net/sf/gridarta/model/mapmodel/TopLevelGameObjectIterator.java trunk/model/src/test/net/sf/gridarta/model/mapmodel/MapSquareIteratorTest.java trunk/model/src/test/net/sf/gridarta/model/mapmodel/TopLevelGameObjectIteratorTest.java trunk/src/app/net/sf/gridarta/gui/map/mapactions/MapActions.java Modified: trunk/model/src/app/net/sf/gridarta/model/exitconnector/ExitMatcher.java =================================================================== --- trunk/model/src/app/net/sf/gridarta/model/exitconnector/ExitMatcher.java 2011-10-19 06:14:11 UTC (rev 9066) +++ trunk/model/src/app/net/sf/gridarta/model/exitconnector/ExitMatcher.java 2011-10-19 06:38:58 UTC (rev 9067) @@ -33,9 +33,14 @@ * Selects valid exit game objects from maps. * @author Andreas Kirschbaum */ -public class ExitMatcher<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> { +public class ExitMatcher<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements GameObjectMatcher { /** + * The serial version UID. + */ + private static final long serialVersionUID = 1L; + + /** * The matcher for selecting exit objects. */ @NotNull @@ -144,8 +149,16 @@ * @param gameObject the exit game object * @return whether the game object has "slaying" set */ - private boolean isExit(@NotNull final GameObject<G, A, R> gameObject) { + private boolean isExit(@NotNull final GameObject<?, ?, ?> gameObject) { return exitMatcher.isMatching(gameObject); } + /** + * {@inheritDoc} + */ + @Override + public boolean isMatching(@NotNull final GameObject<?, ?, ?> gameObject) { + return isExit(gameObject); + } + } // class ExitMatcher Modified: trunk/model/src/app/net/sf/gridarta/model/mapmodel/DefaultMapModel.java =================================================================== --- trunk/model/src/app/net/sf/gridarta/model/mapmodel/DefaultMapModel.java 2011-10-19 06:14:11 UTC (rev 9066) +++ trunk/model/src/app/net/sf/gridarta/model/mapmodel/DefaultMapModel.java 2011-10-19 06:38:58 UTC (rev 9067) @@ -263,7 +263,7 @@ */ @Override public Iterator<MapSquare<G, A, R>> iterator() { - return new MapSquareIterator<G, A, R>(this, null, 1); + return new MapSquareIterator<G, A, R>(this, null, 1, false); } /** Modified: trunk/model/src/app/net/sf/gridarta/model/mapmodel/MapSquareIterator.java =================================================================== --- trunk/model/src/app/net/sf/gridarta/model/mapmodel/MapSquareIterator.java 2011-10-19 06:14:11 UTC (rev 9066) +++ trunk/model/src/app/net/sf/gridarta/model/mapmodel/MapSquareIterator.java 2011-10-19 06:38:58 UTC (rev 9067) @@ -76,8 +76,10 @@ * @param start the starting point or <code>null</code> for default * @param direction the direction to iterate: <code>+1</code> for forward, * <code>-1</code> for backward + * @param skipFirst whether to skip the first map square and return it at + * the end */ - public MapSquareIterator(@NotNull final MapModel<G, A, R> mapModel, @Nullable final Point start, final int direction) { + public MapSquareIterator(@NotNull final MapModel<G, A, R> mapModel, @Nullable final Point start, final int direction, final boolean skipFirst) { if (direction != -1 && direction != +1) { throw new IllegalArgumentException(); } @@ -94,6 +96,9 @@ point = mapWidth * mapHeight - 1; } remainingMapSquares = mapWidth * mapHeight; + if (skipFirst) { + nextMapSquare(); + } } /** @@ -123,13 +128,20 @@ remainingMapSquares--; final MapSquare<G, A, R> square = mapModel.getMapSquare(new Point(point % mapWidth, point / mapWidth)); + nextMapSquare(); + return square; + } + + /** + * Updates {@link #point} to the next map square. + */ + private void nextMapSquare() { point += direction; if (point < 0) { point += mapWidth * mapHeight; } else if (point >= mapWidth * mapHeight) { point -= mapWidth * mapHeight; } - return square; } } // class MapSquareIterator Modified: trunk/model/src/app/net/sf/gridarta/model/mapmodel/TopLevelGameObjectIterator.java =================================================================== --- trunk/model/src/app/net/sf/gridarta/model/mapmodel/TopLevelGameObjectIterator.java 2011-10-19 06:14:11 UTC (rev 9066) +++ trunk/model/src/app/net/sf/gridarta/model/mapmodel/TopLevelGameObjectIterator.java 2011-10-19 06:38:58 UTC (rev 9067) @@ -61,9 +61,11 @@ * @param start the starting point or <code>null</code> for default * @param direction the direction to iterate: <code>+1</code> for forward, * <code>-1</code> for backward + * @param skipFirst whether to skip the first map square and return it at + * the end */ - public TopLevelGameObjectIterator(@NotNull final MapModel<G, A, R> mapModel, @Nullable final Point start, final int direction) { - mapSquareIterator = new MapSquareIterator<G, A, R>(mapModel, start, direction); + public TopLevelGameObjectIterator(@NotNull final MapModel<G, A, R> mapModel, @Nullable final Point start, final int direction, final boolean skipFirst) { + mapSquareIterator = new MapSquareIterator<G, A, R>(mapModel, start, direction, skipFirst); findNext(); } Modified: trunk/model/src/test/net/sf/gridarta/model/mapmodel/MapSquareIteratorTest.java =================================================================== --- trunk/model/src/test/net/sf/gridarta/model/mapmodel/MapSquareIteratorTest.java 2011-10-19 06:14:11 UTC (rev 9066) +++ trunk/model/src/test/net/sf/gridarta/model/mapmodel/MapSquareIteratorTest.java 2011-10-19 06:38:58 UTC (rev 9067) @@ -40,7 +40,7 @@ public void testIteratorForward() { final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false); final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 2); - final Iterator<MapSquare<TestGameObject, TestMapArchObject, TestArchetype>> it = new MapSquareIterator<TestGameObject, TestMapArchObject, TestArchetype>(mapModel, null, +1); + final Iterator<MapSquare<TestGameObject, TestMapArchObject, TestArchetype>> it = new MapSquareIterator<TestGameObject, TestMapArchObject, TestArchetype>(mapModel, null, +1, false); Assert.assertTrue(it.hasNext()); Assert.assertSame(mapModel.getMapSquare(new Point(0, 0)), it.next()); Assert.assertTrue(it.hasNext()); @@ -63,7 +63,7 @@ public void testIteratorBackward() { final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false); final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 2); - final Iterator<MapSquare<TestGameObject, TestMapArchObject, TestArchetype>> it = new MapSquareIterator<TestGameObject, TestMapArchObject, TestArchetype>(mapModel, null, -1); + final Iterator<MapSquare<TestGameObject, TestMapArchObject, TestArchetype>> it = new MapSquareIterator<TestGameObject, TestMapArchObject, TestArchetype>(mapModel, null, -1, false); Assert.assertTrue(it.hasNext()); Assert.assertSame(mapModel.getMapSquare(new Point(2, 1)), it.next()); Assert.assertTrue(it.hasNext()); @@ -86,7 +86,7 @@ public void testIteratorForwardStart() { final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false); final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 2); - final Iterator<MapSquare<TestGameObject, TestMapArchObject, TestArchetype>> it = new MapSquareIterator<TestGameObject, TestMapArchObject, TestArchetype>(mapModel, new Point(0, 1), +1); + final Iterator<MapSquare<TestGameObject, TestMapArchObject, TestArchetype>> it = new MapSquareIterator<TestGameObject, TestMapArchObject, TestArchetype>(mapModel, new Point(0, 1), +1, false); Assert.assertTrue(it.hasNext()); Assert.assertSame(mapModel.getMapSquare(new Point(0, 1)), it.next()); Assert.assertTrue(it.hasNext()); @@ -109,7 +109,7 @@ public void testIteratorBackwardStart() { final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false); final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 2); - final Iterator<MapSquare<TestGameObject, TestMapArchObject, TestArchetype>> it = new MapSquareIterator<TestGameObject, TestMapArchObject, TestArchetype>(mapModel, new Point(0, 1), -1); + final Iterator<MapSquare<TestGameObject, TestMapArchObject, TestArchetype>> it = new MapSquareIterator<TestGameObject, TestMapArchObject, TestArchetype>(mapModel, new Point(0, 1), -1, false); Assert.assertTrue(it.hasNext()); Assert.assertSame(mapModel.getMapSquare(new Point(0, 1)), it.next()); Assert.assertTrue(it.hasNext()); @@ -126,13 +126,61 @@ } /** + * Checks that the forward iterator returns all map squares when skip is + * enabled. + */ + @Test + public void testIteratorForwardStartSkip() { + final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false); + final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 2); + final Iterator<MapSquare<TestGameObject, TestMapArchObject, TestArchetype>> it = new MapSquareIterator<TestGameObject, TestMapArchObject, TestArchetype>(mapModel, new Point(0, 1), +1, true); + Assert.assertTrue(it.hasNext()); + Assert.assertSame(mapModel.getMapSquare(new Point(1, 1)), it.next()); + Assert.assertTrue(it.hasNext()); + Assert.assertSame(mapModel.getMapSquare(new Point(2, 1)), it.next()); + Assert.assertTrue(it.hasNext()); + Assert.assertSame(mapModel.getMapSquare(new Point(0, 0)), it.next()); + Assert.assertTrue(it.hasNext()); + Assert.assertSame(mapModel.getMapSquare(new Point(1, 0)), it.next()); + Assert.assertTrue(it.hasNext()); + Assert.assertSame(mapModel.getMapSquare(new Point(2, 0)), it.next()); + Assert.assertTrue(it.hasNext()); + Assert.assertSame(mapModel.getMapSquare(new Point(0, 1)), it.next()); + Assert.assertFalse(it.hasNext()); + } + + /** + * Checks that the backward iterator returns all map squares when skip is + * enabled. + */ + @Test + public void testIteratorBackwardStartSkip() { + final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false); + final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 2); + final Iterator<MapSquare<TestGameObject, TestMapArchObject, TestArchetype>> it = new MapSquareIterator<TestGameObject, TestMapArchObject, TestArchetype>(mapModel, new Point(0, 1), -1, true); + Assert.assertTrue(it.hasNext()); + Assert.assertSame(mapModel.getMapSquare(new Point(2, 0)), it.next()); + Assert.assertTrue(it.hasNext()); + Assert.assertSame(mapModel.getMapSquare(new Point(1, 0)), it.next()); + Assert.assertTrue(it.hasNext()); + Assert.assertSame(mapModel.getMapSquare(new Point(0, 0)), it.next()); + Assert.assertTrue(it.hasNext()); + Assert.assertSame(mapModel.getMapSquare(new Point(2, 1)), it.next()); + Assert.assertTrue(it.hasNext()); + Assert.assertSame(mapModel.getMapSquare(new Point(1, 1)), it.next()); + Assert.assertTrue(it.hasNext()); + Assert.assertSame(mapModel.getMapSquare(new Point(0, 1)), it.next()); + Assert.assertFalse(it.hasNext()); + } + + /** * Checks that invalid directions are rejected. */ @Test(expected = IllegalArgumentException.class) public void testIteratorDirection0() { final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false); final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 2); - final Iterator<MapSquare<TestGameObject, TestMapArchObject, TestArchetype>> it = new MapSquareIterator<TestGameObject, TestMapArchObject, TestArchetype>(mapModel, null, 0); + final Iterator<MapSquare<TestGameObject, TestMapArchObject, TestArchetype>> it = new MapSquareIterator<TestGameObject, TestMapArchObject, TestArchetype>(mapModel, null, 0, false); } /** @@ -142,7 +190,7 @@ public void testIteratorDirection2() { final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false); final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 2); - final Iterator<MapSquare<TestGameObject, TestMapArchObject, TestArchetype>> it = new MapSquareIterator<TestGameObject, TestMapArchObject, TestArchetype>(mapModel, null, 2); + final Iterator<MapSquare<TestGameObject, TestMapArchObject, TestArchetype>> it = new MapSquareIterator<TestGameObject, TestMapArchObject, TestArchetype>(mapModel, null, 2, false); } } Modified: trunk/model/src/test/net/sf/gridarta/model/mapmodel/TopLevelGameObjectIteratorTest.java =================================================================== --- trunk/model/src/test/net/sf/gridarta/model/mapmodel/TopLevelGameObjectIteratorTest.java 2011-10-19 06:14:11 UTC (rev 9066) +++ trunk/model/src/test/net/sf/gridarta/model/mapmodel/TopLevelGameObjectIteratorTest.java 2011-10-19 06:38:58 UTC (rev 9067) @@ -41,7 +41,7 @@ public void testIteratorForwardEmpty() { final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false); final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 2); - final Iterator<TestGameObject> it = new TopLevelGameObjectIterator<TestGameObject, TestMapArchObject, TestArchetype>(mapModel, new Point(1, 1), +1); + final Iterator<TestGameObject> it = new TopLevelGameObjectIterator<TestGameObject, TestMapArchObject, TestArchetype>(mapModel, new Point(1, 1), +1, false); Assert.assertFalse(it.hasNext()); } @@ -52,7 +52,7 @@ public void testIteratorBackwardEmpty() { final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false); final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 2); - final Iterator<TestGameObject> it = new TopLevelGameObjectIterator<TestGameObject, TestMapArchObject, TestArchetype>(mapModel, new Point(1, 1), -1); + final Iterator<TestGameObject> it = new TopLevelGameObjectIterator<TestGameObject, TestMapArchObject, TestArchetype>(mapModel, new Point(1, 1), -1, false); Assert.assertFalse(it.hasNext()); } @@ -62,7 +62,7 @@ @Test public void testIteratorForward() { final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = createMap(); - final Iterator<TestGameObject> it = new TopLevelGameObjectIterator<TestGameObject, TestMapArchObject, TestArchetype>(mapModel, new Point(1, 1), +1); + final Iterator<TestGameObject> it = new TopLevelGameObjectIterator<TestGameObject, TestMapArchObject, TestArchetype>(mapModel, new Point(1, 1), +1, false); Assert.assertTrue(it.hasNext()); Assert.assertEquals("n4", it.next().getBestName()); Assert.assertTrue(it.hasNext()); @@ -82,7 +82,7 @@ @Test public void testIteratorBackward() { final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = createMap(); - final Iterator<TestGameObject> it = new TopLevelGameObjectIterator<TestGameObject, TestMapArchObject, TestArchetype>(mapModel, new Point(1, 1), -1); + final Iterator<TestGameObject> it = new TopLevelGameObjectIterator<TestGameObject, TestMapArchObject, TestArchetype>(mapModel, new Point(1, 1), -1, false); Assert.assertTrue(it.hasNext()); Assert.assertEquals("n4", it.next().getBestName()); Assert.assertTrue(it.hasNext()); @@ -97,6 +97,46 @@ } /** + * Checks the forward iterator with skipped first element. + */ + @Test + public void testIteratorForwardSkip() { + final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = createMap(); + final Iterator<TestGameObject> it = new TopLevelGameObjectIterator<TestGameObject, TestMapArchObject, TestArchetype>(mapModel, new Point(1, 1), +1, true); + Assert.assertTrue(it.hasNext()); + Assert.assertEquals("n5", it.next().getBestName()); + Assert.assertTrue(it.hasNext()); + Assert.assertEquals("n1", it.next().getBestName()); + Assert.assertTrue(it.hasNext()); + Assert.assertEquals("n2", it.next().getBestName()); + Assert.assertTrue(it.hasNext()); + Assert.assertEquals("n3", it.next().getBestName()); + Assert.assertTrue(it.hasNext()); + Assert.assertEquals("n4", it.next().getBestName()); + Assert.assertFalse(it.hasNext()); + } + + /** + * Checks the backward iterator with skipped first element. + */ + @Test + public void testIteratorBackwardSkip() { + final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = createMap(); + final Iterator<TestGameObject> it = new TopLevelGameObjectIterator<TestGameObject, TestMapArchObject, TestArchetype>(mapModel, new Point(1, 1), -1, true); + Assert.assertTrue(it.hasNext()); + Assert.assertEquals("n3", it.next().getBestName()); + Assert.assertTrue(it.hasNext()); + Assert.assertEquals("n1", it.next().getBestName()); + Assert.assertTrue(it.hasNext()); + Assert.assertEquals("n2", it.next().getBestName()); + Assert.assertTrue(it.hasNext()); + Assert.assertEquals("n5", it.next().getBestName()); + Assert.assertTrue(it.hasNext()); + Assert.assertEquals("n4", it.next().getBestName()); + Assert.assertFalse(it.hasNext()); + } + + /** * Creates a new {@link MapModel} instance filled with game objects. * @return the new map model instance */ Modified: trunk/src/app/net/sf/gridarta/gui/map/mapactions/MapActions.java =================================================================== --- trunk/src/app/net/sf/gridarta/gui/map/mapactions/MapActions.java 2011-10-19 06:14:11 UTC (rev 9066) +++ trunk/src/app/net/sf/gridarta/gui/map/mapactions/MapActions.java 2011-10-19 06:38:58 UTC (rev 9067) @@ -23,6 +23,7 @@ import java.io.File; import java.util.ArrayList; import java.util.Collection; +import java.util.Iterator; import javax.swing.Action; import javax.swing.JFrame; import javax.swing.filechooser.FileFilter; @@ -44,8 +45,10 @@ import net.sf.gridarta.model.mapcursor.MapCursorListener; import net.sf.gridarta.model.mapmanager.MapManager; import net.sf.gridarta.model.mapmanager.MapManagerListener; +import net.sf.gridarta.model.mapmodel.FilterGameObjectIterator; import net.sf.gridarta.model.mapmodel.MapModel; import net.sf.gridarta.model.mapmodel.MapSquare; +import net.sf.gridarta.model.mapmodel.TopLevelGameObjectIterator; import net.sf.gridarta.model.mapviewsettings.MapViewSettings; import net.sf.gridarta.model.mapviewsettings.MapViewSettingsListener; import net.sf.gridarta.utils.ActionUtils; @@ -571,13 +574,11 @@ /** * Selects an exit square. * @param mapView the map view to operate on - * @param xStart the x-coordinate to start searching - * @param yStart the y-coordinate to start searching * @param direction the direction to search */ - private void selectExit(@NotNull final MapView<G, A, R> mapView, final int xStart, final int yStart, final int direction) { - final MapModel<G, A, R> mapModel = mapView.getMapControl().getMapModel(); - final ExitIterator<G, A, R> exitIterator = new ExitIterator<G, A, R>(exitMatcher, mapModel, xStart, yStart, direction); + private void selectExit(@NotNull final MapView<G, A, R> mapView, final int direction) { + final TopLevelGameObjectIterator<G, A, R> gameObjectIterator = new TopLevelGameObjectIterator<G, A, R>(mapView.getMapControl().getMapModel(), mapView.getMapCursor().getLocation(), direction, true); + final Iterator<G> exitIterator = new FilterGameObjectIterator<G, A, R>(gameObjectIterator, exitMatcher); if (exitIterator.hasNext()) { final G exit = exitIterator.next(); final MapSquare<G, A, R> mapSquare = exit.getMapSquare(); @@ -852,12 +853,7 @@ } if (performAction) { - final Point cursorLocation = mapView.getMapCursor().getLocation(); - if (cursorLocation == null) { - selectExit(mapView, -1, 0, 1); - } else { - selectExit(mapView, cursorLocation.x, cursorLocation.y, 1); - } + selectExit(mapView, 1); } return true; @@ -875,13 +871,7 @@ } if (performAction) { - final Point cursorLocation = mapView.getMapCursor().getLocation(); - if (cursorLocation == null) { - final Size2D mapSize = mapView.getMapControl().getMapModel().getMapArchObject().getMapSize(); - selectExit(mapView, mapSize.getWidth(), mapSize.getHeight() - 1, -1); - } else { - selectExit(mapView, cursorLocation.x, cursorLocation.y, -1); - } + selectExit(mapView, -1); } return true; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |