Advertisement
tpeierls

SerializableHolder

May 10th, 2012
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.08 KB | None | 0 0
  1. import com.google.common.base.Supplier;
  2.  
  3. import com.fasterxml.jackson.annotation.JsonProperty;
  4. import com.fasterxml.jackson.annotation.JsonTypeInfo;
  5.  
  6.  
  7. /**
  8.  * Acts as a {@link com.hazelcast.nio.DataSerializable DataSerializable} wrapper
  9.  * for type that might not have default constructor.
  10.  */
  11. public final class SerializableHolder<T> extends JsonDataSerializable implements Supplier<T> {
  12.  
  13.     /**
  14.      * Wraps a value to make it serializable.
  15.      */
  16.     public static <T> SerializableHolder<T> of(T contents) {
  17.         return new SerializableHolder<T>(contents);
  18.     }
  19.  
  20.     /**
  21.      * Default constructor needed in order to be
  22.      * {@link com.hazelcast.nio.DataSerializable DataSerializable}.
  23.      */
  24.     public SerializableHolder() {
  25.         // Nothing to do.
  26.     }
  27.  
  28.     /**
  29.      * Retrieve the wrapped value.
  30.      */
  31.     @JsonTypeInfo(use=JsonTypeInfo.Id.CLASS)
  32.     @JsonProperty("x")
  33.     public T get() {
  34.         return contents;
  35.     }
  36.  
  37.  
  38.     SerializableHolder(T contents) {
  39.         this.contents = contents;
  40.     }
  41.  
  42.     @JsonProperty("x") private T contents;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement