diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/test/CourseTest.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/test/CourseTest.java index 9cf27561c61..5bb04aa475f 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/test/CourseTest.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/test/CourseTest.java @@ -1,8 +1,10 @@ package com.sap.sailing.domain.test; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -80,4 +82,46 @@ public class CourseTest { assertEquals(0, Util.size(course.getWaypoints())); assertEquals(0, Util.size(course.getLegs())); } + + @Test + public void testInsertWaypointToCourseWithTwoWaypoints() { + List waypoints = new ArrayList(); + final WaypointImpl wp1 = new WaypointImpl(new BuoyImpl("Test Buoy 1")); + waypoints.add(wp1); + final WaypointImpl wp2 = new WaypointImpl(new BuoyImpl("Test Buoy 2")); + waypoints.add(wp2); + Course course = new CourseImpl("Test Course", waypoints); + assertEquals(2, Util.size(course.getWaypoints())); + assertEquals(1, Util.size(course.getLegs())); + final WaypointImpl wp1_5 = new WaypointImpl(new BuoyImpl("Test Buoy 1.5")); + course.addWaypoint(1, wp1_5); + assertEquals(3, Util.size(course.getWaypoints())); + assertEquals(2, Util.size(course.getLegs())); + assertTrue(Util.equals(Arrays.asList(new Waypoint[] { wp1, wp1_5, wp2 }), course.getWaypoints())); + assertEquals(0, course.getIndexOfWaypoint(wp1)); + assertEquals(1, course.getIndexOfWaypoint(wp1_5)); + assertEquals(2, course.getIndexOfWaypoint(wp2)); + } + + @Test + public void testRemovetWaypointFromCourseWithThreeWaypoints() { + List waypoints = new ArrayList(); + final WaypointImpl wp1 = new WaypointImpl(new BuoyImpl("Test Buoy 1")); + waypoints.add(wp1); + final WaypointImpl wp2 = new WaypointImpl(new BuoyImpl("Test Buoy 2")); + waypoints.add(wp2); + final WaypointImpl wp3 = new WaypointImpl(new BuoyImpl("Test Buoy 3")); + waypoints.add(wp3); + Course course = new CourseImpl("Test Course", waypoints); + assertEquals(3, Util.size(course.getWaypoints())); + assertEquals(2, Util.size(course.getLegs())); + course.removeWaypoint(1); + assertEquals(2, Util.size(course.getWaypoints())); + assertEquals(1, Util.size(course.getLegs())); + assertTrue(Util.equals(Arrays.asList(new Waypoint[] { wp1, wp3 }), course.getWaypoints())); + assertEquals(0, course.getIndexOfWaypoint(wp1)); + assertEquals(-1, course.getIndexOfWaypoint(wp2)); + assertEquals(1, course.getIndexOfWaypoint(wp3)); + } + } diff --git a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/base/impl/CourseImpl.java b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/base/impl/CourseImpl.java index a46ed7dc970..2724ce1f4aa 100755 --- a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/base/impl/CourseImpl.java +++ b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/base/impl/CourseImpl.java @@ -87,6 +87,14 @@ public class CourseImpl extends NamedImpl implements Course { @Override public synchronized void addWaypoint(int zeroBasedPosition, Waypoint waypointToAdd) { waypoints.add(zeroBasedPosition, waypointToAdd); + Map updatesToWaypointIndexes = new HashMap(); + updatesToWaypointIndexes.put(waypointToAdd, zeroBasedPosition); + for (Map.Entry e : waypointIndexes.entrySet()) { + if (e.getValue() >= zeroBasedPosition) { + updatesToWaypointIndexes.put(e.getKey(), e.getValue()+1); + } + } + waypointIndexes.putAll(updatesToWaypointIndexes); int legStartWaypointIndex; if (zeroBasedPosition == waypoints.size()-1) { // added to end legStartWaypointIndex = zeroBasedPosition-1; @@ -104,6 +112,14 @@ public class CourseImpl extends NamedImpl implements Course { if (zeroBasedPosition >= 0) { boolean isLast = zeroBasedPosition == waypoints.size()-1; Waypoint removedWaypoint = waypoints.remove(zeroBasedPosition); + waypointIndexes.remove(removedWaypoint); + Map updatesToWaypointIndexes = new HashMap(); + for (Map.Entry e : waypointIndexes.entrySet()) { + if (e.getValue() > zeroBasedPosition) { // only > because the entry with == was just removed + updatesToWaypointIndexes.put(e.getKey(), e.getValue()-1); + } + } + waypointIndexes.putAll(updatesToWaypointIndexes); if (isLast) { if (waypoints.size() > 0) { // if we had only one waypoint, we didn't have any legs // last waypoint was removed; remove last leg @@ -167,7 +183,7 @@ public class CourseImpl extends NamedImpl implements Course { } @Override - public int getIndexOfWaypoint(Waypoint waypoint) { + public synchronized int getIndexOfWaypoint(Waypoint waypoint) { int result = -1; Integer indexEntry = waypointIndexes.get(waypoint); if (indexEntry != null) {