2015-04-20 15:36:01 -07:00
|
|
|
|
//: generics/Mixins.java
|
2015-05-29 14:18:51 -07:00
|
|
|
|
// <20>2015 MindView LLC: see Copyright.txt
|
2015-04-20 15:36:01 -07:00
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
interface TimeStamped { long getStamp(); }
|
|
|
|
|
|
|
|
|
|
class TimeStampedImp implements TimeStamped {
|
|
|
|
|
private final long timeStamp;
|
|
|
|
|
public TimeStampedImp() {
|
|
|
|
|
timeStamp = new Date().getTime();
|
|
|
|
|
}
|
2015-05-05 11:20:13 -07:00
|
|
|
|
@Override
|
2015-04-20 15:36:01 -07:00
|
|
|
|
public long getStamp() { return timeStamp; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface SerialNumbered { long getSerialNumber(); }
|
|
|
|
|
|
|
|
|
|
class SerialNumberedImp implements SerialNumbered {
|
|
|
|
|
private static long counter = 1;
|
|
|
|
|
private final long serialNumber = counter++;
|
2015-05-05 11:20:13 -07:00
|
|
|
|
@Override
|
2015-04-20 15:36:01 -07:00
|
|
|
|
public long getSerialNumber() { return serialNumber; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface Basic {
|
2015-05-27 23:30:19 -07:00
|
|
|
|
void set(String val);
|
|
|
|
|
String get();
|
2015-04-20 15:36:01 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class BasicImp implements Basic {
|
|
|
|
|
private String value;
|
2015-05-05 11:20:13 -07:00
|
|
|
|
@Override
|
2015-04-20 15:36:01 -07:00
|
|
|
|
public void set(String val) { value = val; }
|
2015-05-05 11:20:13 -07:00
|
|
|
|
@Override
|
2015-04-20 15:36:01 -07:00
|
|
|
|
public String get() { return value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Mixin extends BasicImp
|
|
|
|
|
implements TimeStamped, SerialNumbered {
|
|
|
|
|
private TimeStamped timeStamp = new TimeStampedImp();
|
|
|
|
|
private SerialNumbered serialNumber =
|
|
|
|
|
new SerialNumberedImp();
|
2015-05-05 11:20:13 -07:00
|
|
|
|
@Override
|
2015-04-20 15:36:01 -07:00
|
|
|
|
public long getStamp() { return timeStamp.getStamp(); }
|
2015-05-05 11:20:13 -07:00
|
|
|
|
@Override
|
2015-04-20 15:36:01 -07:00
|
|
|
|
public long getSerialNumber() {
|
|
|
|
|
return serialNumber.getSerialNumber();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class Mixins {
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
Mixin mixin1 = new Mixin(), mixin2 = new Mixin();
|
|
|
|
|
mixin1.set("test string 1");
|
|
|
|
|
mixin2.set("test string 2");
|
|
|
|
|
System.out.println(mixin1.get() + " " +
|
|
|
|
|
mixin1.getStamp() + " " + mixin1.getSerialNumber());
|
|
|
|
|
System.out.println(mixin2.get() + " " +
|
|
|
|
|
mixin2.getStamp() + " " + mixin2.getSerialNumber());
|
|
|
|
|
}
|
|
|
|
|
} /* Output: (Sample)
|
|
|
|
|
test string 1 1132437151359 1
|
|
|
|
|
test string 2 1132437151359 2
|
|
|
|
|
*///:~
|