From fad10344277f9fb525c1f76f875e5f65833bfb09 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Mon, 2 Jan 2012 10:42:47 -0800 Subject: [PATCH] updated glesys to 2.1 style type adapters --- .../glesys/config/GleSYSParserModule.java | 27 ++++++------- .../internal/CustomDeserializers.java | 34 ---------------- .../internal/GleSYSTypeAdapters.java | 40 +++++++++++++++++++ .../functions/internal/GlesysDateAdapter.java | 27 +++++++------ 4 files changed, 68 insertions(+), 60 deletions(-) delete mode 100644 sandbox-providers/glesys/src/main/java/org/jclouds/glesys/functions/internal/CustomDeserializers.java create mode 100644 sandbox-providers/glesys/src/main/java/org/jclouds/glesys/functions/internal/GleSYSTypeAdapters.java diff --git a/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/config/GleSYSParserModule.java b/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/config/GleSYSParserModule.java index a979df0d45..1818569ea2 100644 --- a/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/config/GleSYSParserModule.java +++ b/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/config/GleSYSParserModule.java @@ -18,19 +18,20 @@ */ package org.jclouds.glesys.config; +import java.lang.reflect.Type; +import java.util.Map; + +import javax.inject.Singleton; + +import org.jclouds.glesys.domain.ServerState; +import org.jclouds.glesys.domain.ServerUptime; +import org.jclouds.glesys.functions.internal.GleSYSTypeAdapters; +import org.jclouds.glesys.functions.internal.GlesysDateAdapter; +import org.jclouds.json.config.GsonModule.DateAdapter; + import com.google.common.collect.ImmutableMap; import com.google.inject.AbstractModule; import com.google.inject.Provides; -import org.jclouds.glesys.domain.ServerState; -import org.jclouds.glesys.domain.ServerUptime; -import org.jclouds.glesys.functions.internal.CustomDeserializers; -import org.jclouds.glesys.functions.internal.GlesysDateAdapter; -import org.jclouds.json.config.GsonModule; -import org.jclouds.json.config.GsonModule.DateAdapter; - -import javax.inject.Singleton; -import java.lang.reflect.Type; -import java.util.Map; /** * @author Adrian Cole @@ -40,10 +41,8 @@ public class GleSYSParserModule extends AbstractModule { @Provides @Singleton public Map provideCustomAdapterBindings() { - return ImmutableMap.of( - ServerState.class, new CustomDeserializers.ServerStateAdapter(), - ServerUptime.class, new CustomDeserializers.ServerUptimeAdaptor() - ); + return ImmutableMap. of(ServerState.class, new GleSYSTypeAdapters.ServerStateAdapter(), + ServerUptime.class, new GleSYSTypeAdapters.ServerUptimeAdapter()); } @Override diff --git a/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/functions/internal/CustomDeserializers.java b/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/functions/internal/CustomDeserializers.java deleted file mode 100644 index dd4b477a9f..0000000000 --- a/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/functions/internal/CustomDeserializers.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.jclouds.glesys.functions.internal; - -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonDeserializer; -import com.google.gson.JsonElement; -import com.google.gson.JsonParseException; -import org.jclouds.glesys.domain.ServerState; -import org.jclouds.glesys.domain.ServerUptime; - -import java.lang.reflect.Type; - -/** - * @author Adam Lowe - */ -public class CustomDeserializers { - - public static class ServerStateAdapter implements JsonDeserializer { - @Override - public ServerState deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) - throws JsonParseException { - String toParse = jsonElement.getAsJsonPrimitive().getAsString(); - return ServerState.fromValue(toParse); - } - } - - public static class ServerUptimeAdaptor implements JsonDeserializer { - @Override - public ServerUptime deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) - throws JsonParseException { - String toParse = jsonElement.getAsJsonPrimitive().getAsString(); - return ServerUptime.fromValue(toParse); - } - } -} \ No newline at end of file diff --git a/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/functions/internal/GleSYSTypeAdapters.java b/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/functions/internal/GleSYSTypeAdapters.java new file mode 100644 index 0000000000..52c6e0e095 --- /dev/null +++ b/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/functions/internal/GleSYSTypeAdapters.java @@ -0,0 +1,40 @@ +package org.jclouds.glesys.functions.internal; + +import java.io.IOException; + +import org.jclouds.glesys.domain.ServerState; +import org.jclouds.glesys.domain.ServerUptime; + +import com.google.gson.TypeAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; + +/** + * @author Adam Lowe + */ +public class GleSYSTypeAdapters { + + public static class ServerStateAdapter extends TypeAdapter { + @Override + public void write(JsonWriter writer, ServerState value) throws IOException { + writer.value(value.value()); + } + + @Override + public ServerState read(JsonReader reader) throws IOException { + return ServerState.fromValue(reader.nextString()); + } + } + + public static class ServerUptimeAdapter extends TypeAdapter { + @Override + public void write(JsonWriter writer, ServerUptime value) throws IOException { + writer.value(value.toString()); + } + + @Override + public ServerUptime read(JsonReader reader) throws IOException { + return ServerUptime.fromValue(reader.nextString()); + } + } +} \ No newline at end of file diff --git a/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/functions/internal/GlesysDateAdapter.java b/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/functions/internal/GlesysDateAdapter.java index 6568b658ab..fff23e4dc3 100644 --- a/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/functions/internal/GlesysDateAdapter.java +++ b/sandbox-providers/glesys/src/main/java/org/jclouds/glesys/functions/internal/GlesysDateAdapter.java @@ -1,39 +1,42 @@ package org.jclouds.glesys.functions.internal; -import com.google.gson.*; -import org.jclouds.json.config.GsonModule; - -import javax.inject.Singleton; -import java.lang.reflect.Type; +import java.io.IOException; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; +import javax.inject.Singleton; + +import org.jclouds.json.config.GsonModule; + +import com.google.common.base.Throwables; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; + /** * Parser for Glesys Date formats * * @author Adam Lowe */ @Singleton -public class GlesysDateAdapter implements GsonModule.DateAdapter { +public class GlesysDateAdapter extends GsonModule.DateAdapter { private final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) { + public void write(JsonWriter writer, Date value) throws IOException { synchronized (dateFormat) { - return new JsonPrimitive(dateFormat.format(src)); + writer.value(dateFormat.format(value)); } } - public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) - throws JsonParseException { - String toParse = json.getAsJsonPrimitive().getAsString(); + public Date read(JsonReader reader) throws IOException { + String toParse = reader.nextString(); try { synchronized (dateFormat) { return dateFormat.parse(toParse); } } catch (ParseException e) { - throw new RuntimeException(e); + throw Throwables.propagate(e); } } }