Commit 3f189de9 authored by Jakob Schöttl's avatar Jakob Schöttl

Make all attributes cloneable

parent b523ea4c
...@@ -16,7 +16,18 @@ package org.vadere.state.attributes; ...@@ -16,7 +16,18 @@ package org.vadere.state.attributes;
* VPoint,...). * VPoint,...).
* *
*/ */
public abstract class Attributes extends DefaultSealable { public abstract class Attributes extends DefaultSealable implements Cloneable {
/** Used for default ID values of some scenario elements. */ /** Used for default ID values of some scenario elements. */
protected static final int ID_NOT_SET = -1; 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.");
}
}
} }
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();
}
}
...@@ -11,7 +11,7 @@ import org.vadere.util.geometry.GeometryUtils; ...@@ -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); public static final VPoint ZERO = new VPoint(0, 0);
......
...@@ -7,9 +7,8 @@ import org.vadere.util.geometry.ShapeType; ...@@ -7,9 +7,8 @@ import org.vadere.util.geometry.ShapeType;
/** /**
* Geometric shape and position. * Geometric shape and position.
* *
*
*/ */
public interface VShape extends Shape { public interface VShape extends Shape, Cloneable {
double distance(VPoint point); double distance(VPoint point);
VPoint closestPoint(VPoint point); VPoint closestPoint(VPoint point);
......
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