Skip to content

Commit

Permalink
remove access to int SHAPE_POINT_n, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bobjacobsen committed Apr 25, 2020
1 parent ccfc12e commit 0fa8c08
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 20 deletions.
69 changes: 64 additions & 5 deletions java/src/jmri/jmrit/display/layoutEditor/LayoutEditor.java
Expand Up @@ -380,6 +380,10 @@ protected static boolean isPopupHitType(HitPointType hitType) {
return result;
} // isPopupHitType

// *****************************************************************
// Turntable Ray support
// *****************************************************************

/**
* Find the 0-63 index with respect to TURNTABLE_RAY_0
* of a given enum entry. Throws {@link IllegalArgumentException} if
Expand All @@ -391,7 +395,8 @@ protected static boolean isPopupHitType(HitPointType hitType) {
*/
protected int turntableTrackIndex() {
int result = this.xmlValue - HitPointType.TURNTABLE_RAY_0.xmlValue;
if (result < 0) throw new IllegalArgumentException(this.toString()+ "is not a valid TURNTABLE_RAY");
if (result < 0) throw new IllegalArgumentException(this.toString()+ "is not a valid TURNTABLE_RAY");
if (result > 63) throw new IllegalArgumentException(this.toString()+ "is not a valid TURNTABLE_RAY");
return result;
}

Expand Down Expand Up @@ -427,6 +432,60 @@ protected static HitPointType[] turntableValues() {
TURNTABLE_RAY_56, TURNTABLE_RAY_57, TURNTABLE_RAY_58, TURNTABLE_RAY_59, TURNTABLE_RAY_60, TURNTABLE_RAY_61, TURNTABLE_RAY_62, TURNTABLE_RAY_63
};
}

// *****************************************************************
// Shape Point support
// *****************************************************************

/**
* Find the 0-9 index with respect to SHAPE_POINT_0
* of a given enum entry. Throws {@link IllegalArgumentException} if
* the given enum value isn't one of the SHAPE_POINT_0 entries.
* <p>
* Ideally, this would be replaced by shape code that works
* directly with the enum values as a step toward using objects
* to implement hit points.
*/
protected int shapePointIndex() {
int result = this.xmlValue - HitPointType.SHAPE_POINT_0.xmlValue;
if (result < 0) throw new IllegalArgumentException(this.toString()+ "is not a valid SHAPE_POINT");
if (result > 9) throw new IllegalArgumentException(this.toString()+ "is not a valid SHAPE_POINT");
return result;
}

/**
* Return a specific SHAPE_POINT from its 0-9 index.
* Throws {@link IllegalArgumentException} if
* the given index value isn't valid for the SHAPE_POINT entries.
* <p>
* Ideally, this would be replaced by shape code that works
* directly with the enum values as a step toward using objects
* to implement hit points.
*/
protected static HitPointType shapePointIndexedValue(int i) {
if (i<0 || i>9 ) throw new IllegalArgumentException(i+ "is not a valid SHAPE_POINT index");
return getValue(SHAPE_POINT_0.xmlValue+i);
}


/**
* Return an array of the valid SHAPE_POINT enum values.
* Meant for interations over the set of points. Order is
* from 0 to 9.
*/
protected static HitPointType[] shapePointValues() {
return new HitPointType[]{
SHAPE_POINT_0, SHAPE_POINT_1, SHAPE_POINT_2, SHAPE_POINT_3, SHAPE_POINT_4, SHAPE_POINT_5, SHAPE_POINT_6, SHAPE_POINT_7, SHAPE_POINT_8, SHAPE_POINT_9
};
}

protected static boolean isShapePointOffsetHitPointType(LayoutEditor.HitPointType t) {
return ((t.compareTo(SHAPE_POINT_0) >= 0)
&& (t.compareTo(SHAPE_POINT_9) <= 0));
}



}


Expand Down Expand Up @@ -3269,7 +3328,7 @@ public void mousePressed(MouseEvent event) {
selectedObject = null; // assume we're adding...
for (LayoutShape ls : layoutShapes) {
selectedHitPointType = ls.findHitPointType(dLoc, true);
if (LayoutShape.isShapePointOffsetHitPointType(selectedHitPointType)) {
if (LayoutEditor.HitPointType.isShapePointOffsetHitPointType(selectedHitPointType)) {
//log.warn("extend selectedObject: ", lt);
selectedObject = ls; // nope, we're extending
beginLocation.setLocation(dLoc);
Expand Down Expand Up @@ -3675,7 +3734,7 @@ public void mouseReleased(MouseEvent event) {
_targetPanel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
} else {
LayoutShape ls = (LayoutShape) selectedObject;
ls.addPoint(currentPoint, selectedHitPointType.getXmlValue() - HitPointType.SHAPE_POINT_0.getXmlValue());
ls.addPoint(currentPoint, selectedHitPointType.shapePointIndex());
}
} else if (leToolBarPanel.signalMastButton.isSelected()) {
addSignalMast();
Expand Down Expand Up @@ -4999,8 +5058,8 @@ public void mouseDragged(@Nonnull MouseEvent event) {
((TrackSegment) selectedObject).setBezierControlPoint(currentPoint, index);
} else if ((selectedHitPointType == HitPointType.SHAPE_CENTER)) {
((LayoutShape) selectedObject).setCoordsCenter(currentPoint);
} else if (LayoutShape.isShapePointOffsetHitPointType(selectedHitPointType)) {
int index = selectedHitPointType.getXmlValue() - HitPointType.SHAPE_POINT_0.getXmlValue();
} else if (LayoutEditor.HitPointType.isShapePointOffsetHitPointType(selectedHitPointType)) {
int index = selectedHitPointType.shapePointIndex();
((LayoutShape) selectedObject).setPoint(index, currentPoint);
} else if (HitPointType.isTurntableRayHitType(selectedHitPointType)) {
LayoutTurntable turn = (LayoutTurntable) selectedObject;
Expand Down
15 changes: 5 additions & 10 deletions java/src/jmri/jmrit/display/layoutEditor/LayoutShape.java
Expand Up @@ -292,7 +292,7 @@ public int getNumberPoints() {
* @return the maximum number of points
*/
public int getMaxNumberPoints() {
return LayoutEditor.HitPointType.SHAPE_POINT_9.getXmlValue() - LayoutEditor.HitPointType.SHAPE_POINT_0.getXmlValue() + 1;
return LayoutEditor.HitPointType.shapePointValues().length;
}

/**
Expand Down Expand Up @@ -330,7 +330,7 @@ protected LayoutEditor.HitPointType findHitPointType(@Nonnull Point2D hitPoint,
}
for (int idx = 0; idx < shapePoints.size(); idx++) {
if (r.contains(shapePoints.get(idx).getPoint())) {
result = LayoutEditor.HitPointType.getValue(LayoutEditor.HitPointType.SHAPE_POINT_0.getXmlValue() + idx);
result = LayoutEditor.HitPointType.shapePointIndexedValue(idx);
break;
}
}
Expand All @@ -340,7 +340,7 @@ protected LayoutEditor.HitPointType findHitPointType(@Nonnull Point2D hitPoint,
distance = MathUtil.distance(shapePoints.get(idx).getPoint(), hitPoint);
if (distance < minDistance) {
minDistance = distance;
result = LayoutEditor.HitPointType.getValue(LayoutEditor.HitPointType.SHAPE_POINT_0.getXmlValue() + idx);
result = LayoutEditor.HitPointType.shapePointIndexedValue(idx);
}
}
}
Expand All @@ -349,12 +349,7 @@ protected LayoutEditor.HitPointType findHitPointType(@Nonnull Point2D hitPoint,

public static boolean isShapeHitPointType(LayoutEditor.HitPointType t) {
return ((t == LayoutEditor.HitPointType.SHAPE_CENTER)
|| isShapePointOffsetHitPointType(t));
}

public static boolean isShapePointOffsetHitPointType(LayoutEditor.HitPointType t) {
return ((t.compareTo(LayoutEditor.HitPointType.SHAPE_POINT_0) >= 0)
&& (t.compareTo(LayoutEditor.HitPointType.SHAPE_POINT_9) <= 0));
|| LayoutEditor.HitPointType.isShapePointOffsetHitPointType(t));
}

/**
Expand Down Expand Up @@ -428,7 +423,7 @@ protected JPopupMenu showShapePopUp(@CheckForNull MouseEvent mouseEvent, LayoutE
popup = new JPopupMenu();
}
if (layoutEditor.isEditable()) {
int pointIndex = hitPointType.getXmlValue() - LayoutEditor.HitPointType.SHAPE_POINT_0.getXmlValue();
int pointIndex = hitPointType.shapePointIndex();

//JMenuItem jmi = popup.add(Bundle.getMessage("MakeLabel", Bundle.getMessage("LayoutShape")) + getName());
JMenuItem jmi = popup.add(Bundle.getMessage("ShapeNameMenuItemTitle", getName()));
Expand Down
3 changes: 0 additions & 3 deletions java/src/jmri/jmrit/display/layoutEditor/LayoutTrack.java
Expand Up @@ -49,9 +49,6 @@ public abstract class LayoutTrack {
// public static final int BEZIER_CONTROL_POINT_OFFSET_MIN = 30; // offset for TrackSegment Bezier control points (minimum)
// public static final int BEZIER_CONTROL_POINT_OFFSET_MAX = 38; // offset for TrackSegment Bezier control points (maximum)
// public static final int SHAPE_CENTER = 39;
// public static final int SHAPE_POINT_OFFSET_MIN = 40; // offset for Shape points (minimum)
// public static final int SHAPE_POINT_OFFSET_MAX = 49; // offset for Shape points (maximum)
// public static final int TURNTABLE_RAY_OFFSET = 50; // offset for turntable connection points
// operational instance variables (not saved between sessions)
protected LayoutEditor layoutEditor = null;
protected String ident = "";
Expand Down
93 changes: 91 additions & 2 deletions java/test/jmri/jmrit/display/layoutEditor/LayoutEditorTest.java
Expand Up @@ -72,6 +72,10 @@ public void testDefaultCtor() {
JUnitUtil.dispose(e);
}

// *****************************************************************
// Turntable Ray support
// *****************************************************************

@Test
public void testHPTturntableTrackIndexOK() {
Assert.assertEquals(0, LayoutEditor.HitPointType.TURNTABLE_RAY_0.turntableTrackIndex());
Expand Down Expand Up @@ -114,7 +118,7 @@ public void testHPTturntableTrackIndexedValueBad2() {
}

@Test
public void turntableValues() {
public void testHPTturntableValues() {
LayoutEditor.HitPointType[] array = LayoutEditor.HitPointType.turntableValues();
Assert.assertEquals(64, array.length);
Assert.assertEquals(LayoutEditor.HitPointType.TURNTABLE_RAY_0, array[0]);
Expand All @@ -124,11 +128,96 @@ public void turntableValues() {
Assert.assertEquals(LayoutEditor.HitPointType.TURNTABLE_RAY_63, array[63]);

// while available, use the indexing operations to test content
for (int i = 0; i<=63; i++) {
for (int i = 0; i<array.length; i++) {
Assert.assertEquals(LayoutEditor.HitPointType.turntableTrackIndexedValue(i),array[i]);
}
}

// *****************************************************************
// Shape Point support
// *****************************************************************

@Test
public void testHPTshapePointIndexOK() {
Assert.assertEquals(0, LayoutEditor.HitPointType.SHAPE_POINT_0.shapePointIndex());
Assert.assertEquals(1, LayoutEditor.HitPointType.SHAPE_POINT_1.shapePointIndex());
Assert.assertEquals(2, LayoutEditor.HitPointType.SHAPE_POINT_2.shapePointIndex());
// ..
Assert.assertEquals(8, LayoutEditor.HitPointType.SHAPE_POINT_8.shapePointIndex());
Assert.assertEquals(9, LayoutEditor.HitPointType.SHAPE_POINT_9.shapePointIndex());
}

@Test(expected = IllegalArgumentException.class)
public void testHPTshapePointIndexBad1() {
LayoutEditor.HitPointType.POS_POINT.shapePointIndex();
}

@Test(expected = IllegalArgumentException.class)
public void testHPTshapePointIndexBad2() {
LayoutEditor.HitPointType.TURNTABLE_RAY_0.shapePointIndex();
}

@Test
public void testHPTshapePointIndexedValueOK() {
Assert.assertEquals(LayoutEditor.HitPointType.SHAPE_POINT_0, LayoutEditor.HitPointType.shapePointIndexedValue(0));
Assert.assertEquals(LayoutEditor.HitPointType.SHAPE_POINT_1, LayoutEditor.HitPointType.shapePointIndexedValue(1));
Assert.assertEquals(LayoutEditor.HitPointType.SHAPE_POINT_2, LayoutEditor.HitPointType.shapePointIndexedValue(2));
// ..
Assert.assertEquals(LayoutEditor.HitPointType.SHAPE_POINT_7, LayoutEditor.HitPointType.shapePointIndexedValue(7));
Assert.assertEquals(LayoutEditor.HitPointType.SHAPE_POINT_8, LayoutEditor.HitPointType.shapePointIndexedValue(8));
Assert.assertEquals(LayoutEditor.HitPointType.SHAPE_POINT_9, LayoutEditor.HitPointType.shapePointIndexedValue(9));
}

@Test(expected = IllegalArgumentException.class)
public void testHPTshapePointIndexedValueBad1() throws IllegalArgumentException {
LayoutEditor.HitPointType.shapePointIndexedValue(-1);
}

@Test(expected = IllegalArgumentException.class)
public void testHPTshapePointIndexedValueBad2() {
LayoutEditor.HitPointType.shapePointIndexedValue(10);
}

@Test
public void testHPTshapePointValues() {
LayoutEditor.HitPointType[] array = LayoutEditor.HitPointType.shapePointValues();
Assert.assertEquals(10, array.length);
Assert.assertEquals(LayoutEditor.HitPointType.SHAPE_POINT_0, array[0]);
Assert.assertEquals(LayoutEditor.HitPointType.SHAPE_POINT_1, array[1]);
// ..
Assert.assertEquals(LayoutEditor.HitPointType.SHAPE_POINT_8, array[8]);
Assert.assertEquals(LayoutEditor.HitPointType.SHAPE_POINT_9, array[9]);

// while available, use the indexing operations to test content
for (int i = 0; i<array.length; i++) {
Assert.assertEquals(LayoutEditor.HitPointType.shapePointIndexedValue(i),array[i]);
}
}

public void testHPTisShapePointOffsetHitPointType() {
Assert.assertTrue(LayoutEditor.HitPointType.isShapePointOffsetHitPointType(LayoutEditor.HitPointType.SHAPE_POINT_0));
Assert.assertTrue(LayoutEditor.HitPointType.isShapePointOffsetHitPointType(LayoutEditor.HitPointType.SHAPE_POINT_1));
// ..
Assert.assertTrue(LayoutEditor.HitPointType.isShapePointOffsetHitPointType(LayoutEditor.HitPointType.SHAPE_POINT_8));
Assert.assertTrue(LayoutEditor.HitPointType.isShapePointOffsetHitPointType(LayoutEditor.HitPointType.SHAPE_POINT_9));

Assert.assertFalse(LayoutEditor.HitPointType.isShapePointOffsetHitPointType(LayoutEditor.HitPointType.NONE));
Assert.assertFalse(LayoutEditor.HitPointType.isShapePointOffsetHitPointType(LayoutEditor.HitPointType.SHAPE_CENTER));
// ..
Assert.assertFalse(LayoutEditor.HitPointType.isShapePointOffsetHitPointType(LayoutEditor.HitPointType.TURNTABLE_RAY_0));
Assert.assertFalse(LayoutEditor.HitPointType.isShapePointOffsetHitPointType(LayoutEditor.HitPointType.TURNTABLE_RAY_63));
}

/**
* Ensure that assigned integer values (xmlValue) and ordinal values are actually the same
*/
@Test
public void testHPTesureOrdinalValuesSameAsAssigned() {
for (LayoutEditor.HitPointType instance : LayoutEditor.HitPointType.values()) {
Assert.assertEquals(Integer.valueOf(instance.ordinal()), instance.getXmlValue());
}
}

/**
* The specific values are tested for historical i/O compatibility
* and because some internal operations do arithmetic with them.
Expand Down

0 comments on commit 0fa8c08

Please sign in to comment.