diff options
Diffstat (limited to 'israfil-foundation-nspace/src/main/java')
7 files changed, 657 insertions, 0 deletions
diff --git a/israfil-foundation-nspace/src/main/java/net/israfil/foundation/collections/nspace/Coordinate.java b/israfil-foundation-nspace/src/main/java/net/israfil/foundation/collections/nspace/Coordinate.java new file mode 100755 index 0000000..620124a --- /dev/null +++ b/israfil-foundation-nspace/src/main/java/net/israfil/foundation/collections/nspace/Coordinate.java @@ -0,0 +1,56 @@ +/*
+ * Copyright (c) 2003, Mishkin Berteig and Christian Gruber
+ *
+ * $Id: Coordinate.java 254 2003-02-10 16:01:01Z cgruber $
+ */
+package net.israfil.foundation.collections.nspace;
+
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * Logically an array of tuples
+ *
+ * @author <a href="mailto:mishkin@berteig.com">Mishkin Berteig</a>
+ * @author <a href="mailto:cgruber@israfil.net">Christian Gruber</a>
+ * @version $Revision: 254 $
+ */
+public interface Coordinate {
+
+ /**
+ * A single empty Coordinate, which can be passed in to point-NSpaces, where
+ * no dimension constraint is required.
+ */
+ public final static Coordinate EMPTY =
+ new Coordinate() {
+ private final Set<String> dim = new HashSet<String>();
+
+ public Set<String> getDimensions() {
+ return dim;
+ }
+ public long getDimensionality() { return 0; }
+ public String getOrdinal(String dimensionName) {
+ if (false) dimensionName.hashCode(); //quiet compiler warning.
+ return null;
+ }
+ };
+
+ /**
+ * Returns a set of names of contained dimensions.
+ * @return Set
+ */
+ public Set<String> getDimensions();
+
+ /**
+ * Returns the number of dimensions
+ * @return long
+ */
+ public long getDimensionality();
+
+ /**
+ * Returns the ordinal of the given named dimension..
+ * @return Set
+ */
+ public String getOrdinal(String dimensionName);
+
+}
diff --git a/israfil-foundation-nspace/src/main/java/net/israfil/foundation/collections/nspace/Dimension.java b/israfil-foundation-nspace/src/main/java/net/israfil/foundation/collections/nspace/Dimension.java new file mode 100755 index 0000000..f4e73bb --- /dev/null +++ b/israfil-foundation-nspace/src/main/java/net/israfil/foundation/collections/nspace/Dimension.java @@ -0,0 +1,36 @@ +/*
+ * Copyright (c) 2003, Mishkin Berteig and Christian Gruber
+ *
+ * $Id: Dimension.java 260 2005-05-15 13:08:53Z cgruber $
+ */
+package net.israfil.foundation.collections.nspace;
+
+import java.util.Collection;
+import java.util.Set;
+
+/**
+ * A named set of positions within an nspace
+ *
+ * @author <a href="mailto:mishkin@berteig.com">Mishkin Berteig</a>
+ * @author <a href="mailto:cgruber@israfil.net">Christian Gruber</a>
+ * @version $Revision: 260 $
+ */
+public interface Dimension extends Set<String> {
+
+ /**
+ * Returns the name of the position
+ * @return String
+ */
+ public String getName();
+
+
+ void getWithNSpace(NSpace space);
+ void abandonNSpace(NSpace space);
+ void getWithNSpaces(Collection<NSpace> spaces);
+ void abandonNSpaces(Collection<NSpace> spaces);
+
+ public Dimension addPosition(String s);
+ public Dimension deletePosition(String s);
+
+
+}
diff --git a/israfil-foundation-nspace/src/main/java/net/israfil/foundation/collections/nspace/DimensionExistsException.java b/israfil-foundation-nspace/src/main/java/net/israfil/foundation/collections/nspace/DimensionExistsException.java new file mode 100755 index 0000000..e53397a --- /dev/null +++ b/israfil-foundation-nspace/src/main/java/net/israfil/foundation/collections/nspace/DimensionExistsException.java @@ -0,0 +1,38 @@ +/*
+ * Copyright (c) 2003, Mishkin Berteig and Christian Gruber
+ *
+ * $Id: DimensionExistsException.java 249 2003-02-02 03:33:46Z cgruber $
+ */
+package net.israfil.foundation.collections.nspace;
+
+/**
+ * Thrown if a dimension is added to an NSpace which already defines a
+ * Dimension with that name.
+ *
+ * @author <a href="mailto:mishkin@berteig.com">Mishkin Berteig</a>
+ * @author <a href="mailto:cgruber@israfil.net">Christian Gruber</a>
+ * @version $Revision: 249 $
+ */
+public class DimensionExistsException extends Exception {
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = -801633377290441235L;
+
+ /**
+ * Constructor for DimensionExistsException.
+ */
+ public DimensionExistsException() {
+ super();
+ }
+
+ /**
+ * Constructor for DimensionExistsException.
+ * @param arg0
+ */
+ public DimensionExistsException(String arg0) {
+ super(arg0);
+ }
+
+}
diff --git a/israfil-foundation-nspace/src/main/java/net/israfil/foundation/collections/nspace/InvalidDimensionalityException.java b/israfil-foundation-nspace/src/main/java/net/israfil/foundation/collections/nspace/InvalidDimensionalityException.java new file mode 100755 index 0000000..3a12d31 --- /dev/null +++ b/israfil-foundation-nspace/src/main/java/net/israfil/foundation/collections/nspace/InvalidDimensionalityException.java @@ -0,0 +1,39 @@ +/*
+ * Copyright (c) 2003, Mishkin Berteig and Christian Gruber
+ *
+ * $Id: InvalidDimensionalityException.java 249 2003-02-02 03:33:46Z cgruber $
+ */
+package net.israfil.foundation.collections.nspace;
+
+/**
+ * Thrown when the provided Coodinate inadequately specifies, or incorrectly
+ * specifies the dimensions relative to the NSpace. Primarily used with
+ * setters.
+ *
+ * @author <a href="mailto:mishkin@berteig.com">Mishkin Berteig</a>
+ * @author <a href="mailto:cgruber@israfil.net">Christian Gruber</a>
+ * @version $Revision: 249 $
+ */
+public class InvalidDimensionalityException extends Exception {
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = -5349982487272515716L;
+
+ /**
+ * Constructor for InvalidDimensionalityException.
+ */
+ public InvalidDimensionalityException() {
+ super();
+ }
+
+ /**
+ * Constructor for InvalidDimensionalityException.
+ * @param arg0
+ */
+ public InvalidDimensionalityException(String arg0) {
+ super(arg0);
+ }
+
+}
diff --git a/israfil-foundation-nspace/src/main/java/net/israfil/foundation/collections/nspace/NSpace.java b/israfil-foundation-nspace/src/main/java/net/israfil/foundation/collections/nspace/NSpace.java new file mode 100755 index 0000000..196f4e9 --- /dev/null +++ b/israfil-foundation-nspace/src/main/java/net/israfil/foundation/collections/nspace/NSpace.java @@ -0,0 +1,98 @@ +/*
+ * Copyright (c) 2003, Mishkin Berteig and Christian Gruber
+ *
+ * $Id: NSpace.java 259 2004-08-23 21:30:01Z mberteig $
+ */
+package net.israfil.foundation.collections.nspace;
+
+import java.util.Set;
+
+/**
+ * An N-Dimensional Space representation, such that:
+ * <ol>
+ * <li>Each dimension is named</li>
+ * <li>Each co-ordinate is specified using names not numbers <subscript>IOW:
+ * Domain of each dim. is specified as an ordered set of names
+ * </subscript></li>
+ * <li>Number of dimensions can change throughout lifecycle</li>
+ * <li>Dimensions
+ * </ol>
+ * <p>
+ * A SparseNSpace represents a set of tuples of the form (D1,D2,D3,...,Dn;V)
+ * where Dx represents a location in dimension x and V represents a value in
+ * the n-dimensional space at a location specified by the dimensional locations
+ * D1..Dn. Dimensions can be added or removed at any time with well-defined
+ * results.</p>
+ * <p><strong>Note:</strong> Maybe have origin</p>
+ *
+ * @author <a href="mailto:mishkin@berteig.com">Mishkin Berteig</a>
+ * @author <a href="mailto:cgruber@israfil.net">Christian Gruber</a>
+ * @version $Revision: 259 $
+ */
+public interface NSpace {
+
+ /*
+ * Content Management
+ */
+
+ /**
+ * Returns an NSpace such that each coordinate of the receiver that contains the values of the passed in
+ * coordinate becomes a part of the resulting NSpace. The dimensionality of the resulting NSpace is the difference
+ * of the dimensionality of the receiver and the coordinate. E.g. say we have 3-NSpace with dimensions (D1,D2,D3)
+ * and data [0,0,0;A], [0,1,0;B], [1,0,0;C], [1,1,0;D]. If we lookup a coordinate with dimensions (D2,D3) and values
+ * [0,0] we will get a resulting 1-NSpace with dimension (D1) and data [0;A], [1;C].
+ */
+ public NSpace lookup(Coordinate c);
+
+ /**
+ * Returns an NSpace such that each coordinate of the receiver that contains the values of the passed in
+ * coordinate is not a part of the resulting NSpace. The dimensionality of the resulting NSpace is the dimensionality
+ * of the receiver. E.g. say we have 3-NSpace with dimensions (D1,D2,D3)
+ * and data [0,0,0;A], [0,1,0;B], [1,0,0;C], [1,1,0;D]. If we slice with a coordinate with dimensions (D2,D3) and values
+ * [0,0] we will get a resulting 3-NSpace with dimension (D1,D2,D3) and data [0,1,0;B], [1,1,0;D].
+ */
+ public NSpace slice(Coordinate c);
+
+ /**
+ * Returns an array of NSpace instances such that for each ordinal in Dimension d, there is an NSpace in the array
+ * of dimensionality one less than the receiver that contains the data from the receiver that contains the ordinal.
+ * E.g. say we have 3-NSpace with dimensions (D1,D2,D3)
+ * and data [0,0,0;A], [0,1,0;B], [1,0,0;C], [1,1,0;D].
+ * then slicing on dimension D2 would give us two 2-NSpaces with dimensions (D1,D3):
+ * [0,0;A], [1,0;C] and
+ * [0,0;B], [1,0;D]
+ * @param d the dimension to use to make the cuts in the receiver
+ * @return all the sub-NSpaces, one for each ordinal of d
+ */
+ public NSpace[] slice(Dimension d);
+
+ /**
+ * Returns true if NSpace contains zero dimensions, or if the size of each
+ * dimension == 1.
+ * @return boolean
+ */
+ public boolean isPoint();
+ public Object getValue(Coordinate c);
+
+ public NSpace setValue(Coordinate c, Object value)
+ throws InvalidDimensionalityException;
+ public NSpace setValue(Coordinate c, NSpace value)
+ throws InvalidDimensionalityException;
+
+ /*
+ * Dimension Management
+ */
+ public Dimension createDimension(String name, String initialSlot)
+ throws DimensionExistsException;
+ public void deleteDimension(Dimension dimension);
+ public Set<Dimension> getDimensions();
+ public Dimension getDimension(String name);
+
+ /*
+ * Notifications
+ */
+
+ void dimensionAddedPosition(Dimension dimension,String position);
+ void dimensionRemovedPosition(Dimension dimension, String position);
+
+}
diff --git a/israfil-foundation-nspace/src/main/java/net/israfil/foundation/collections/nspace/SimpleDimension.java b/israfil-foundation-nspace/src/main/java/net/israfil/foundation/collections/nspace/SimpleDimension.java new file mode 100755 index 0000000..52250fa --- /dev/null +++ b/israfil-foundation-nspace/src/main/java/net/israfil/foundation/collections/nspace/SimpleDimension.java @@ -0,0 +1,225 @@ +package net.israfil.foundation.collections.nspace;
+
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
+/**
+ * @author cgruber
+ *
+ * To change this generated comment edit the template variable "typecomment":
+ * Window>Preferences>Java>Templates.
+ * To enable and disable the creation of type comments go to
+ * Window>Preferences>Java>Code Generation.
+ */
+public class SimpleDimension implements Dimension {
+
+ private final String name;
+ private final Set<String> positions = new HashSet<String>();
+ private final Set<NSpace> spaces = new HashSet<NSpace>();
+
+ public SimpleDimension() {
+ name = null;
+ }
+
+ public SimpleDimension(String name, String ordinal) {
+ this.name = name;
+ positions.add(ordinal);
+ }
+
+ /**
+ * @see org.frugenplat.framework.nspace.Dimension#getName()
+ */
+ public String getName() {
+ return this.name;
+ }
+
+ /**
+ * @see java.util.Collection#size()
+ */
+ public int size() {
+ return this.positions.size();
+ }
+
+ /**
+ * @see java.util.Collection#isEmpty()
+ */
+ public boolean isEmpty() {
+ throw new UnsupportedOperationException("Dimension cannot have no positions. Remove the dimension.");
+ }
+
+ /**
+ * @see java.util.Collection#contains(java.lang.Object)
+ */
+ public boolean contains(Object positions) {
+ return this.positions.contains(positions);
+ }
+
+ /**
+ * @see java.util.Collection#iterator()
+ */
+ public Iterator<String> iterator() {
+ return this.positions.iterator();
+ }
+
+ /**
+ * @see java.util.Collection#toArray()
+ */
+ public String[] toArray() {
+ return this.positions.toArray(new String[size()]);
+ }
+
+ /**
+ * @see java.util.Collection#toArray(java.lang.Object)
+ */
+ public <T> T[] toArray(T[] array) {
+ return this.positions.toArray(array);
+ }
+
+ /**
+ * @see java.util.Collection#add(java.lang.Object)
+ */
+ public boolean add(String
+ position) {
+ boolean result = this.positions.add(position);
+ notifyAddPosition((String)position);
+ return result;
+ }
+
+ /**
+ * @see java.util.Collection#remove(java.lang.Object)
+ */
+ public boolean remove(Object position) {
+ boolean result = this.positions.remove(position);
+ notifyRemovePosition((String)position);
+ return result;
+ }
+
+ /**
+ * @see java.util.Collection#containsAll(java.util.Collection)
+ */
+ public boolean containsAll(Collection<?> positions) {
+ return this.positions.containsAll(positions);
+ }
+
+ /**
+ * @see java.util.Collection#addAll(java.util.Collection)
+ */
+ public boolean addAll(Collection<? extends String> positions) {
+ return this.positions.addAll(positions);
+ }
+
+ /**
+ * @see java.util.Collection#retainAll(java.util.Collection)
+ */
+ public boolean retainAll(Collection<?> positions) {
+ return this.positions.retainAll(positions);
+ }
+
+ /**
+ * @see java.util.Collection#removeAll(java.util.Collection)
+ */
+ public boolean removeAll(Collection<?> positions) {
+ return this.positions.removeAll(positions);
+ }
+
+ /**
+ * @see java.util.Collection#clear()
+ */
+ public void clear() {
+ throw new UnsupportedOperationException("Dimensions must always have one ordinal");
+ }
+
+
+
+ /**
+ * @see org.frugenplat.framework.nspace.Dimension#abandonNSpace(org.frugenplat.framework.nspace.NSpace)
+ */
+ public void abandonNSpace(NSpace n) {
+ this.spaces.remove(n);
+ }
+
+ /**
+ * @see org.frugenplat.framework.nspace.Dimension#getWithNSpace(org.frugenplat.framework.nspace.NSpace)
+ */
+ public void getWithNSpace(NSpace n) {
+ this.spaces.add(n);
+ }
+
+ /**
+ * @see org.frugenplat.framework.nspace.Dimension#abandonNSpace(org.frugenplat.framework.nspace.NSpace)
+ */
+ public void abandonNSpaces(Collection<NSpace> spaces) {
+ this.spaces.removeAll(spaces);
+ }
+
+ /**
+ * @see org.frugenplat.framework.nspace.Dimension#getWithNSpace(org.frugenplat.framework.nspace.NSpace)
+ */
+ public void getWithNSpaces(Collection<NSpace> spaces) {
+ this.spaces.addAll(spaces);
+ }
+
+ private void notifyAddPosition(String position) {
+ for (NSpace space : spaces) {
+ space.dimensionAddedPosition(this,position);
+ }
+ }
+
+ private void notifyRemovePosition(String position) {
+ for (NSpace space : spaces) {
+ space.dimensionRemovedPosition(this,position);
+ }
+ }
+
+ @SuppressWarnings("unused")
+ private void notifyAddPositions(Collection<String> positions) {
+
+ }
+
+ @SuppressWarnings("unused")
+ private void notifyRemovePositions(Collection<String> positions) {
+
+ }
+
+ /**
+ * @see java.lang.Object#hashCode()
+ */
+ public int hashCode() {
+ return name.hashCode();
+ }
+
+ /**
+ * @see java.lang.Object#toString()
+ */
+ public String toString() {
+ StringBuffer sb = new StringBuffer();
+ sb.append(super.toString());
+ sb.append(" [");
+ sb.append(name);
+ sb.append("] {");
+ for (String s : positions) sb.append(s);
+ sb.append("}");
+ return sb.toString();
+ }
+
+ /**
+ * @see org.frugenplat.framework.nspace.Dimension#addPosition(java.lang.String)
+ */
+ public Dimension addPosition(String s) {
+ add(s);
+ return this;
+ }
+
+ /**
+ * @see org.frugenplat.framework.nspace.Dimension#deletePosition(java.lang.String)
+ */
+ public Dimension deletePosition(String s) {
+ remove(s);
+ return this;
+ }
+
+
+
+}
diff --git a/israfil-foundation-nspace/src/main/java/net/israfil/foundation/collections/nspace/SparseNSpace.java b/israfil-foundation-nspace/src/main/java/net/israfil/foundation/collections/nspace/SparseNSpace.java new file mode 100755 index 0000000..4c48842 --- /dev/null +++ b/israfil-foundation-nspace/src/main/java/net/israfil/foundation/collections/nspace/SparseNSpace.java @@ -0,0 +1,165 @@ +/*
+ * Copyright (c) 2003, Mishkin Berteig and Christian Gruber
+ *
+ * $Id: SparseNSpace.java 260 2005-05-15 13:08:53Z cgruber $
+ */
+package net.israfil.foundation.collections.nspace;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * An implementatino of NSpace that is sparsely populated.
+ *
+ * @author <a href="mailto:mishkin@berteig.com">Mishkin Berteig</a>
+ * @author <a href="mailto:cgruber@israfil.net">Christian Gruber</a>
+ * @version $Revision: 260 $
+ */
+public class SparseNSpace implements NSpace {
+
+ private final Set<Dimension> dimensions = new HashSet<Dimension>();
+
+ public SparseNSpace() {
+ super();
+ }
+
+ public SparseNSpace(Set<Dimension> initialDimensions) {
+ this();
+ for (Dimension d : dimensions) {
+ // This is temporary, until we have an internal protected mutator.
+ dimensions.add(d);
+ }
+ }
+
+ /**
+ * @see org.frugenplat.framework.nspace.NSpace#createDimension(java.lang.String, java.lang.String)
+ */
+ public Dimension createDimension(String name, String initialSlot)
+ throws DimensionExistsException {
+ Dimension d = new SimpleDimension(name,initialSlot);
+ dimensions.add(d);
+ return d;
+ }
+
+ /**
+ * @see org.frugenplat.framework.nspace.NSpace#deleteDimension(org.frugenplat.framework.nspace.Dimension)
+ */
+ public void deleteDimension(Dimension dimension) {
+ throw new UnsupportedOperationException("Not yet implemented.");
+ }
+
+ /**
+ * @see org.frugenplat.framework.nspace.NSpace#dimensionAddedPosition(org.frugenplat.framework.nspace.Dimension, java.lang.String)
+ */
+ public void dimensionAddedPosition(Dimension dimension, String position) {
+ throw new UnsupportedOperationException("Not yet implemented.");
+ }
+
+ /**
+ * @see org.frugenplat.framework.nspace.NSpace#dimensionRemovedPosition(org.frugenplat.framework.nspace.Dimension, java.lang.String)
+ */
+ public void dimensionRemovedPosition(
+ Dimension dimension,
+ String position) {
+ throw new UnsupportedOperationException("Not yet implemented.");
+ }
+
+ /**
+ * @see org.frugenplat.framework.nspace.NSpace#getDimension(java.lang.String)
+ */
+ public Dimension getDimension(String name) {
+ throw new UnsupportedOperationException("Not yet implemented.");
+ }
+
+ /**
+ * Returns an immutable set of dimensions for inspection..
+ * @see org.frugenplat.framework.nspace.NSpace#getDimensions()
+ */
+ public Set<Dimension> getDimensions() {
+ return Collections.unmodifiableSet(dimensions);
+ }
+
+ /**
+ * Exposes the internal dimensions set to any children
+ * @return Set
+ */
+ protected Set<Dimension> getInternalDimensions() {
+ return dimensions;
+ }
+
+ /**
+ * @see org.frugenplat.framework.nspace.NSpace#getValue()
+ */
+ public Object getValue(Coordinate c) {
+ // SparseNSpace s = _slice(c);
+ throw new UnsupportedOperationException("Not yet implemented.");
+ }
+
+ Object _findValue() {
+ throw new UnsupportedOperationException("Not yet implemented.");
+ }
+
+ /**
+ * @see org.frugenplat.framework.nspace.NSpace#isPoint()
+ */
+ public boolean isPoint() {
+ if (dimensions.size() == 0) return true;
+ else {
+ boolean hasThinDimensions = true;
+ for(Dimension d : dimensions){
+ hasThinDimensions = hasThinDimensions &&
+ (d.size() == 1);
+ }
+ return hasThinDimensions;
+ }
+
+ }
+
+ /**
+ * @see org.frugenplat.framework.nspace.NSpace#lookup(org.frugenplat.framework.nspace.Coordinate)
+ */
+ public NSpace lookup(Coordinate c) {
+ return _lookup(c);
+ }
+
+ /**
+ * @see org.frugenplat.framework.nspace.NSpace#setValue(org.frugenplat.framework.nspace.Coordinate, org.frugenplat.framework.nspace.NSpace)
+ */
+ public NSpace setValue(Coordinate c, NSpace value)
+ throws InvalidDimensionalityException {
+ throw new UnsupportedOperationException("Not yet implemented.");
+ }
+
+ /**
+ * @see org.frugenplat.framework.nspace.NSpace#setValue(org.frugenplat.framework.nspace.Coordinate, java.lang.Object)
+ */
+ public NSpace setValue(Coordinate c, Object value)
+ throws InvalidDimensionalityException {
+ throw new UnsupportedOperationException("Not yet implemented.");
+ }
+
+ /**
+ * @see org.frugenplat.framework.nspace.NSpace#slice(org.frugenplat.framework.nspace.Coordinate)
+ */
+ public NSpace slice(Coordinate c) {
+ return _slice(c);
+ }
+
+ private SparseNSpace _lookup(Coordinate c) {
+ if (c.equals(Coordinate.EMPTY)) return this;
+ throw new UnsupportedOperationException("Not yet implemented.");
+ }
+
+ private SparseNSpace _slice(Coordinate c) {
+ if (c.equals(Coordinate.EMPTY)) return this;
+ throw new UnsupportedOperationException("Not yet implemented.");
+ }
+ /* (non-Javadoc)
+ * @see org.frugenplat.framework.nspace.NSpace#slice(org.frugenplat.framework.nspace.Dimension)
+ */
+ public NSpace[] slice(Dimension d) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+}
|
