Commits

Michael Ludwig committed 9f28a71

Add generic type parameter to ObjectProerty and add friendlier setter/getters to the default properties.

Comments (0)

Files changed (5)

src/main/java/com/googlecode/entreri/property/FloatProperty.java

  */
 public final class FloatProperty implements Property {
     private FloatDataStore store;
+    
+    /**
+     * Create a FloatProperty with an element size of 1.
+     */
+    public FloatProperty() {
+        this(1);
+    }
 
     /**
      * Create a new FloatProperty where each property will have
         return store.array;
     }
     
+    /**
+     * Get the value stored in this property for the given component index, and
+     * offset. Offset is measured from 0 to 1 minus the element size the
+     * property was originally created with.
+     * 
+     * @param componentIndex The component's index
+     * @param offset The offset into the component's data
+     * @return The object at the given offset for the given component
+     * @throws ArrayIndexOutOfBoundsException if the componentIndex and offset
+     *             would access illegal indices
+     */
+    public float get(int componentIndex, int offset) {
+        return store.array[componentIndex * store.elementSize + offset];
+    }
+
+    /**
+     * Store <tt>val</tt> in this property for the given component index, at the
+     * specified offset. The offset is measured from 0 to 1 minus the element
+     * size that this property was originally created with.
+     * 
+     * @param val The value to store, can be null
+     * @param componentIndex The index of the component being modified
+     * @param offset The offset into the component's data
+     * @throws ArrayIndexOutOfBoundsException if the componentIndex and offset
+     *             would access illegal indices
+     */
+    public void set(float val, int componentIndex, int offset) {
+        store.array[componentIndex * store.elementSize] = val;
+    }
+    
     @Override
     public IndexedDataStore getDataStore() {
         return store;

src/main/java/com/googlecode/entreri/property/IntProperty.java

     private IntDataStore store;
     
     /**
+     * Create an IntProperty with an element size of 1.
+     */
+    public IntProperty() {
+        this(1);
+    }
+    
+    /**
      * Create a new IntProperty where each property will have
      * <tt>elementSize</tt> array elements together.
      * 
         return store.array;
     }
     
+    /**
+     * Get the value stored in this property for the given component index, and
+     * offset. Offset is measured from 0 to 1 minus the element size the
+     * property was originally created with.
+     * 
+     * @param componentIndex The component's index
+     * @param offset The offset into the component's data
+     * @return The object at the given offset for the given component
+     * @throws ArrayIndexOutOfBoundsException if the componentIndex and offset
+     *             would access illegal indices
+     */
+    public int get(int componentIndex, int offset) {
+        return store.array[componentIndex * store.elementSize + offset];
+    }
+
+    /**
+     * Store <tt>val</tt> in this property for the given component index, at the
+     * specified offset. The offset is measured from 0 to 1 minus the element
+     * size that this property was originally created with.
+     * 
+     * @param val The value to store, can be null
+     * @param componentIndex The index of the component being modified
+     * @param offset The offset into the component's data
+     * @throws ArrayIndexOutOfBoundsException if the componentIndex and offset
+     *             would access illegal indices
+     */
+    public void set(int val, int componentIndex, int offset) {
+        store.array[componentIndex * store.elementSize] = val;
+    }
+    
     @Override
     public IndexedDataStore getDataStore() {
         return store;

src/main/java/com/googlecode/entreri/property/ObjectProperty.java

  * 
  * @author Michael Ludwig
  */
-public final class ObjectProperty implements Property {
+public final class ObjectProperty<T> implements Property {
     private ObjectDataStore store;
     
     /**
+     * Create an ObjectProperty with an element size of 1.
+     */
+    public ObjectProperty() {
+        this(1);
+    }
+    
+    /**
      * Create a new ObjectProperty where each property will have
      * <tt>elementSize</tt> array elements together.
      * 
      * @param elementSize The element size of the created properties
      * @return A PropertyFactory for ObjectProperty
      */
-    public static PropertyFactory<ObjectProperty> factory(final int elementSize) {
-        return new PropertyFactory<ObjectProperty>() {
+    public static <T> PropertyFactory<ObjectProperty<T>> factory(final int elementSize) {
+        return new PropertyFactory<ObjectProperty<T>>() {
             @Override
-            public ObjectProperty create() {
-                return new ObjectProperty(elementSize);
+            public ObjectProperty<T> create() {
+                return new ObjectProperty<T>(elementSize);
             }
         };
     }
     public Object[] getIndexedData() {
         return store.array;
     }
+
+    /**
+     * Get the value stored in this property for the given component index, and
+     * offset. Offset is measured from 0 to 1 minus the element size the
+     * property was originally created with.
+     * 
+     * @param componentIndex The component's index
+     * @param offset The offset into the component's data
+     * @return The object at the given offset for the given component
+     * @throws ArrayIndexOutOfBoundsException if the componentIndex and offset
+     *             would access illegal indices
+     */
+    @SuppressWarnings("unchecked")
+    public T get(int componentIndex, int offset) {
+        return (T) store.array[componentIndex * store.elementSize + offset];
+    }
+
+    /**
+     * Store <tt>val</tt> in this property for the given component index, at the
+     * specified offset. The offset is measured from 0 to 1 minus the element
+     * size that this property was originally created with.
+     * 
+     * @param val The value to store, can be null
+     * @param componentIndex The index of the component being modified
+     * @param offset The offset into the component's data
+     * @throws ArrayIndexOutOfBoundsException if the componentIndex and offset
+     *             would access illegal indices
+     */
+    public void set(T val, int componentIndex, int offset) {
+        store.array[componentIndex * store.elementSize] = val;
+    }
     
     @Override
     public IndexedDataStore getDataStore() {

src/test/java/com/googlecode/entreri/component/AbstractComponent.java

  */
 public abstract class AbstractComponent extends Component {
     @Parameter(type=int.class, value="1")
-    private ObjectProperty property;
+    private ObjectProperty<Object> property;
     
     protected AbstractComponent(EntitySystem system, int index) {
         super(system, index);

src/test/java/com/googlecode/entreri/component/ObjectComponent.java

  */
 public class ObjectComponent extends AbstractComponent {
     @Parameter(type=int.class, value="3")
-    private ObjectProperty property;
+    private ObjectProperty<Object> property;
     
     protected ObjectComponent(EntitySystem system, int index) {
         super(system, index);