Prechádzať zdrojové kódy

implement the JmeCloneable interface for CompactArray

Stephen Gold 7 rokov pred
rodič
commit
fc07b541b8

+ 47 - 1
jme3-core/src/main/java/com/jme3/animation/CompactArray.java

@@ -31,6 +31,8 @@
  */
 package com.jme3.animation;
 
+import com.jme3.util.clone.Cloner;
+import com.jme3.util.clone.JmeCloneable;
 import java.lang.reflect.Array;
 import java.util.HashMap;
 import java.util.Map;
@@ -40,7 +42,7 @@ import java.util.Map;
  * @author Lim, YongHoon
  * @param <T>
  */
-public abstract class CompactArray<T> {
+public abstract class CompactArray<T> implements JmeCloneable {
 
     private Map<T, Integer> indexPool = new HashMap<T, Integer>();
     protected int[] index;
@@ -68,6 +70,7 @@ public abstract class CompactArray<T> {
      * They are serialized automatically when get() method is called.
      * @param objArray
      */
+    @SuppressWarnings("unchecked")
     public void add(T... objArray) {
         if (objArray == null || objArray.length == 0) {
             return;
@@ -190,6 +193,7 @@ public abstract class CompactArray<T> {
      * @param objArray
      * @return 
      */
+    @SuppressWarnings("unchecked")
     public final int[] getIndex(T... objArray) {
         int[] index = new int[objArray.length];
         for (int i = 0; i < index.length; i++) {
@@ -228,6 +232,7 @@ public abstract class CompactArray<T> {
      * decompress and return object array
      * @return decompress and return object array
      */
+    @SuppressWarnings("unchecked")
     public final T[] toObjectArray() {
         try {
             T[] compactArr = (T[]) Array.newInstance(getElementClass(), getSerializedSize() / getTupleSize());
@@ -247,6 +252,47 @@ public abstract class CompactArray<T> {
         }
     }
 
+    /**
+     * Create a deep clone of this array.
+     *
+     * @return a new array
+     * @throws java.lang.CloneNotSupportedException
+     */
+    @Override
+    public Object clone() throws CloneNotSupportedException {
+        return Cloner.deepClone(this);
+    }
+
+    /**
+     * Create a shallow clone for the JME cloner.
+     *
+     * @return a new array
+     */
+    @Override
+    public Object jmeClone() {
+        try {
+            return super.clone();
+        } catch (CloneNotSupportedException exception) {
+            throw new RuntimeException("Can't clone array", exception);
+        }
+    }
+
+    /**
+     * Callback from {@link com.jme3.util.clone.Cloner} to convert this
+     * shallow-cloned array into a deep-cloned one, using the specified cloner
+     * to resolve copied fields.
+     *
+     * @param cloner the cloner currently cloning this control (not null)
+     * @param original the array from which this array was shallow-cloned
+     * (unused)
+     */
+    @Override
+    public void cloneFields(Cloner cloner, Object original) {
+        indexPool = cloner.clone(indexPool);
+        index = cloner.clone(index);
+        array = cloner.clone(array);
+    }
+
     /**
      * serialize object
      * @param compactIndex compacted object index