Revert "Removed parents from regions."

Removal of parents will make upgrading extremely difficult and so consider them pending for the itme being.

This reverts commit df54d4de91.
This commit is contained in:
sk89q 2013-02-06 17:19:09 -08:00
parent 7acfb3586c
commit 4b15855b2f
2 changed files with 63 additions and 0 deletions

View File

@ -40,6 +40,7 @@ public class Region implements Comparable<Region> {
private final String id;
private IndexableShape shape;
private int priority = 0;
private Region parent;
private Map<String, Attribute> attributes = new HashMap<String, Attribute>();
/**
@ -105,6 +106,46 @@ public int getPriority() {
public void setPriority(int priority) {
this.priority = priority;
}
/**
* Get the parent of the region. Parents can determine how multiple overlapping
* regions are handled in regards to some flags, but it is dependent on the flag.
*
* @return parent region or null
*/
public Region getParent() {
return parent;
}
/**
* Set the parent of this region.
*
* @see #getParent() for an explanation of parents
* @param parent the new parent, or null
* @throws IllegalArgumentException when circular inheritance is detected
*/
public synchronized void setParent(Region parent) throws IllegalArgumentException {
if (parent == null) {
this.parent = null;
} else {
if (parent == this) {
throw new IllegalArgumentException(
"Circular region inheritance detected");
}
Region p = parent.getParent();
while (p != null) {
if (p == this) {
throw new IllegalArgumentException(
"Circular region inheritance detected");
}
p = p.getParent();
}
this.parent = parent;
}
}
/**
* Set an attribute to this region.

View File

@ -60,6 +60,28 @@ public void testCompareTo() {
assertEquals(regionB.compareTo(regionB), 0);
}
@Test
public void testSetParent() {
// Test self setting
Region regionA = new Region("A", mock(IndexableShape.class));
thrown.expect(IllegalArgumentException.class);
regionA.setParent(regionA);
// Test regular setting
Region child = new Region("A", mock(IndexableShape.class));
Region parent = new Region("B", mock(IndexableShape.class));
child.setParent(parent);
assertEquals(child.getParent(), parent);
assertEquals(parent.getParent(), null);
// Test circular
child = new Region("A", mock(IndexableShape.class));
parent = new Region("B", mock(IndexableShape.class));
child.setParent(parent);
thrown.expect(IllegalArgumentException.class);
parent.setParent(child);
}
@Test
public void testGetPriority() {
Region regionA = new Region("A", mock(IndexableShape.class));