Skip to content

Commit a1813b5

Browse files
committed
Use the type variable D for all DynamicObject types
1 parent 9491a2f commit a1813b5

File tree

13 files changed

+57
-57
lines changed

13 files changed

+57
-57
lines changed

src/main/java/com/github/rschmitt/dynamicobject/DynamicObject.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import com.github.rschmitt.dynamicobject.internal.Instances;
1717
import com.github.rschmitt.dynamicobject.internal.Serialization;
1818

19-
public interface DynamicObject<T extends DynamicObject<T>> {
19+
public interface DynamicObject<D extends DynamicObject<D>> {
2020
/**
2121
* @return the underlying Clojure map backing this instance. Downcasting the return value of this method to any
2222
* particular Java type (e.g. IPersistentMap) is not guaranteed to work with future versions of Clojure.
@@ -27,7 +27,7 @@ public interface DynamicObject<T extends DynamicObject<T>> {
2727
* @return the apparent type of this instance. Note that {@code getClass} will return the class of the interface
2828
* proxy and not the interface itself.
2929
*/
30-
Class<T> getType();
30+
Class<D> getType();
3131

3232
/**
3333
* Invokes clojure.pprint/pprint, which writes a pretty-printed representation of the object to the currently bound
@@ -46,7 +46,7 @@ public interface DynamicObject<T extends DynamicObject<T>> {
4646
* <p/>
4747
* Equivalent to: {@code (merge-with (fn [a b] (if (nil? b) a b)) this other)}
4848
*/
49-
T merge(T other);
49+
D merge(D other);
5050

5151
/**
5252
* Recursively compares this instance with {@code other}, returning a new instance containing all of the common
@@ -55,22 +55,22 @@ public interface DynamicObject<T extends DynamicObject<T>> {
5555
* <p/>
5656
* Equivalent to: {@code (nth (clojure.data/diff this other) 2)}
5757
*/
58-
T intersect(T other);
58+
D intersect(D other);
5959

6060
/**
6161
* Recursively compares this instance with {@code other}, similar to {@link #intersect}, but returning the fields that
6262
* are unique to {@code this}. Uses the same recursion strategy as {@code intersect}.
6363
* <p/>
6464
* Equivalent to: {@code (nth (clojure.data/diff this other) 0)}
6565
*/
66-
T subtract(T other);
66+
D subtract(D other);
6767

6868
/**
6969
* Validate that all fields annotated with @Required are non-null, and that all present fields are of the correct
7070
* type. Returns the validated instance unchanged, which allows the validate method to be called at the end of a
7171
* fluent builder chain.
7272
*/
73-
T validate();
73+
D validate();
7474

7575
/**
7676
* Serialize the given object to Edn. Any {@code EdnTranslator}s that have been registered through
@@ -141,14 +141,14 @@ static <T> T fromFressianByteArray(byte[] bytes) {
141141
/**
142142
* Use the supplied {@code map} to back an instance of {@code type}.
143143
*/
144-
static <T extends DynamicObject<T>> T wrap(Object map, Class<T> type) {
144+
static <D extends DynamicObject<D>> D wrap(Object map, Class<D> type) {
145145
return Instances.wrap(map, type);
146146
}
147147

148148
/**
149149
* Create a "blank" instance of {@code type}, backed by an empty Clojure map. All fields will be null.
150150
*/
151-
static <T extends DynamicObject<T>> T newInstance(Class<T> type) {
151+
static <D extends DynamicObject<D>> D newInstance(Class<D> type) {
152152
return Instances.newInstance(type);
153153
}
154154

@@ -180,14 +180,14 @@ static <T> void deregisterType(Class<T> type) {
180180
* Register a reader tag for a DynamicObject type. This is useful for reading Edn representations of Clojure
181181
* records.
182182
*/
183-
static <T extends DynamicObject<T>> void registerTag(Class<T> type, String tag) {
183+
static <D extends DynamicObject<D>> void registerTag(Class<D> type, String tag) {
184184
Serialization.registerTag(type, tag);
185185
}
186186

187187
/**
188188
* Deregister the reader tag for the given DynamicObject type.
189189
*/
190-
static <T extends DynamicObject<T>> void deregisterTag(Class<T> type) {
190+
static <D extends DynamicObject<D>> void deregisterTag(Class<D> type) {
191191
Serialization.deregisterTag(type);
192192
}
193193

src/main/java/com/github/rschmitt/dynamicobject/internal/CustomValidationHook.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
import com.github.rschmitt.dynamicobject.DynamicObject;
44

5-
public interface CustomValidationHook<T extends DynamicObject<T>> {
6-
T $$customValidate();
5+
public interface CustomValidationHook<D extends DynamicObject<D>> {
6+
D $$customValidate();
77
}

src/main/java/com/github/rschmitt/dynamicobject/internal/DynamicObjectInstance.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@
1111
import com.github.rschmitt.dynamicobject.DynamicObject;
1212
import clojure.lang.AFn;
1313

14-
public abstract class DynamicObjectInstance<T extends DynamicObject<T>> implements CustomValidationHook<T> {
14+
public abstract class DynamicObjectInstance<D extends DynamicObject<D>> implements CustomValidationHook<D> {
1515
private static final Object Default = new Object();
1616
private static final Object Null = new Object();
1717

1818
Object map;
19-
Class<T> type;
19+
Class<D> type;
2020
private final ConcurrentHashMap valueCache = new ConcurrentHashMap();
2121

2222
public DynamicObjectInstance() {
2323
}
2424

25-
DynamicObjectInstance(Object map, Class<T> type) {
25+
DynamicObjectInstance(Object map, Class<D> type) {
2626
this.map = map;
2727
this.type = type;
2828
}
@@ -31,7 +31,7 @@ public Map getMap() {
3131
return (Map) map;
3232
}
3333

34-
public Class<T> getType() {
34+
public Class<D> getType() {
3535
return type;
3636
}
3737

@@ -62,7 +62,7 @@ public String toFormattedString() {
6262
return w.toString();
6363
}
6464

65-
public T merge(T other) {
65+
public D merge(D other) {
6666
AFn ignoreNulls = new AFn() {
6767
public Object invoke(Object arg1, Object arg2) {
6868
return (arg2 == null) ? arg1 : arg2;
@@ -72,33 +72,33 @@ public Object invoke(Object arg1, Object arg2) {
7272
return DynamicObject.wrap(mergedMap, type);
7373
}
7474

75-
public T intersect(T arg) {
75+
public D intersect(D arg) {
7676
return diff(arg, 2);
7777
}
7878

79-
public T subtract(T arg) {
79+
public D subtract(D arg) {
8080
return diff(arg, 0);
8181
}
8282

83-
private T diff(T arg, int idx) {
83+
private D diff(D arg, int idx) {
8484
Object array = ClojureStuff.Diff.invoke(map, arg.getMap());
8585
Object union = ClojureStuff.Nth.invoke(array, idx);
8686
if (union == null) union = ClojureStuff.EmptyMap;
8787
union = Metadata.withTypeMetadata(union, type);
8888
return DynamicObject.wrap(union, type);
8989
}
9090

91-
public T convertAndAssoc(Object key, Object value) {
91+
public D convertAndAssoc(Object key, Object value) {
9292
return assoc(key, Conversions.javaToClojure(value));
9393
}
9494

95-
public T assoc(Object key, Object value) {
95+
public D assoc(Object key, Object value) {
9696
if (value instanceof DynamicObject)
9797
value = ((DynamicObject) value).getMap();
9898
return DynamicObject.wrap(ClojureStuff.Assoc.invoke(map, key, value), type);
9999
}
100100

101-
public T assocMeta(Object key, Object value) {
101+
public D assocMeta(Object key, Object value) {
102102
return DynamicObject.wrap(ClojureStuff.VaryMeta.invoke(map, ClojureStuff.Assoc, key, value), type);
103103
}
104104

@@ -132,7 +132,7 @@ public Object getValueFor(Object key, Type genericReturnType) {
132132
return Conversions.clojureToJava(val, genericReturnType);
133133
}
134134

135-
public T validate(T self) {
135+
public D validate(D self) {
136136
Validation.validateInstance(this);
137137
return self;
138138
}

src/main/java/com/github/rschmitt/dynamicobject/internal/DynamicObjectInvocationHandler.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010

1111
import com.github.rschmitt.dynamicobject.DynamicObject;
1212

13-
class DynamicObjectInvocationHandler<T extends DynamicObject<T>> implements InvocationHandler {
13+
class DynamicObjectInvocationHandler<D extends DynamicObject<D>> implements InvocationHandler {
1414
private static final ConcurrentMap<Method, MethodHandle> methodHandleCache = new ConcurrentHashMap<>();
1515
private static final ConcurrentMap<Method, Invokee> invocationCache = new ConcurrentHashMap<>();
1616

17-
private final DynamicObjectInstance<T> instance;
17+
private final DynamicObjectInstance<D> instance;
1818

19-
DynamicObjectInvocationHandler(DynamicObjectInstance<T> instance) {
19+
DynamicObjectInvocationHandler(DynamicObjectInstance<D> instance) {
2020
this.instance = instance;
2121
}
2222

@@ -32,7 +32,7 @@ private Invokee getInvocation(Method method) {
3232
if (method.isDefault()) {
3333
if (methodName.equals("validate")) {
3434
return (instance, p, a) -> {
35-
instance.validate((T) p);
35+
instance.validate((D) p);
3636
return invokeDefaultMethod(p, method, a);
3737
};
3838
}
@@ -53,10 +53,10 @@ private Invokee getInvocation(Method method) {
5353
case "hashCode": return (instance, p, a) -> instance.hashCode();
5454
case "prettyPrint": return (instance, p, a) -> { instance.prettyPrint(); return null; };
5555
case "toFormattedString": return (instance, p, a) -> instance.toFormattedString();
56-
case "merge": return (instance, p, a) -> instance.merge((T) a[0]);
57-
case "intersect": return (instance, p, a) -> instance.intersect((T) a[0]);
58-
case "subtract": return (instance, p, a) -> instance.subtract((T) a[0]);
59-
case "validate": return (instance, p, a) -> instance.validate((T) p);
56+
case "merge": return (instance, p, a) -> instance.merge((D) a[0]);
57+
case "intersect": return (instance, p, a) -> instance.intersect((D) a[0]);
58+
case "subtract": return (instance, p, a) -> instance.subtract((D) a[0]);
59+
case "validate": return (instance, p, a) -> instance.validate((D) p);
6060
case "equals": return (instance, p, a) -> instance.equals(a[0]);
6161
default:
6262
if (Reflection.isMetadataGetter(method))

src/main/java/com/github/rschmitt/dynamicobject/internal/EdnSerialization.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,14 @@ public static synchronized <T> void deregisterType(Class<T> type) {
119119
translatorCache.remove(type);
120120
}
121121

122-
public static synchronized <T extends DynamicObject<T>> void registerTag(Class<T> type, String tag) {
122+
public static synchronized <D extends DynamicObject<D>> void registerTag(Class<D> type, String tag) {
123123
recordTagCache.put(type, tag);
124124
translators.getAndUpdate(translators -> ClojureStuff.Assoc.invoke(translators, ClojureStuff.cachedRead(
125125
tag), new RecordReader<>(type)));
126126
definePrintMethod(":" + type.getTypeName(), "RecordPrinter/printRecord", tag);
127127
}
128128

129-
public static synchronized <T extends DynamicObject<T>> void deregisterTag(Class<T> type) {
129+
public static synchronized <D extends DynamicObject<D>> void deregisterTag(Class<D> type) {
130130
String tag = recordTagCache.get(type);
131131
translators.getAndUpdate(translators -> ClojureStuff.Dissoc.invoke(translators, ClojureStuff.cachedRead(tag)));
132132
recordTagCache.remove(type);

src/main/java/com/github/rschmitt/dynamicobject/internal/FressianSerialization.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ static synchronized <T> void deregisterType(Class<T> type) {
9292
}
9393
}
9494

95-
static synchronized <T extends DynamicObject<T>> void registerTag(Class<T> type, String tag) {
95+
static synchronized <D extends DynamicObject<D>> void registerTag(Class<D> type, String tag) {
9696
binaryTagCache.put(type, tag);
9797
Handlers.installHandler(fressianWriteHandlers, type, tag, new FressianWriteHandler(type, tag));
9898
fressianReadHandlers.putIfAbsent(tag, new FressianReadHandler(type));
9999
}
100100

101-
static synchronized <T extends DynamicObject<T>> void deregisterTag(Class<T> type) {
101+
static synchronized <D extends DynamicObject<D>> void deregisterTag(Class<D> type) {
102102
String tag = binaryTagCache.get(type);
103103
fressianWriteHandlers.remove(type);
104104
fressianReadHandlers.remove(tag);

src/main/java/com/github/rschmitt/dynamicobject/internal/Instances.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class Instances {
1313
private static final ConcurrentMap<Class, DynamicProxy> proxyCache = new ConcurrentHashMap<>();
1414
public static boolean USE_INVOKEDYNAMIC = true;
1515

16-
public static <T extends DynamicObject<T>> T newInstance(Class<T> type) {
16+
public static <D extends DynamicObject<D>> D newInstance(Class<D> type) {
1717
return wrap(Metadata.withTypeMetadata(EmptyMap, type), type);
1818
}
1919

@@ -44,14 +44,14 @@ private static <T> T createIndyProxy(Object map, Class<T> type) {
4444
}
4545
}
4646

47-
private static <T, U extends DynamicObject<U>> T createReflectionProxy(Object map, Class<T> type) {
48-
DynamicObjectInstance<U> instance = new DynamicObjectInstance<U>() {
47+
private static <T, D extends DynamicObject<D>> T createReflectionProxy(Object map, Class<T> type) {
48+
DynamicObjectInstance<D> instance = new DynamicObjectInstance<D>() {
4949
@Override
50-
public U $$customValidate() {
50+
public D $$customValidate() {
5151
return null;
5252
}
5353
};
54-
instance.type = (Class<U>) type;
54+
instance.type = (Class<D>) type;
5555
instance.map = map;
5656
return (T) Proxy.newProxyInstance(
5757
Thread.currentThread().getContextClassLoader(),

src/main/java/com/github/rschmitt/dynamicobject/internal/RecordReader.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import com.github.rschmitt.dynamicobject.DynamicObject;
44
import clojure.lang.AFn;
55

6-
public final class RecordReader<T extends DynamicObject<T>> extends AFn {
7-
private final Class<T> type;
6+
public final class RecordReader<D extends DynamicObject<D>> extends AFn {
7+
private final Class<D> type;
88

9-
RecordReader(Class<T> type) {
9+
RecordReader(Class<D> type) {
1010
this.type = type;
1111
}
1212

src/main/java/com/github/rschmitt/dynamicobject/internal/Reflection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
import com.github.rschmitt.dynamicobject.Required;
1919

2020
class Reflection {
21-
static <T extends DynamicObject<T>> Collection<Method> requiredFields(Class<T> type) {
21+
static <D extends DynamicObject<D>> Collection<Method> requiredFields(Class<D> type) {
2222
Collection<Method> fields = fieldGetters(type);
2323
return fields.stream().filter(Reflection::isRequired).collect(Collectors.toSet());
2424
}
2525

26-
static <T extends DynamicObject<T>> Collection<Method> fieldGetters(Class<T> type) {
26+
static <D extends DynamicObject<D>> Collection<Method> fieldGetters(Class<D> type) {
2727
Collection<Method> ret = new LinkedHashSet<>();
2828
for (Method method : type.getDeclaredMethods())
2929
if (method.getParameterCount() == 0 && !method.isDefault() && !isMetadataGetter(method))

src/main/java/com/github/rschmitt/dynamicobject/internal/Serialization.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ public static synchronized <T> void deregisterType(Class<T> type) {
1414
FressianSerialization.deregisterType(type);
1515
}
1616

17-
public static synchronized <T extends DynamicObject<T>> void registerTag(Class<T> type, String tag) {
17+
public static synchronized <D extends DynamicObject<D>> void registerTag(Class<D> type, String tag) {
1818
EdnSerialization.registerTag(type, tag);
1919
FressianSerialization.registerTag(type, tag);
2020
}
2121

22-
public static synchronized <T extends DynamicObject<T>> void deregisterTag(Class<T> type) {
22+
public static synchronized <D extends DynamicObject<D>> void deregisterTag(Class<D> type) {
2323
EdnSerialization.deregisterTag(type);
2424
FressianSerialization.deregisterTag(type);
2525
}

0 commit comments

Comments
 (0)