Skip to content

Commit e786432

Browse files
committed
refactored AnyTemplate, GenericCollectionTemplate and TemplateRegistry classes
1 parent 11df915 commit e786432

3 files changed

Lines changed: 8 additions & 22 deletions

File tree

src/main/java/org/msgpack/template/AnyTemplate.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,6 @@
2525

2626

2727
public class AnyTemplate<T> extends AbstractTemplate<T> {
28-
/*@SuppressWarnings("rawtypes")
29-
private static AnyTemplate INSTANCE = null;
30-
31-
@SuppressWarnings("rawtypes")
32-
public static AnyTemplate getInstance(TemplateRegistry registry) {
33-
if (INSTANCE == null) {
34-
INSTANCE = new AnyTemplate(registry);
35-
}
36-
return INSTANCE;
37-
}*/
3828

3929
private TemplateRegistry registry;
4030

@@ -44,9 +34,9 @@ public AnyTemplate(TemplateRegistry registry) {
4434

4535
@SuppressWarnings("unchecked")
4636
public void write(Packer pk, T target, boolean required) throws IOException {
47-
if(target instanceof Value) {
37+
if (target instanceof Value) {
4838
pk.write((Value) target);
49-
} else if(target == null) {
39+
} else if (target == null) {
5040
if(required) {
5141
throw new MessageTypeException("Attempted to write null");
5242
}
@@ -57,11 +47,11 @@ public void write(Packer pk, T target, boolean required) throws IOException {
5747
}
5848

5949
public T read(Unpacker u, T to, boolean required) throws IOException, MessageTypeException {
60-
if(!required && u.trySkipNil()) {
50+
if (!required && u.trySkipNil()) {
6151
return null;
6252
}
6353
T o = u.read(to);
64-
if(required && o == null) {
54+
if (required && o == null) {
6555
throw new MessageTypeException("Unexpected nil value");
6656
}
6757
return o;

src/main/java/org/msgpack/template/GenericCollectionTemplate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class GenericCollectionTemplate implements GenericTemplate {
2929
public GenericCollectionTemplate(TemplateRegistry registry, Class<? extends Template> tmpl) {
3030
try {
3131
constructor = tmpl.getConstructor(new Class<?>[]{ Template.class });
32-
constructor.newInstance(new Object[]{ new AnyTemplate(registry)});//AnyTemplate.getInstance(registry) });
32+
constructor.newInstance(new Object[]{ new AnyTemplate(registry)});
3333
} catch (NoSuchMethodException e) {
3434
throw new IllegalArgumentException(e);
3535
} catch (InvocationTargetException e) {

src/main/java/org/msgpack/template/TemplateRegistry.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ public TemplateRegistry(TemplateRegistry registry) {
8888
}
8989
chain = createTemplateBuilderChain();
9090
cache = new HashMap<Type, Template<Type>>();
91-
//genericCache = parent.genericCache;
9291
genericCache = new HashMap<Type, GenericTemplate>();
9392
registerTemplatesWhichRefersRegistry();
9493
}
@@ -103,7 +102,6 @@ public void setClassLoader(final ClassLoader cl) {
103102
}
104103

105104
private void registerTemplates() {
106-
107105
register(boolean.class, BooleanTemplate.getInstance());
108106
register(Boolean.class, BooleanTemplate.getInstance());
109107
register(byte.class, ByteTemplate.getInstance());
@@ -136,14 +134,12 @@ private void registerTemplates() {
136134

137135
}
138136

139-
140137
protected void registerTemplatesWhichRefersRegistry() {
141138
AnyTemplate anyTemplate = new AnyTemplate(this);
142139

143-
register(List.class, new ListTemplate(anyTemplate));//new ListTemplate(AnyTemplate.getInstance(this)));
144-
register(Collection.class,new CollectionTemplate(anyTemplate));//new CollectionTemplate(AnyTemplate.getInstance(this)));
145-
register(Map.class,new MapTemplate(anyTemplate,anyTemplate));//new MapTemplate(AnyTemplate.getInstance(this), AnyTemplate.getInstance(this)));
146-
140+
register(List.class, new ListTemplate(anyTemplate));
141+
register(Collection.class, new CollectionTemplate(anyTemplate));
142+
register(Map.class, new MapTemplate(anyTemplate,anyTemplate));
147143
registerGeneric(List.class, new GenericCollectionTemplate(this, ListTemplate.class));
148144
registerGeneric(Collection.class, new GenericCollectionTemplate(this, CollectionTemplate.class));
149145
registerGeneric(Map.class, new GenericMapTemplate(this, MapTemplate.class));

0 commit comments

Comments
 (0)