Skip to content

Instantly share code, notes, and snippets.

@isopov
Created January 2, 2013 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isopov/4436071 to your computer and use it in GitHub Desktop.
Save isopov/4436071 to your computer and use it in GitHub Desktop.
package com.sopovs.moradanen.gson;
public abstract class AbstractConfigPart implements ConfigElement {
private boolean uniqueOnly;
private int threshold;
public AbstractConfigPart() {
}
public AbstractConfigPart(boolean uniqueOnly, int threshold) {
this.uniqueOnly = uniqueOnly;
this.threshold = threshold;
}
public boolean isUniqueOnly() {
return uniqueOnly;
}
public void setUniqueOnly(boolean uniqueOnly) {
this.uniqueOnly = uniqueOnly;
}
public int getThreshold() {
return threshold;
}
public void setThreshold(int threshold) {
this.threshold = threshold;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + threshold;
result = prime * result + (uniqueOnly ? 1231 : 1237);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
AbstractConfigPart other = (AbstractConfigPart) obj;
if (threshold != other.threshold)
return false;
if (uniqueOnly != other.uniqueOnly)
return false;
return true;
}
}
package com.sopovs.moradanen.gson;
public enum CombinationOperation {
AND, OR;
}
package com.sopovs.moradanen.gson;
public interface ConfigElement {
}
package com.sopovs.moradanen.gson;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
public class ConfigParts implements ConfigElement {
private CombinationOperation combOperation;
private Collection<ConfigElement> elements;
public ConfigParts() {
}
public ConfigParts(CombinationOperation combOperation, ConfigElement first, ConfigElement second,
ConfigElement... otherElements) {
this.combOperation = combOperation;
elements = new ArrayList<ConfigElement>(otherElements.length + 2);
elements.add(first);
elements.add(second);
elements.addAll(Arrays.asList(otherElements));
}
public CombinationOperation getCombOperation() {
return combOperation;
}
public void setCombOperation(CombinationOperation combOperation) {
this.combOperation = combOperation;
}
public Collection<ConfigElement> getElements() {
return elements;
}
public void setElements(Collection<ConfigElement> elements) {
if (elements.size() < 2) {
throw new IllegalArgumentException();
}
this.elements = elements;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((combOperation == null) ? 0 : combOperation.hashCode());
result = prime * result + ((elements == null) ? 0 : elements.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ConfigParts other = (ConfigParts) obj;
if (combOperation != other.combOperation)
return false;
if (elements == null) {
if (other.elements != null)
return false;
} else if (!elements.equals(other.elements))
return false;
return true;
}
}
package com.sopovs.moradanen.gson;
import static com.sopovs.moradanen.gson.CombinationOperation.AND;
import static com.sopovs.moradanen.gson.CombinationOperation.OR;
import java.lang.reflect.Type;
import org.junit.Test;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
public class GsonTest {
@Test
public void test() {
Gson gson = new GsonBuilder()
.setPrettyPrinting()
.registerTypeAdapter(ConfigElement.class, new
JsonDeserializer<ConfigElement>() {
public ConfigElement deserialize(JsonElement json, Type
typeOfT, JsonDeserializationContext context)
throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
if (jsonObject.get("combOperation") != null) {
return context.deserialize(json, ConfigParts.class);
}
if (jsonObject.get("keyWord") != null) {
return context.deserialize(json, KeyWordConfigPart.class);
}
if (jsonObject.get("regex") != null) {
return context.deserialize(json, RegexConfigPart.class);
}
throw new IllegalArgumentException();
}
})
// This Serializer is needed only if Deserializer is specified
.registerTypeAdapter(ConfigElement.class, new
JsonSerializer<ConfigElement>() {
public JsonElement serialize(ConfigElement src, Type typeOfSrc,
JsonSerializationContext context) {
return context.serialize(src, src.getClass());
}
})
.create();
ConfigElement config = new ConfigParts(OR, new ConfigParts(AND, new RegexConfigPart(false, 1, "^[a-zA-Z]*"),
new KeyWordConfigPart(false, 5, "BarFoo")),
new KeyWordConfigPart(true, 2, "FooBar"));
String json = gson.toJson(config);
System.out.println(json);
System.out.println("Deserialization successful " + config.equals(gson.fromJson(json, ConfigElement.class)));
}
}
package com.sopovs.moradanen.gson;
public class KeyWordConfigPart extends AbstractConfigPart {
private String keyWord;
public KeyWordConfigPart() {
}
public KeyWordConfigPart(boolean uniqueOnly, int threshold, String keyWord) {
super(uniqueOnly, threshold);
this.keyWord = keyWord;
}
public String getKeyWord() {
return keyWord;
}
public void setKeyWord(String keyWord) {
this.keyWord = keyWord;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((keyWord == null) ? 0 : keyWord.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
KeyWordConfigPart other = (KeyWordConfigPart) obj;
if (keyWord == null) {
if (other.keyWord != null)
return false;
} else if (!keyWord.equals(other.keyWord))
return false;
return true;
}
}
package com.sopovs.moradanen.gson;
public class RegexConfigPart extends AbstractConfigPart {
private String regex;
public RegexConfigPart() {
}
public RegexConfigPart(boolean uniqueOnly, int threshold, String regex) {
super(uniqueOnly, threshold);
this.regex = regex;
}
public String getRegex() {
return regex;
}
public void setRegex(String regex) {
this.regex = regex;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((regex == null) ? 0 : regex.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
RegexConfigPart other = (RegexConfigPart) obj;
if (regex == null) {
if (other.regex != null)
return false;
} else if (!regex.equals(other.regex))
return false;
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment