diff --git a/VadereState/src/org/vadere/state/attributes/Attributes.java b/VadereState/src/org/vadere/state/attributes/Attributes.java index ef95c313880e61dc930706d7b388f18ec52eb352..9a892ae59a6f41887c377e5dee984561007f5141 100644 --- a/VadereState/src/org/vadere/state/attributes/Attributes.java +++ b/VadereState/src/org/vadere/state/attributes/Attributes.java @@ -16,7 +16,18 @@ package org.vadere.state.attributes; * VPoint,...). * */ -public abstract class Attributes extends DefaultSealable { +public abstract class Attributes extends DefaultSealable implements Cloneable { /** Used for default ID values of some scenario elements. */ protected static final int ID_NOT_SET = -1; + + public Attributes() {} + + public Attributes cloneAttributes() { + try { + return (Attributes) super.clone(); + } catch (CloneNotSupportedException e) { + throw new RuntimeException("This should never happen because the base class Attributes is Cloneable."); + } + } + } diff --git a/VadereState/tests/org/vadere/state/attributes/TestAttributesCloneable.java b/VadereState/tests/org/vadere/state/attributes/TestAttributesCloneable.java new file mode 100644 index 0000000000000000000000000000000000000000..2f08bde780cf0256e8fd38ea62036eef7e07da4a --- /dev/null +++ b/VadereState/tests/org/vadere/state/attributes/TestAttributesCloneable.java @@ -0,0 +1,59 @@ +package org.vadere.state.attributes; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class TestAttributesCloneable { + + private static class SimpleAttributes extends Attributes { + int id = 1; + } + + private static class NestedAttributes extends Attributes { + int id = 1; + SimpleAttributes c = new SimpleAttributes(); + SimpleCloneable d = new SimpleCloneable(); + } + + private static class SimpleCloneable implements Cloneable { + int id = 1; + } + + private static class NotCloneableClass { + @Override + public Object clone() throws CloneNotSupportedException { + return super.clone(); // throws exception because it does not implement Cloneable + } + } + + @Test + public void testSimpleCloneable() { + try { + SimpleAttributes a = new SimpleAttributes(); + SimpleAttributes b = (SimpleAttributes) a.cloneAttributes(); + assertEquals(a.id, b.id); + } catch (Exception e) { + fail("clone should not throw exception"); + } + } + + @Test + public void testExtendedCloneable() { + try { + NestedAttributes a = new NestedAttributes(); + NestedAttributes b = (NestedAttributes) a.cloneAttributes(); + assertEquals(a.id, b.id); + assertEquals(a.c.id, b.c.id); + assertEquals(a.d.id, b.d.id); + } catch (Exception e) { + fail("clone should not throw exception"); + } + } + + @Test(expected=CloneNotSupportedException.class) + public void testNotCloneableClass() throws CloneNotSupportedException { + new NotCloneableClass().clone(); + } + +} diff --git a/VadereUtils/src/org/vadere/util/geometry/shapes/VPoint.java b/VadereUtils/src/org/vadere/util/geometry/shapes/VPoint.java index 66be818f5ff239e4f72c96eab7e2659eed6d4fc3..fcb41fe299a97f5e8f2b44ed1e1842ae3c39e172 100644 --- a/VadereUtils/src/org/vadere/util/geometry/shapes/VPoint.java +++ b/VadereUtils/src/org/vadere/util/geometry/shapes/VPoint.java @@ -11,7 +11,7 @@ import org.vadere.util.geometry.GeometryUtils; * * */ -public class VPoint { +public class VPoint implements Cloneable { public static final VPoint ZERO = new VPoint(0, 0); diff --git a/VadereUtils/src/org/vadere/util/geometry/shapes/VShape.java b/VadereUtils/src/org/vadere/util/geometry/shapes/VShape.java index ab8b7c20986177ea477b9164bb38cd1760e8c1bf..a35ea48f828df1ddd5226640220a3cd60810915c 100644 --- a/VadereUtils/src/org/vadere/util/geometry/shapes/VShape.java +++ b/VadereUtils/src/org/vadere/util/geometry/shapes/VShape.java @@ -7,9 +7,8 @@ import org.vadere.util.geometry.ShapeType; /** * Geometric shape and position. * - * */ -public interface VShape extends Shape { +public interface VShape extends Shape, Cloneable { double distance(VPoint point); VPoint closestPoint(VPoint point);