A bunch of finagling to work around GWT limitations.

This commit is contained in:
Michael Bayne
2011-06-27 16:17:38 -07:00
parent 7a3da3b08c
commit 684c92c59e
16 changed files with 195 additions and 65 deletions
+4 -4
View File
@@ -41,6 +41,7 @@
<target name="compile" depends="-prepare" description="Compiles the code.">
<mkdir dir="${classes.dir}"/>
<copy todir="${classes.dir}"><fileset dir="src/main/resources" includes="**"/></copy>
<javac srcdir="${src.dir}" destdir="${classes.dir}" includeAntRuntime="false"
debug="on" optimize="${build.optimize}" source="1.6" target="1.6" encoding="utf-8">
<classpath refid="classpath"/>
@@ -63,9 +64,7 @@
<mkdir dir="${javadoc.dir}"/>
<javadoc windowtitle="${ant.project.name} API" doctitle="${ant.project.name} API"
access="public" destdir="${javadoc.dir}" additionalparam="-breakiterator">
<packageset dir="${src.dir}">
<exclude name="**/impl/**"/>
</packageset>
<packageset dir="${src.dir}"/>
<classpath refid="classpath"/>
<link href="http://download.oracle.com/javase/1.5.0/docs/api/"/>
</javadoc>
@@ -91,8 +90,9 @@
<!-- todo: depend on tests target -->
<target name="package" depends="compile" description="Compiles code and builds jar file.">
<jar destfile="${target.dir}/${ant.project.name}.jar">
<fileset dir="${classes.dir}" includes="**"/>
<fileset dir="${src.dir}" includes="**"/> <!-- for use by GWT apps -->
<!-- this must follow src.dir so that Platform.java is properly overwritten -->
<fileset dir="${classes.dir}" includes="**"/>
</jar>
</target>
@@ -3,4 +3,5 @@
<source path="f"/>
<source path="d"/>
<source path="i"/>
<source path="util"/>
</module>
@@ -4,6 +4,8 @@
package pythagoras.d;
import pythagoras.util.Platform;
/**
* Provides most of the implementation of {@link IDimension}, obtaining only width and height from
* the derived class.
@@ -17,8 +19,8 @@ public abstract class AbstractDimension implements IDimension
@Override
public int hashCode () {
long bits = Double.doubleToLongBits(getWidth());
bits += Double.doubleToLongBits(getHeight()) * 37;
long bits = Platform.hashCode(getWidth());
bits += Platform.hashCode(getHeight()) * 37;
return (((int) bits) ^ ((int) (bits >> 32)));
}
@@ -4,6 +4,8 @@
package pythagoras.d;
import pythagoras.util.Platform;
/**
* Provides most of the implementation of {@link IPoint}, obtaining only the location from the
* derived class.
@@ -49,9 +51,7 @@ public abstract class AbstractPoint implements IPoint
@Override
public int hashCode () {
long bits = Double.doubleToLongBits(getX());
bits += Double.doubleToLongBits(getY()) * 37;
return (((int) bits) ^ ((int) (bits >> 32)));
return Platform.hashCode(getX()) ^ Platform.hashCode(getY());
}
@Override
@@ -6,6 +6,8 @@ package pythagoras.d;
import java.util.NoSuchElementException;
import pythagoras.util.Platform;
/**
* Provides most of the implementation of {@link IRectangle}, obtaining only the location and
* dimensions from the derived class.
@@ -151,11 +153,8 @@ public abstract class AbstractRectangle extends RectangularShape implements IRec
@Override // from Object
public int hashCode () {
long bits = Double.doubleToLongBits(getX());
bits += Double.doubleToLongBits(getY()) * 37;
bits += Double.doubleToLongBits(getWidth()) * 43;
bits += Double.doubleToLongBits(getHeight()) * 47;
return (((int) bits) ^ ((int) (bits >> 32)));
return Platform.hashCode(getX()) ^ Platform.hashCode(getY()) ^
Platform.hashCode(getWidth()) ^ Platform.hashCode(getHeight());
}
@Override // from Object
@@ -6,6 +6,8 @@ package pythagoras.d;
import java.io.Serializable;
import pythagoras.util.Platform;
/**
* Represents a 2D affine transform, which performs a linear mapping that preserves the
* straightness and parallelness of lines.
@@ -437,24 +439,17 @@ public class AffineTransform implements Cloneable, Serializable
"[[" + m00 + ", " + m01 + ", " + m02 + "], [" + m10 + ", " + m11 + ", " + m12 + "]]";
}
@Override
// @Override // can't declare @Override due to GWT
public AffineTransform clone () {
try {
return (AffineTransform)super.clone();
} catch (CloneNotSupportedException e) {
throw new InternalError();
}
AffineTransform xform = new AffineTransform(m00, m01, m02, m10, m11, m12);
xform.type = this.type;
return xform;
}
@Override
public int hashCode () {
long bits = Double.doubleToLongBits(m00);
bits += Double.doubleToLongBits(m01) * 37;
bits += Double.doubleToLongBits(m02) * 43;
bits += Double.doubleToLongBits(m10) * 47;
bits += Double.doubleToLongBits(m11) * 53;
bits += Double.doubleToLongBits(m12) * 59;
return (((int) bits) ^ ((int) (bits >> 32)));
return Platform.hashCode(m00) ^ Platform.hashCode(m01) ^ Platform.hashCode(m02) ^
Platform.hashCode(m10) ^ Platform.hashCode(m11) ^ Platform.hashCode(m12);
}
@Override
+6 -4
View File
@@ -6,6 +6,8 @@ package pythagoras.d;
import java.util.NoSuchElementException;
import pythagoras.util.Platform;
/**
* Stores and manipulates an enclosed area of 2D space.
* See http://download.oracle.com/javase/6/docs/api/java/awt/geom/Area.html
@@ -277,7 +279,7 @@ public class Area implements IShape, Cloneable
return area.isEmpty();
}
@Override // from Cloneable
// @Override // can't declare @Override due to GWT
public Area clone () {
Area area = new Area();
copy(this, area);
@@ -1038,11 +1040,11 @@ public class Area implements IShape, Cloneable
private void copy (Area src, Area dst) {
dst.coordsSize = src.coordsSize;
dst.coords = src.coords.clone();
dst.coords = Platform.clone(src.coords);
dst.rulesSize = src.rulesSize;
dst.rules = src.rules.clone();
dst.rules = Platform.clone(src.rules);
dst.moveToCount = src.moveToCount;
dst.offsets = src.offsets.clone();
dst.offsets = Platform.clone(src.offsets);
}
private int containsExact (double x, double y) {
+12 -9
View File
@@ -6,6 +6,8 @@ package pythagoras.d;
import java.util.NoSuchElementException;
import pythagoras.util.Platform;
/**
* Represents a path constructed from lines and curves and which can contain subpaths.
*/
@@ -248,16 +250,9 @@ public final class Path implements IShape, Cloneable
return new FlatteningPathIterator(getPathIterator(t), flatness);
}
@Override
// @Override // can't declare @Override due to GWT
public Path clone () {
try {
Path p = (Path)super.clone();
p.types = types.clone();
p.points = points.clone();
return p;
} catch (CloneNotSupportedException e) {
throw new InternalError();
}
return new Path(rule, Platform.clone(types), Platform.clone(points), typeSize, pointSize);
}
/**
@@ -293,6 +288,14 @@ public final class Path implements IShape, Cloneable
Crossing.isInsideEvenOdd(cross);
}
private Path (int rule, byte[] types, double[] points, int typeSize, int pointSize) {
this.types = types;
this.points = points;
this.typeSize = typeSize;
this.pointSize = pointSize;
setWindingRule(rule);
}
/** An iterator over a {@link Path}. */
protected static class Iterator implements PathIterator
{
@@ -4,6 +4,8 @@
package pythagoras.f;
import pythagoras.util.Platform;
/**
* Provides most of the implementation of {@link IDimension}, obtaining only width and height from
* the derived class.
@@ -17,7 +19,7 @@ public abstract class AbstractDimension implements IDimension
@Override
public int hashCode () {
return Float.floatToIntBits(getWidth()) ^ Float.floatToIntBits(getHeight());
return Platform.hashCode(getWidth()) ^ Platform.hashCode(getHeight());
}
@Override
@@ -4,6 +4,8 @@
package pythagoras.f;
import pythagoras.util.Platform;
/**
* Provides most of the implementation of {@link IPoint}, obtaining only the location from the
* derived class.
@@ -49,7 +51,7 @@ public abstract class AbstractPoint implements IPoint
@Override
public int hashCode () {
return Float.floatToIntBits(getX()) ^ Float.floatToIntBits(getY());
return Platform.hashCode(getX()) ^ Platform.hashCode(getY());
}
@Override
@@ -6,6 +6,8 @@ package pythagoras.f;
import java.util.NoSuchElementException;
import pythagoras.util.Platform;
/**
* Provides most of the implementation of {@link IRectangle}, obtaining only the location and
* dimensions from the derived class.
@@ -151,8 +153,8 @@ public abstract class AbstractRectangle extends RectangularShape implements IRec
@Override // from Object
public int hashCode () {
return Float.floatToIntBits(getX()) ^ Float.floatToIntBits(getY()) ^
Float.floatToIntBits(getWidth()) ^ Float.floatToIntBits(getHeight());
return Platform.hashCode(getX()) ^ Platform.hashCode(getY()) ^
Platform.hashCode(getWidth()) ^ Platform.hashCode(getHeight());
}
@Override // from Object
@@ -6,6 +6,8 @@ package pythagoras.f;
import java.io.Serializable;
import pythagoras.util.Platform;
/**
* Represents a 2D affine transform, which performs a linear mapping that preserves the
* straightness and parallelness of lines.
@@ -436,19 +438,17 @@ public class AffineTransform implements Cloneable, Serializable
"[[" + m00 + ", " + m01 + ", " + m02 + "], [" + m10 + ", " + m11 + ", " + m12 + "]]";
}
@Override
// @Override // can't declare @Override due to GWT
public AffineTransform clone () {
try {
return (AffineTransform)super.clone();
} catch (CloneNotSupportedException e) {
throw new InternalError();
}
AffineTransform xform = new AffineTransform(m00, m01, m02, m10, m11, m12);
xform.type = this.type;
return xform;
}
@Override
public int hashCode () {
return Float.floatToIntBits(m00) ^ Float.floatToIntBits(m01) ^ Float.floatToIntBits(m02) ^
Float.floatToIntBits(m10) ^ Float.floatToIntBits(m11) ^ Float.floatToIntBits(m12);
return Platform.hashCode(m00) ^ Platform.hashCode(m01) ^ Platform.hashCode(m02) ^
Platform.hashCode(m10) ^ Platform.hashCode(m11) ^ Platform.hashCode(m12);
}
@Override
+6 -4
View File
@@ -6,6 +6,8 @@ package pythagoras.f;
import java.util.NoSuchElementException;
import pythagoras.util.Platform;
/**
* Stores and manipulates an enclosed area of 2D space.
* See http://download.oracle.com/javase/6/docs/api/java/awt/geom/Area.html
@@ -277,7 +279,7 @@ public class Area implements IShape, Cloneable
return area.isEmpty();
}
@Override // from Cloneable
// @Override // can't declare @Override due to GWT
public Area clone () {
Area area = new Area();
copy(this, area);
@@ -1038,11 +1040,11 @@ public class Area implements IShape, Cloneable
private void copy (Area src, Area dst) {
dst.coordsSize = src.coordsSize;
dst.coords = src.coords.clone();
dst.coords = Platform.clone(src.coords);
dst.rulesSize = src.rulesSize;
dst.rules = src.rules.clone();
dst.rules = Platform.clone(src.rules);
dst.moveToCount = src.moveToCount;
dst.offsets = src.offsets.clone();
dst.offsets = Platform.clone(src.offsets);
}
private int containsExact (float x, float y) {
+12 -9
View File
@@ -6,6 +6,8 @@ package pythagoras.f;
import java.util.NoSuchElementException;
import pythagoras.util.Platform;
/**
* Represents a path constructed from lines and curves and which can contain subpaths.
*/
@@ -248,16 +250,9 @@ public final class Path implements IShape, Cloneable
return new FlatteningPathIterator(getPathIterator(t), flatness);
}
@Override
// @Override // can't declare @Override due to GWT
public Path clone () {
try {
Path p = (Path)super.clone();
p.types = types.clone();
p.points = points.clone();
return p;
} catch (CloneNotSupportedException e) {
throw new InternalError();
}
return new Path(rule, Platform.clone(types), Platform.clone(points), typeSize, pointSize);
}
/**
@@ -293,6 +288,14 @@ public final class Path implements IShape, Cloneable
Crossing.isInsideEvenOdd(cross);
}
private Path (int rule, byte[] types, float[] points, int typeSize, int pointSize) {
this.types = types;
this.points = points;
this.typeSize = typeSize;
this.pointSize = pointSize;
setWindingRule(rule);
}
/** An iterator over a {@link Path}. */
protected static class Iterator implements PathIterator
{
@@ -0,0 +1,54 @@
//
// Pythagoras - a collection of geometry classes
// http://github.com/samskivert/pythagoras
package pythagoras.util;
/**
* Handles differences between the JDK and GWT platforms.
*/
public class Platform
{
/**
* Returns a hash code for the supplied float value.
*/
public static int hashCode (float f1) {
return Float.floatToIntBits(f1);
}
/**
* Returns a hash code for the supplied double value.
*/
public static int hashCode (double d1) {
long bits = Double.doubleToLongBits(d1);
return (int)(bits ^ (bits >>> 32));
}
/**
* Clones the supplied array of bytes.
*/
public static byte[] clone (byte[] values) {
return values.clone();
}
/**
* Clones the supplied array of ints.
*/
public static int[] clone (int[] values) {
return values.clone();
}
/**
* Clones the supplied array of floats.
*/
public static float[] clone (float[] values) {
return values.clone();
}
/**
* Clones the supplied array of doubles.
*/
public static double[] clone (double[] values) {
return values.clone();
}
}
@@ -0,0 +1,63 @@
//
// Pythagoras - a collection of geometry classes
// http://github.com/samskivert/pythagoras
package pythagoras.util;
/**
* A platform instance that's used when running in GWT. Note that this is copied over top of the
* JDK implementation in the pythagoras.jar file, so this is never actually compiled to bytecode.
* Thus be careful when making changes to this file.
*/
public class Platform
{
/**
* Returns a hash code for the supplied float value.
*/
public static int hashCode (float f1) {
return (int)f1; // alas nothing better can be done in JavaScript
}
/**
* Returns a hash code for the supplied double value.
*/
public static int hashCode (double d1) {
return (int)d1; // alas nothing better can be done in JavaScript
}
/**
* Clones the supplied array of bytes.
*/
public static byte[] clone (byte[] values) {
byte[] nvalues = new byte[values.length];
System.arraycopy(values, 0, nvalues, 0, values.length);
return nvalues;
}
/**
* Clones the supplied array of ints.
*/
public static int[] clone (int[] values) {
int[] nvalues = new int[values.length];
System.arraycopy(values, 0, nvalues, 0, values.length);
return nvalues;
}
/**
* Clones the supplied array of floats.
*/
public static float[] clone (float[] values) {
float[] nvalues = new float[values.length];
System.arraycopy(values, 0, nvalues, 0, values.length);
return nvalues;
}
/**
* Clones the supplied array of doubles.
*/
public static double[] clone (double[] values) {
double[] nvalues = new double[values.length];
System.arraycopy(values, 0, nvalues, 0, values.length);
return nvalues;
}
}