Splitter-NG CodingTask: add deepCopy functionality for metadata map
authorMaximilian Quellmalz <maximilian.quellmalz@mailbox.tu-dresden.de>
Mon, 3 Nov 2014 13:53:24 +0000 (14:53 +0100)
committerMaximilian Quellmalz <maximilian.quellmalz@mailbox.tu-dresden.de>
Mon, 3 Nov 2014 13:53:24 +0000 (14:53 +0100)
Splitter-ng/src/splitterng/CodingTask.java

index e7fc65a..7a46590 100644 (file)
@@ -1,5 +1,10 @@
 package splitterng;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 import java.nio.file.Path;
 import java.util.Map;
 
@@ -107,6 +112,11 @@ public abstract class CodingTask {
        public Map<String, Object> getMetadata() {
                return metadataMap;
        }
+       
+       public Map<String, Object> getMetadataAsDeepCopy() throws Exception {
+               return CodingTask.deepCopy(metadataMap);
+       }
+       
        public Object getMetadata(String key) {
                return metadataMap.get(key);
        }
@@ -115,11 +125,25 @@ public abstract class CodingTask {
                this.metadataMap = metadataMap;
        }
        
+       public void setMetadataMapAsDeepCopy(Map<String, Object> metadataMap) throws Exception {
+               this.metadataMap = CodingTask.deepCopy(metadataMap);
+       }
+       
        public void setMetadata(String name, Object object) {
                this.metadataMap.put(name, object);
        }
 
+       @SuppressWarnings("unchecked")
+       public static <T> T deepCopy( T o ) throws Exception
+       {
+         ByteArrayOutputStream baos = new ByteArrayOutputStream();
+         new ObjectOutputStream( baos ).writeObject( o );
+
+         ByteArrayInputStream bais = new ByteArrayInputStream( baos.toByteArray() );
+         Object p = new ObjectInputStream( bais ).readObject();
 
+         return (T) p;
+       }
 //     public boolean isEncodingTask() {
 //             return isEncodingTask;
 //     }