Created
August 26, 2016 20:24
-
-
Save branflake2267/cd2d25d7bd723a716fcfbcad52c97a14 to your computer and use it in GitHub Desktop.
GWT Datastore Key and Text supersource for App Engine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.google.appengine.api.datastore; | |
import java.io.Serializable; | |
import java.util.Iterator; | |
import java.util.LinkedList; | |
public final class Key implements Serializable, Comparable<Key> { | |
static final long serialVersionUID = -448150158203091507L; | |
private Key parentKey; | |
private String kind; | |
private long id; | |
private String name; | |
@SuppressWarnings("unused") | |
private Key() { | |
parentKey = null; | |
kind = null; | |
id = 0; | |
name = null; | |
} | |
Key(String kind, String name) { | |
this(kind, null, name); | |
} | |
Key(String kind, Key parentKey) { | |
this(kind, parentKey, 0); | |
} | |
Key(String kind, Key parentKey, long id) { | |
this(kind, parentKey, id, null); | |
} | |
Key(String kind, Key parentKey, String name) { | |
this(kind, parentKey, 0, name); | |
} | |
Key(String kind, Key parentKey, long id, String name) { | |
if (kind == null || kind.length() == 0) throw new IllegalArgumentException("No kind specified."); | |
if (name != null) { | |
if (name.length() == 0) throw new IllegalArgumentException("Name may not be empty."); | |
if (id != 0) throw new IllegalArgumentException("Id and name may not both be specified at once."); | |
} | |
this.name = name; | |
this.id = id; | |
this.parentKey = parentKey; | |
this.kind = kind; | |
} | |
public String getKind() { | |
return kind; | |
} | |
public Key getParent() { | |
return parentKey; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (o == null || getClass() != o.getClass()) return false; | |
Key key = (Key) o; | |
if (id != key.id) return false; | |
if (!kind.equals(key.kind)) return false; | |
if (name != null ? !name.equals(key.name) : key.name != null) return false; | |
if (parentKey != null ? !parentKey.equals(key.parentKey) : key.parentKey != null) return false; | |
return true; | |
} | |
@Override | |
public int hashCode() { | |
int result = parentKey != null ? parentKey.hashCode() : 0; | |
result = 31 * result + kind.hashCode(); | |
result = 31 * result + (int) (id ^ (id >>> 32)); | |
result = 31 * result + (name != null ? name.hashCode() : 0); | |
return result; | |
} | |
public String toString() { | |
StringBuilder buffer = new StringBuilder(); | |
appendToString(buffer); | |
return buffer.toString(); | |
} | |
private void appendToString(StringBuilder buffer) { | |
if (parentKey != null) { | |
parentKey.appendToString(buffer); | |
buffer.append("/"); | |
} | |
buffer.append(kind); | |
buffer.append("("); | |
if (name != null) | |
buffer.append('"').append(name).append('"'); | |
else | |
buffer.append(id); | |
buffer.append(")"); | |
} | |
public long getId() { | |
return id; | |
} | |
public String getName() { | |
return name; | |
} | |
public Key getChild(String kind, long id) { | |
return new Key(kind, this, id); | |
} | |
public Key getChild(String kind, String name) { | |
return new Key(kind, this, name); | |
} | |
private static Iterator<Key> getPathIterator(Key key) { | |
LinkedList<Key> stack = new LinkedList<Key>(); | |
do { | |
stack.addFirst(key); | |
key = key.getParent(); | |
} while (key != null); | |
return stack.iterator(); | |
} | |
public int compareTo(Key other) { | |
if (this == other) return 0; | |
Iterator<Key> thisPath = getPathIterator(this); | |
Iterator<Key> otherPath = getPathIterator(other); | |
while (thisPath.hasNext()) { | |
Key thisKey = (Key) thisPath.next(); | |
if (otherPath.hasNext()) { | |
Key otherKey = (Key) otherPath.next(); | |
int result = compareToInternal(thisKey, otherKey); | |
if (result != 0) return result; | |
} else { | |
return 1; | |
} | |
} | |
return otherPath.hasNext() ? -1 : 0; | |
} | |
private static int compareToInternal(Key thisKey, Key otherKey) { | |
if (thisKey == otherKey) return 0; | |
int result = thisKey.getKind().compareTo(otherKey.getKind()); | |
if (result != 0) return result; | |
if (thisKey.getId() != 0) if (otherKey.getId() == 0) | |
return -1; | |
else | |
return Long.valueOf(thisKey.getId()).compareTo(Long.valueOf(otherKey.getId())); | |
if (otherKey.getId() != 0) | |
return 1; | |
else | |
return thisKey.getName().compareTo(otherKey.getName()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.google.gwt.user.client.rpc.core.com.google.appengine.api.datastore; | |
import com.google.appengine.api.datastore.Key; | |
import com.google.appengine.api.datastore.KeyFactory; | |
import com.google.gwt.user.client.rpc.CustomFieldSerializer; | |
import com.google.gwt.user.client.rpc.SerializationException; | |
import com.google.gwt.user.client.rpc.SerializationStreamReader; | |
import com.google.gwt.user.client.rpc.SerializationStreamWriter; | |
public class Key_CustomFieldSerializer extends CustomFieldSerializer<Key> { | |
public static void deserialize(SerializationStreamReader streamReader, Key instance) { | |
} | |
public static Key instantiate(SerializationStreamReader streamReader) throws SerializationException { | |
Key parent = (Key) streamReader.readObject(); | |
String kind = streamReader.readString(); | |
long id = streamReader.readLong(); | |
String name = streamReader.readString(); | |
if (name == null) { | |
return KeyFactory.createKey(parent, kind, id); | |
} else { | |
return KeyFactory.createKey(parent, kind, name); | |
} | |
} | |
public static void serialize(SerializationStreamWriter streamWriter, Key instance) throws SerializationException { | |
streamWriter.writeObject(instance.getParent()); | |
streamWriter.writeString(instance.getKind()); | |
streamWriter.writeLong(instance.getId()); | |
streamWriter.writeString(instance.getName()); | |
} | |
@Override | |
public boolean hasCustomInstantiateInstance() { | |
return true; | |
} | |
public Key instantiateInstance(SerializationStreamReader streamReader) throws SerializationException { | |
return instantiate(streamReader); | |
} | |
@Override | |
public void deserializeInstance(SerializationStreamReader streamReader, Key instance) throws SerializationException { | |
deserialize(streamReader, instance); | |
} | |
@Override | |
public void serializeInstance(SerializationStreamWriter streamWriter, Key instance) throws SerializationException { | |
serialize(streamWriter, instance); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.google.appengine.api.datastore; | |
/** | |
* GWT emulation class, with most of the methods from KeyFactory. Does not support the keyToString()/stringToKey() | |
* methods. | |
*/ | |
public class KeyFactory { | |
public static final class Builder { | |
public Builder addChild(String kind, String name) { | |
current = KeyFactory.createKey(current, kind, name); | |
return this; | |
} | |
public Builder addChild(String kind, long id) { | |
current = KeyFactory.createKey(current, kind, id); | |
return this; | |
} | |
public Key getKey() { | |
return current; | |
} | |
private Key current; | |
public Builder(String kind, String name) { | |
current = KeyFactory.createKey(null, kind, name); | |
} | |
public Builder(String kind, long id) { | |
current = KeyFactory.createKey(null, kind, id); | |
} | |
public Builder(Key key) { | |
current = key; | |
} | |
} | |
public static Key createKey(String kind, long id) { | |
return createKey(null, kind, id); | |
} | |
public static Key createKey(Key parent, String kind, long id) { | |
if (id == 0L) | |
throw new IllegalArgumentException("id cannot be zero"); | |
else | |
return new Key(kind, parent, id); | |
} | |
public static Key createKey(String kind, String name) { | |
return createKey(null, kind, name); | |
} | |
public static Key createKey(Key parent, String kind, String name) { | |
if (name == null || name.length() == 0) | |
throw new IllegalArgumentException("name cannot be null or empty"); | |
else | |
return new Key(kind, parent, name); | |
} | |
private KeyFactory() { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.7.0//EN" | |
"http://gwtproject.org/doctype/2.7.0/gwt-module.dtd"> | |
<module rename-to='WrightwayEconomy'> | |
<!-- Add this to point to the super source directory --> | |
<super-source path="gwtsupersource" /> | |
</module> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.google.appengine.api.datastore; | |
import java.io.Serializable; | |
/** | |
* GWT emulation class. | |
*/ | |
@SuppressWarnings("serial") | |
public class Text implements Serializable { | |
private final String value; | |
public Text(String value) { | |
this.value = value; | |
} | |
public String getValue() { | |
return value; | |
} | |
public int hashCode() { | |
return value.hashCode(); | |
} | |
public boolean equals(Object object) { | |
if (object instanceof Text) { | |
Text key = (Text) object; | |
return value.equals(key.value); | |
} else { | |
return false; | |
} | |
} | |
public String toString() { | |
String text = value; | |
if (text.length() > 70) text = text.substring(0, 70) + "..."; | |
return "<Text: " + text + ">"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.google.gwt.user.client.rpc.core.com.google.appengine.api.datastore; | |
import com.google.appengine.api.datastore.Text; | |
import com.google.gwt.user.client.rpc.SerializationException; | |
import com.google.gwt.user.client.rpc.SerializationStreamReader; | |
import com.google.gwt.user.client.rpc.SerializationStreamWriter; | |
public class Text_CustomFieldSerializer { | |
public static void deserialize(SerializationStreamReader streamReader, Text instance) throws SerializationException { | |
// already handled in instantiate | |
} | |
public static Text instantiate(SerializationStreamReader streamReader) throws SerializationException { | |
return new Text(streamReader.readString()); | |
} | |
public static void serialize(SerializationStreamWriter streamWriter, Text instance) throws SerializationException { | |
streamWriter.writeString(instance.getValue()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment