From 4d708a500370dabe30776179b73f24d66b5d01ec Mon Sep 17 00:00:00 2001 From: TomyLobo Date: Tue, 3 Jan 2012 15:57:29 +0100 Subject: [PATCH] Extracted an AbstractRegion class with some common functionality of all the region types. --- .../worldedit/regions/AbstractRegion.java | 34 +++++++++++++++++++ .../sk89q/worldedit/regions/CuboidRegion.java | 18 ++-------- .../worldedit/regions/EllipsoidRegion.java | 28 ++------------- .../worldedit/regions/Polygonal2DRegion.java | 14 ++------ 4 files changed, 42 insertions(+), 52 deletions(-) create mode 100644 src/main/java/com/sk89q/worldedit/regions/AbstractRegion.java diff --git a/src/main/java/com/sk89q/worldedit/regions/AbstractRegion.java b/src/main/java/com/sk89q/worldedit/regions/AbstractRegion.java new file mode 100644 index 000000000..a5216de35 --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/regions/AbstractRegion.java @@ -0,0 +1,34 @@ +package com.sk89q.worldedit.regions; + +import java.util.Iterator; + +import com.sk89q.worldedit.BlockVector; +import com.sk89q.worldedit.LocalWorld; + +public abstract class AbstractRegion implements Region { + /** + * Stores the world. + */ + protected LocalWorld world; + + public AbstractRegion(LocalWorld world) { + this.world = world; + } + + /** + * Get the iterator. + * + * @return iterator of points inside the region + */ + public Iterator iterator() { + return new RegionIterator(this); + } + + public LocalWorld getWorld() { + return world; + } + + public void setWorld(LocalWorld world) { + this.world = world; + } +} diff --git a/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java b/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java index 0b5a41b0b..62d612d31 100644 --- a/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java +++ b/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java @@ -32,7 +32,7 @@ import java.util.HashSet; * * @author sk89q */ -public class CuboidRegion implements Region { +public class CuboidRegion extends AbstractRegion { /** * Store the first point. */ @@ -41,11 +41,6 @@ public class CuboidRegion implements Region { * Store the second point. */ private Vector pos2; - /** - * Stores the world. - */ - private LocalWorld world; - /** * Construct a new instance of this cuboid region. * @@ -64,8 +59,8 @@ public class CuboidRegion implements Region { * @param pos2 */ public CuboidRegion(LocalWorld world, Vector pos1, Vector pos2) { + super(world); this.pos1 = pos1; - this.world = world; this.pos2 = pos2; } @@ -327,6 +322,7 @@ public class CuboidRegion implements Region { * * @return iterator of points inside the region */ + @Override public Iterator iterator() { return new Iterator() { private Vector min = getMinimumPoint(); @@ -370,12 +366,4 @@ public class CuboidRegion implements Region { public String toString() { return getMinimumPoint() + " - " + getMaximumPoint(); } - - public LocalWorld getWorld() { - return world; - } - - public void setWorld(LocalWorld world) { - this.world = world; - } } diff --git a/src/main/java/com/sk89q/worldedit/regions/EllipsoidRegion.java b/src/main/java/com/sk89q/worldedit/regions/EllipsoidRegion.java index 673f4387d..ed7a4283e 100644 --- a/src/main/java/com/sk89q/worldedit/regions/EllipsoidRegion.java +++ b/src/main/java/com/sk89q/worldedit/regions/EllipsoidRegion.java @@ -19,12 +19,10 @@ package com.sk89q.worldedit.regions; -import com.sk89q.worldedit.BlockVector; import com.sk89q.worldedit.LocalWorld; import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector2D; import com.sk89q.worldedit.data.ChunkStore; -import java.util.Iterator; import java.util.Set; import java.util.HashSet; @@ -32,7 +30,7 @@ import java.util.HashSet; * * @author TomyLobo */ -public class EllipsoidRegion implements Region { +public class EllipsoidRegion extends AbstractRegion { /** * Stores the center. */ @@ -41,11 +39,6 @@ public class EllipsoidRegion implements Region { * Stores the radiuses plus 0.5 on each axis. */ private Vector radius; - /** - * Stores the world. - */ - private LocalWorld world; - /** * Construct a new instance of this ellipsoid region. * @@ -64,7 +57,7 @@ public class EllipsoidRegion implements Region { * @param radius */ public EllipsoidRegion(LocalWorld world, Vector center, Vector radius) { - this.world = world; + super(world); this.center = center; this.radius = radius; } @@ -211,15 +204,6 @@ public class EllipsoidRegion implements Region { return pt.subtract(center).divide(radius).lengthSq() <= 1; } - /** - * Get the iterator. - * - * @return iterator of points inside the region - */ - public Iterator iterator() { - return new RegionIterator(this); - } - /** * Returns string representation in the format * "(centerX, centerY, centerZ) - (radiusX, radiusY, radiusZ)". @@ -231,14 +215,6 @@ public class EllipsoidRegion implements Region { return center + " - " + getRadius(); } - public LocalWorld getWorld() { - return world; - } - - public void setWorld(LocalWorld world) { - this.world = world; - } - public void extendRadius(Vector minRadius) { setRadius(Vector.getMaximum(minRadius, getRadius())); } diff --git a/src/main/java/com/sk89q/worldedit/regions/Polygonal2DRegion.java b/src/main/java/com/sk89q/worldedit/regions/Polygonal2DRegion.java index c9d406daa..a2b6c90fc 100644 --- a/src/main/java/com/sk89q/worldedit/regions/Polygonal2DRegion.java +++ b/src/main/java/com/sk89q/worldedit/regions/Polygonal2DRegion.java @@ -37,14 +37,13 @@ import com.sk89q.worldedit.data.ChunkStore; * * @author sk89q */ -public class Polygonal2DRegion implements Region { +public class Polygonal2DRegion extends AbstractRegion { private List points; private BlockVector min; private BlockVector max; private int minY; private int maxY; private boolean hasY = false; - private LocalWorld world; /** * Construct the region @@ -72,11 +71,11 @@ public class Polygonal2DRegion implements Region { * @param maxY */ public Polygonal2DRegion(LocalWorld world, List points, int minY, int maxY) { + super(world); this.points = new ArrayList(points); this.minY = minY; this.maxY = maxY; hasY = true; - this.world = world; recalculate(); } @@ -440,6 +439,7 @@ public class Polygonal2DRegion implements Region { * * @return iterator of points inside the region */ + @Override public Iterator iterator() { return new Polygonal2DRegionIterator(this); @@ -603,12 +603,4 @@ public class Polygonal2DRegion implements Region { throw new UnsupportedOperationException("Not supported"); } } - - public LocalWorld getWorld() { - return world; - } - - public void setWorld(LocalWorld world) { - this.world = world; - } }