Results 1 to 6 of 6
  1. #1
    thorne_ is offline Member
    Join Date
    Apr 2009
    Posts
    12
    Rep Power
    0

    Default [SOLVED] Deep copy

    Hi,

    Im trying to clone a list of objects in my application.

    this is the object hierarchy

    Java Code:
    public class M implements Cloneable, java.io.Serializable {
     
    	private static final long serialVersionUID = 1L;
     
    	private List<S> s;
    	private Map<String, Object> parameters;//getters included, sniped
    	
    	 @Override
    	public Object clone() {
    		try {
    		  return super.clone();
    		} catch (CloneNotSupportedException ex) {
    		  System.out.println("Cloning of M failed");
    		      throw new InternalError(ex.toString());
    		   }
    	  }
    	
    }


    this class includes a list of this class

    Java Code:
    public class S implements Cloneable, java.io.Serializable {
     
    	private static final long serialVersionUID = 1L;
    	
    	private Map<String, Object> parameters;
    	
    	
    	 @Override
    	public Object clone() {
    			try {
    			  return super.clone();
    			} catch (CloneNotSupportedException ex) {
    			  System.out.println("Cloning of Sfailed");
    			      throw new InternalError(ex.toString());
    			   }
    		  }
    }


    In my application i create an set of class M objects, each containing a list of S objects. This set only has default parameters. i need to be able to grab a copy of this set and modify it without effecting the original contents - this is why i implemented cloneable.

    This is my attempt to do this :

    Java Code:
    class ObjectDao{
     
    private static ArrayList<M> allMs = new ArrayList<M>();
     
    private void addM(M m){
    allMs.add(m);
    }
     
    private static ArrayList<M> getClonedMList(){
    		
    		ArrayList<M> clonedMs = new ArrayList<M>();
    		
    		try {
    			
    			for( int i = 0;i < allMs.size();i++ ) {
    			
    				M clonedM = (M)allMs.get(i).clone();
    				
    				clonedM.setParameters( (HashMap) 
    						( (HashMap) allMs.get(i).getParameters() ).clone() );
    			
    				
    				for(int j = 0;j < ( allMs.get(i).getSs().size() );j++) {
    				
    					S clonedS= (S) allMs.get(i).getSs().get(j).clone();
    						
    					clonedS.setParameters( (HashMap)
    							( (HashMap) allMs.get(j).getSs().get(i).getParameters() ).clone() );
    					
    					clonedM.getSs().set(j, clonedS);
    						
    				}
    				
    				clonedMs.add(clonedM);
    				
    			}
    			
    		}
    		catch (Exception e) {
    			log.error(e.toString());
    		}
    		
    		
    		return clonedMs;
    		
    	}

    I clone each object with the parent object individually but it doesnt have the desired effect i.e. if i modify an S's parameters (on an M received from this method) somewhere else in the application the next call to this method returns objects with the modified parameters instead of the original state

    Can anybody help?

  2. #2
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    444
    Rep Power
    6

    Default

    i think you have to read about how to ask questions gently?
    Mak
    (Living @ Virtual World)

  3. #3
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Your clone() methods have to work through your lists and maps and deep copy them too. super.clone() just performs a shallow copy of the the outer object.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  4. #4
    thorne_ is offline Member
    Join Date
    Apr 2009
    Posts
    12
    Rep Power
    0

    Default

    Is serialising and then deserialising an object the only way of performing a deep copy?

    This is simply too slow for what i need

  5. #5
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Not at all. Usually you just have to iterate through everything. Serialisation is for serialising things, not cloning them.

    Java Code:
    public class Example implements Cloneable {
        private Cloneable[] array;
        Map<String,Cloneable> map;
        int primitive;
        Cloneable object;
    
        public Example clone() {
            Example clone = new Example();
    
            clone.primitive = this.primitive;
            clone.object = this.object.clone();
    
            clone.array = new Cloneable[this.array.length];
            for (int i=0; i<array.length; i++) {
                clone.array[i] = this.array[i].clone();
            }
    
            clone.map = new Map<String,Cloneable>();
            for (String s : map.getKeys()) {
                clone.map.put(s, this.map.get(s).clone());
            }
    
            return clone;
        }
    }
    Note that Strings, and other immutable objects, don't need cloning.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  6. #6
    thorne_ is offline Member
    Join Date
    Apr 2009
    Posts
    12
    Rep Power
    0

Similar Threads

  1. Deep copy?!
    By deepthought015 in forum New To Java
    Replies: 3
    Last Post: 05-07-2009, 06:31 PM
  2. Help with Deep Cloning
    By jralexander137 in forum New To Java
    Replies: 6
    Last Post: 10-30-2008, 05:34 PM
  3. deep copying arraylist to add to a list
    By alvations in forum New To Java
    Replies: 13
    Last Post: 10-08-2008, 03:13 PM
  4. Deep Copy Test
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:05 PM
  5. Replies: 0
    Last Post: 12-28-2007, 12:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •