Profile: Implement deep copying
This is a simple, naive implementation that {,de}serializes the
profile's state, but it does not depend on the internal representation
of Profile or its contained classes.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
			
			
This commit is contained in:
		
							parent
							
								
									c3b42b85cc
								
							
						
					
					
						commit
						ca708ba382
					
				@ -19,7 +19,7 @@ import java.nio.charset.StandardCharsets;
 | 
				
			|||||||
 * Represents a wg-quick profile.
 | 
					 * Represents a wg-quick profile.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class Profile extends BaseObservable implements Observable {
 | 
					public class Profile extends BaseObservable implements Copyable<Profile>, Observable {
 | 
				
			||||||
    private final Interface iface = new Interface();
 | 
					    private final Interface iface = new Interface();
 | 
				
			||||||
    private boolean isConnected;
 | 
					    private boolean isConnected;
 | 
				
			||||||
    private final String name;
 | 
					    private final String name;
 | 
				
			||||||
@ -30,6 +30,22 @@ public class Profile extends BaseObservable implements Observable {
 | 
				
			|||||||
        this.name = name;
 | 
					        this.name = name;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private Profile(Profile original)
 | 
				
			||||||
 | 
					            throws IOException {
 | 
				
			||||||
 | 
					        this(original.getName());
 | 
				
			||||||
 | 
					        final byte configBytes[] = original.toString().getBytes(StandardCharsets.UTF_8);
 | 
				
			||||||
 | 
					        final ByteArrayInputStream configStream = new ByteArrayInputStream(configBytes);
 | 
				
			||||||
 | 
					        parseFrom(configStream);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public Profile copy() {
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
 | 
					            return new Profile(this);
 | 
				
			||||||
 | 
					        } catch (IOException e) {
 | 
				
			||||||
 | 
					            return null;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public Interface getInterface() {
 | 
					    public Interface getInterface() {
 | 
				
			||||||
        return iface;
 | 
					        return iface;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user