Simplify inner strategy in LongSerializationPolicy.

This commit is contained in:
Jesse Wilson 2011-11-20 20:16:46 +00:00
parent 1540201713
commit a00c5ff9f1

View File

@ -31,44 +31,28 @@ public enum LongSerializationPolicy {
* would be: * would be:
* {@code {"f":123}}. * {@code {"f":123}}.
*/ */
DEFAULT(new DefaultStrategy()), DEFAULT() {
public JsonElement serialize(Long value) {
return new JsonPrimitive(value);
}
},
/** /**
* Serializes a long value as a quoted string. For example, assume an object has a long field * Serializes a long value as a quoted string. For example, assume an object has a long field
* named "f" then the serialized output would be: * named "f" then the serialized output would be:
* {@code {"f":"123"}}. * {@code {"f":"123"}}.
*/ */
STRING(new StringStrategy()); STRING() {
public JsonElement serialize(Long value) {
return new JsonPrimitive(String.valueOf(value));
}
};
private final Strategy strategy;
private LongSerializationPolicy(Strategy strategy) {
this.strategy = strategy;
}
/** /**
* Serialize this {@code value} using this serialization policy. * Serialize this {@code value} using this serialization policy.
* *
* @param value the long value to be serialized into a {@link JsonElement} * @param value the long value to be serialized into a {@link JsonElement}
* @return the serialized version of {@code value} * @return the serialized version of {@code value}
*/ */
public JsonElement serialize(Long value) { public abstract JsonElement serialize(Long value);
return strategy.serialize(value);
}
private interface Strategy {
JsonElement serialize(Long value);
}
private static class DefaultStrategy implements Strategy {
public JsonElement serialize(Long value) {
return new JsonPrimitive(value);
}
}
private static class StringStrategy implements Strategy {
public JsonElement serialize(Long value) {
return new JsonPrimitive(String.valueOf(value));
}
}
} }