126 lines
3.1 KiB
Java
Raw Normal View History

2015-05-05 11:20:13 -07:00
//: references/DeepCopy.java
2015-05-29 14:18:51 -07:00
// <20>2015 MindView LLC: see Copyright.txt
2015-05-05 11:20:13 -07:00
// Cloning a composed object.
2015-05-18 23:05:20 -07:00
// (Install libraries from www.junit.org)
2015-05-05 11:20:13 -07:00
import org.junit.Test;
2015-05-18 23:05:20 -07:00
import static org.junit.Assert.assertEquals;
2015-05-05 11:20:13 -07:00
class DepthReading implements Cloneable {
private double depth;
2015-05-18 23:05:20 -07:00
public DepthReading(double depth) {
this.depth = depth;
}
2015-05-05 11:20:13 -07:00
@Override
public Object clone() {
Object o = null;
try {
o = super.clone();
} catch(CloneNotSupportedException e) {
e.printStackTrace();
}
return o;
}
public double getDepth() { return depth; }
2015-05-18 23:05:20 -07:00
public void setDepth(double depth){
this.depth = depth;
}
2015-05-05 11:20:13 -07:00
@Override
2015-05-18 23:05:20 -07:00
public String toString() {
return String.valueOf(depth);
}
2015-05-05 11:20:13 -07:00
}
class TemperatureReading implements Cloneable {
private long time;
private double temperature;
2015-05-18 23:05:20 -07:00
public TemperatureReading(double temperature){
2015-05-05 11:20:13 -07:00
time = System.currentTimeMillis();
this.temperature = temperature;
}
@Override
public Object clone() {
Object o = null;
try {
o = super.clone();
} catch(CloneNotSupportedException e) {
e.printStackTrace();
}
return o;
}
2015-05-18 23:05:20 -07:00
public double getTemperature() {
return temperature;
}
public void setTemperature(double temp) {
this.temperature = temp;
2015-05-05 11:20:13 -07:00
}
@Override
public String toString() {
return String.valueOf(temperature);
}
}
class OceanReading implements Cloneable {
private DepthReading depth;
private TemperatureReading temperature;
2015-05-18 23:05:20 -07:00
public
OceanReading(double tdata, double ddata) {
2015-05-05 11:20:13 -07:00
temperature = new TemperatureReading(tdata);
depth = new DepthReading(ddata);
}
@Override
public Object clone() {
OceanReading o = null;
try {
o = (OceanReading)super.clone();
} catch(CloneNotSupportedException e) {
e.printStackTrace();
}
// Must clone references:
o.depth = (DepthReading)o.depth.clone();
o.temperature =
(TemperatureReading)o.temperature.clone();
return o; // Upcasts back to Object
}
public TemperatureReading getTemperatureReading() {
return temperature;
}
public void setTemperatureReading(TemperatureReading tr){
temperature = tr;
}
public DepthReading getDepthReading() { return depth; }
public void setDepthReading(DepthReading dr) {
this.depth = dr;
}
@Override
public String toString() {
return "temperature: " + temperature +
", depth: " + depth;
}
}
public class DeepCopy {
@Test
public void testClone() {
2015-05-18 23:05:20 -07:00
OceanReading reading =
new OceanReading(33.9, 100.5);
2015-05-05 11:20:13 -07:00
// Now clone it:
2015-05-18 23:05:20 -07:00
OceanReading clone =
(OceanReading)reading.clone();
TemperatureReading tr =
clone.getTemperatureReading();
2015-05-05 11:20:13 -07:00
tr.setTemperature(tr.getTemperature() + 1);
clone.setTemperatureReading(tr);
DepthReading dr = clone.getDepthReading();
dr.setDepth(dr.getDepth() + 1);
clone.setDepthReading(dr);
2015-05-18 23:05:20 -07:00
assertEquals(reading.toString(),
2015-05-05 11:20:13 -07:00
"temperature: 33.9, depth: 100.5");
2015-05-18 23:05:20 -07:00
assertEquals(clone.toString(),
2015-05-05 11:20:13 -07:00
"temperature: 34.9, depth: 101.5");
}
public static void main(String[] args) {
2015-05-18 23:05:20 -07:00
org.junit.runner.JUnitCore.runClasses(
DeepCopy.class);
2015-05-05 11:20:13 -07:00
}
} ///:~