Michael Ludwig avatar Michael Ludwig committed 7359a64

Add an add() method to Entity that takes a template component, and fix documentation in EntitySystem.

Comments (0)

Files changed (2)

src/main/java/com/googlecode/entreri/Entity.java

      * @param componentId The TypedId of the component type
      * @param initParams The initialization parameters required for the
      *            component type
-     * @return A new component of type T, or an existing T if already attached
+     * @return A new component of type T
      * @throws NullPointerException if componentId is null
      * @throws IllegalArgumentException if any of the init params are invalid
      */
     }
 
     /**
+     * Add a Component of type T to this Entity, but clone its state from the
+     * existing component of type T. The existing component must still be
+     * attached to an Entity other than this entity, but it could be from a
+     * different EntitySystem. If there already exists a component of type T
+     * added to this entity, it is removed first, and a new one is instantiated.
+     * 
+     * @param <T> The parameterized type of component to add
+     * @param toClone The existing T to clone when attaching to this component
+     * @return A new component of type T
+     * @throws NullPointerException if toClone is null
+     */
+    @SuppressWarnings({ "unchecked", "rawtypes" })
+    public <T extends Component> T add(T toClone) {
+        if (toClone == null)
+            throw new NullPointerException("Component template, toClone, cannot be null");
+        ComponentIndex ci = system.getIndex(toClone.getTypedId());
+        return (T) ci.addComponent(index, toClone);
+    }
+
+    /**
      * Remove any attached Component of the given type, T, from this Entity.
      * True is returned if a component was removed, and false otherwise. If a
      * component is removed, the component should no longer be used and it will

src/main/java/com/googlecode/entreri/EntitySystem.java

      * <p>
      * The iterator returns the canonical Component instance for each component
      * of the type in the system. This is the same instance that was returned by
-     * {@link Entity#add(TypedId)} and is safe to access and store after
+     * {@link Entity#add(TypedId, Object...)} and is safe to access and store after
      * iteration has completed.
      * </p>
      * 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.