This is an automated email from the git hooks/post-receive script. tjaalton pushed a commit to branch master in repository jackson-annotations.
commit 5c5dd56f7e1a500c4d6af7157670a488e80d5125 Author: Tatu Saloranta <[email protected]> Date: Thu Feb 9 11:02:57 2012 -0800 Last mods for ObjectId generation? --- .../jackson/annotation/ObjectIdGenerator.java | 96 ++++++++++++++-------- .../jackson/annotation/ObjectIdGenerators.java | 89 ++++++-------------- 2 files changed, 88 insertions(+), 97 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/annotation/ObjectIdGenerator.java b/src/main/java/com/fasterxml/jackson/annotation/ObjectIdGenerator.java index 0b86233..995c522 100644 --- a/src/main/java/com/fasterxml/jackson/annotation/ObjectIdGenerator.java +++ b/src/main/java/com/fasterxml/jackson/annotation/ObjectIdGenerator.java @@ -17,6 +17,15 @@ public abstract class ObjectIdGenerator<T> */ public abstract Class<?> getScope(); + + /** + * Method called to check whether this generator instance can + * be used for Object Ids of specific generator type and + * scope. + * + * @return True if this instance can be used as-is; false if not + */ + public abstract boolean canUseFor(ObjectIdGenerator<?> gen); /* /********************************************************** @@ -32,27 +41,16 @@ public abstract class ObjectIdGenerator<T> /** * Factory method called to create a new instance to use for - * serialization. This includes initializing storage for keeping - * track of serialized instances, along with id used. + * serialization: needed since generators may have state + * (next id to produce) */ public abstract ObjectIdGenerator<T> newForSerialization(); /** - * Factory method called to create a new instance to use for - * serialization. This includes initializing storage for keeping - * track of deserialized instances, along with id used. + * Method for constructing key to use for ObjectId-to-POJO maps. */ - public abstract ObjectIdGenerator<T> newForDeserialization(); + public abstract IdKey key(Object key); - /** - * Method called to check whether this generator instance can - * be used for Object Ids of specific generator type and - * scope. - * - * @return True if this instance can be used as-is; false if not - */ - public abstract boolean canUseFor(ObjectIdGenerator<?> gen); - /* /********************************************************** /* Methods for serialization @@ -60,12 +58,6 @@ public abstract class ObjectIdGenerator<T> */ /** - * Method used during serialization, to try to find an Object Id for given already serialized - * Object: if none found, returns null. - */ - public abstract T findId(Object forPojo); - - /** * Method used for generating a new Object Identifier to serialize * for given POJO. * @@ -74,23 +66,63 @@ public abstract class ObjectIdGenerator<T> * @return Object Identifier to use. */ public abstract T generateId(Object forPojo); - + /* /********************************************************** - /* Methods for deserialization + /* Helper classes /********************************************************** */ - - /** - * Method used during deserialization, to try to find an item for which given - * id was used. - */ - public abstract Object findItem(T id); /** - * Method called during deserialization to establishing mapping between - * given item, and the id it was using. + * Simple key class that can be used as a key for + * ObjectId-to-POJO mappings, when multiple ObjectId types + * and scopes are used. */ - public abstract void addItem(Object item, T id); + public final static class IdKey + { + /** + * Type of {@link ObjectIdGenerator} used for generating Object Id + */ + private final Class<?> type; + + /** + * Scope of the Object Id (may be null, to denote global) + */ + private final Class<?> scope; + + /** + * Object for which Object Id was generated: can NOT be null. + */ + private final Object key; + + /** + * Hash code + */ + private final int hashCode; + + public IdKey(Class<?> type, Class<?> scope, Object key) { + this.type = type; + this.scope = scope; + this.key = key; + + int h = key.hashCode() + type.getName().hashCode(); + if (scope != null) { + h ^= scope.getName().hashCode(); + } + hashCode = h; + } + + @Override + public int hashCode() { return hashCode; } + @Override + public boolean equals(Object o) + { + if (o == this) return true; + if (o == null) return false; + if (o.getClass() != getClass()) return false; + IdKey other = (IdKey) o; + return (other.key.equals(key)) && (other.type == type) && (other.scope == scope); + } + } } diff --git a/src/main/java/com/fasterxml/jackson/annotation/ObjectIdGenerators.java b/src/main/java/com/fasterxml/jackson/annotation/ObjectIdGenerators.java index 9615911..40f4215 100644 --- a/src/main/java/com/fasterxml/jackson/annotation/ObjectIdGenerators.java +++ b/src/main/java/com/fasterxml/jackson/annotation/ObjectIdGenerators.java @@ -1,6 +1,5 @@ package com.fasterxml.jackson.annotation; -import java.util.IdentityHashMap; import java.util.UUID; /** @@ -22,13 +21,6 @@ public class ObjectIdGenerators { protected final Class<?> _scope; - /** - * Lazily constructed mapping of "ids-to-Objects" used by deserialization. - */ - protected IdentityHashMap<Object, T> _ids; - - protected IdentityHashMap<T, Object> _items; - protected Base(Class<?> scope) { _scope = scope; } @@ -44,50 +36,7 @@ public class ObjectIdGenerators } @Override - public T findId(Object item) { - if (_ids == null) { - return null; - } - return _ids.get(item); - } - - - @Override - public T generateId(Object forPojo) { - T id = _generateId(forPojo); - addId(forPojo, id); - return id; - } - - protected abstract T _generateId(Object forPojo); - - @Override - public Object findItem(T id) { - if (_items == null) { - return null; - } - return _items.get(id); - } - - /** - * Method called during deserialization to keep track of items we have - * deserialized, along with ids they had. - */ - @Override - public void addItem(Object item, T id) { - if (_items == null) { - _items = new IdentityHashMap<T, Object>(16); - } - _ids.put(item, id); - } - - protected void addId(Object item, T id) { - if (_ids == null) { - _ids = new IdentityHashMap<Object, T>(16); - } - _ids.put(item, id); - } - + public abstract T generateId(Object forPojo); } /* @@ -136,12 +85,12 @@ public class ObjectIdGenerators } @Override - public ObjectIdGenerator<Integer> newForDeserialization() { - return new IntSequenceGenerator(_scope, initialValue()); + public IdKey key(Object key) { + return new IdKey(getClass(), _scope, key); } - + @Override - public Integer _generateId(Object forPojo) { + public Integer generateId(Object forPojo) { int id = _nextValue; ++_nextValue; return id; @@ -152,34 +101,44 @@ public class ObjectIdGenerators * Implementation that just uses {@link java.util.UUID}s as reliably * unique identifiers: downside is that resulting String is * 36 characters long. + *<p> + * One difference to other generators is that scope is always + * set as <code>Object.class</code> (regardless of arguments): this + * because UUIDs are globally unique, and scope has no meaning. */ public final static class UUIDGenerator extends Base<UUID> { public UUIDGenerator() { this(Object.class); } - public UUIDGenerator(Class<?> scope) { - super(scope); + private UUIDGenerator(Class<?> scope) { + super(Object.class); } + /** + * Can just return base instance since this is essentially scopeless + */ @Override public ObjectIdGenerator<UUID> forScope(Class<?> scope) { - return (_scope == scope) ? this : new UUIDGenerator(scope); + return this; } + /** + * Can just return base instance since this is essentially scopeless + */ @Override public ObjectIdGenerator<UUID> newForSerialization() { - return new UUIDGenerator(_scope); + return this; } @Override - public ObjectIdGenerator<UUID> newForDeserialization() { - return new UUIDGenerator(_scope); + public UUID generateId(Object forPojo) { + return UUID.randomUUID(); } @Override - protected UUID _generateId(Object forPojo) { - return UUID.randomUUID(); + public IdKey key(Object key) { + return new IdKey(getClass(), null, key); } - + /** * Since UUIDs are always unique, let's fully ignore scope definition */ -- Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/jackson-annotations.git _______________________________________________ pkg-java-commits mailing list [email protected] http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/pkg-java-commits

