[jackson-annotations] 17/207: Bit more work on ObjectIdGenerator definition

Timo Aaltonen tjaalton at moszumanska.debian.org
Sat Sep 6 13:55:35 UTC 2014


This is an automated email from the git hooks/post-receive script.

tjaalton pushed a commit to branch master
in repository jackson-annotations.

commit 9aa54e082ad397cd3294f8eab6a7f9e3a1777fe1
Author: Tatu <tatu at ning.com>
Date:   Tue Feb 7 15:00:22 2012 -0800

    Bit more work on ObjectIdGenerator definition
---
 .../jackson/annotation/JsonIdentityInfo.java       | 19 +++---
 .../jackson/annotation/ObjectIdGenerator.java      | 22 ++-----
 .../jackson/annotation/ObjectIdGenerators.java     | 69 ++++++++++++++--------
 3 files changed, 59 insertions(+), 51 deletions(-)

diff --git a/src/main/java/com/fasterxml/jackson/annotation/JsonIdentityInfo.java b/src/main/java/com/fasterxml/jackson/annotation/JsonIdentityInfo.java
index 28c0724..bb729a8 100644
--- a/src/main/java/com/fasterxml/jackson/annotation/JsonIdentityInfo.java
+++ b/src/main/java/com/fasterxml/jackson/annotation/JsonIdentityInfo.java
@@ -52,15 +52,16 @@ public @interface JsonIdentityInfo
     public Class<? extends ObjectIdGenerator<?>> generator();
 
     /**
-     * Scope is a concept used by {@link ObjectIdGenerator} created based
-     * on {@link #property}, iff {@link ObjectIdGenerator#usesGlobalScope()}
-     * returns false. If so, separate generator instances are created for
-     * each distinct scope. If not defined (i.e. left at default value of
-     * {@link JsonIdentityInfo}), will just use type of the annotated
-     * class or property as scope.
+     * Scope is used to define applicability of an Object Id: all ids
+     * must be unique within their scope; where scope is defined
+     * as combination of this value and generator type.
+     * Comparison is simple equivalence, meaning that both type
+     * generator type and scope class must be the same.
      *<p>
-     * If {@link ObjectIdGenerator#usesGlobalScope()} returns true,
-     * value of this property is ignored.
+     * Scope is used for determining how many generators are needed;
+     * more than one scope is typically only needed if external Object Ids
+     * have overlapping value domains (i.e. are only unique within some
+     * limited scope)
      */
-    public Class<?> scope() default JsonIdentityInfo.class;
+    public Class<?> scope() default Object.class;
 }
diff --git a/src/main/java/com/fasterxml/jackson/annotation/ObjectIdGenerator.java b/src/main/java/com/fasterxml/jackson/annotation/ObjectIdGenerator.java
index 83d176c..a7b9099 100644
--- a/src/main/java/com/fasterxml/jackson/annotation/ObjectIdGenerator.java
+++ b/src/main/java/com/fasterxml/jackson/annotation/ObjectIdGenerator.java
@@ -29,25 +29,13 @@ public abstract class ObjectIdGenerator<T>
     public abstract ObjectIdGenerator<T> newForDeserialization(Class<?> scope);
     
     /**
-     * Accessor called to determine whether scope of Object Identifiers
-     * is global (within context of a single serialization) or not;
-     * if not, scope is assumed to be per-type (using statically declared
-     * type). Definition of scope is that all identifiers produced must
-     * be unique within a scope: thus global scope would guarantee
-     * that all produced identifiers are unique for full serialization
-     * process, whereas local scopes only guarantee it for the supported
-     * type (within single serialization).
-     *<p>
-     * One generator instance is needed per scope, and for deserialization,
-     * separate Maps are kept on per-scope basis.
-     *<p>
-     * Standard generators (UUID, sequence-number) support global scope;
-     * custom generators may support
+     * Method called to check whether this generator instance can
+     * be used for Object Ids of specific generator type and
+     * scope.
      * 
-     * @return True if global (one per serialization) scope is needed by
-     *   generator; false if per-type scope is needed.
+     * @return True if this instance can be used as-is; false if not
      */
-    public abstract boolean usesGlobalScope();
+    public abstract boolean canUseFor(ObjectIdGenerator<?> gen, Class<?> scope);
     
     /**
      * Method used for generating an Object Identifier to serialize
diff --git a/src/main/java/com/fasterxml/jackson/annotation/ObjectIdGenerators.java b/src/main/java/com/fasterxml/jackson/annotation/ObjectIdGenerators.java
index 2fc08f8..d5c0fdb 100644
--- a/src/main/java/com/fasterxml/jackson/annotation/ObjectIdGenerators.java
+++ b/src/main/java/com/fasterxml/jackson/annotation/ObjectIdGenerators.java
@@ -10,7 +10,7 @@ public class ObjectIdGenerators
 {
     /*
     /**********************************************************
-    /* Implementation classes
+    /* Shared base class for concrete implementations
     /**********************************************************
      */
 
@@ -18,14 +18,29 @@ public class ObjectIdGenerators
      * Helper class that implements scoped storage for Object
      * references.
      */
-    private abstract static class Base<T> extends ObjectIdGenerator<T>
+    protected abstract static class Base<T> extends ObjectIdGenerator<T>
     {
+        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;
+        }
+
+        @Override
+        public boolean canUseFor(ObjectIdGenerator<?> gen, Class<?> scope) {
+            return (gen.getClass() == getClass()) && (scope == _scope);
+        }
+        
+        public Class<?> getScope() {
+            return _scope;
+        }
         
         protected T findId(Object item) {
             if (_ids == null) {
@@ -63,6 +78,12 @@ public class ObjectIdGenerators
             _ids.put(item, id);
         }
     }
+
+    /*
+    /**********************************************************
+    /* Implementation classes
+    /**********************************************************
+     */
     
     /**
      * Abstract place-holder class which is used to denote case
@@ -73,39 +94,33 @@ public class ObjectIdGenerators
      * Actual implementation class is part of <code>databind</code>
      * package.
      */
-    public abstract class PropertyGenerator<T> extends Base<T> { }
+    public abstract class PropertyGenerator<T> extends Base<T> {
+        protected PropertyGenerator(Class<?> scope) { super(scope); }
+    }
     
     /**
      * Simple sequence-number based generator, which uses basic Java
      * <code>int</code>s (starting with value 1) as Object Identifiers.
      */
-    public static class IntSequenceGenerator extends Base<Integer>
+    public final static class IntSequenceGenerator extends Base<Integer>
     {
         protected int _nextValue;
 
-        public IntSequenceGenerator() { this(1); }
-        public IntSequenceGenerator(int fv) {
-            super();
+        public IntSequenceGenerator(Class<?> scope) { this(scope, 1); }
+        public IntSequenceGenerator(Class<?> scope, int fv) {
+            super(scope);
             _nextValue = fv;
         }
 
         @Override
         public ObjectIdGenerator<Integer> newForSerialization(Class<?> scope) {
-            return new IntSequenceGenerator(_nextValue);
+            return new IntSequenceGenerator(scope, _nextValue);
         }
 
         // we don't really need value for deserialization but...
         @Override
         public ObjectIdGenerator<Integer> newForDeserialization(Class<?> scope) {
-            return new IntSequenceGenerator(_nextValue);
-        }
-        
-        /**
-         * We can easily support global scope with simple sequences, so return true
-         */
-        @Override
-        public boolean usesGlobalScope() {
-            return true;
+            return new IntSequenceGenerator(scope, _nextValue);
         }
 
         @Override
@@ -121,26 +136,30 @@ public class ObjectIdGenerators
      * unique identifiers: downside is that resulting String is
      * 36 characters long.
      */
-    public static class UUIDGenerator extends Base<UUID>
+    public final static class UUIDGenerator extends Base<UUID>
     {
+        public UUIDGenerator(Class<?> scope) {
+            super(scope);
+        }
+        
         @Override
         public ObjectIdGenerator<UUID> newForSerialization(Class<?> scope) {
-            return new UUIDGenerator();
+            return new UUIDGenerator(scope);
         }
 
         @Override
         public ObjectIdGenerator<UUID> newForDeserialization(Class<?> scope) {
-            return new UUIDGenerator();
+            return new UUIDGenerator(scope);
         }
-        
+
         /**
-         * UUIDs are globally unique, so yes we can support global scope
+         * Since UUIDs are always unique, let's fully ignore scope definition
          */
         @Override
-        public boolean usesGlobalScope() {
-            return true;
+        public boolean canUseFor(ObjectIdGenerator<?> gen, Class<?> scope) {
+            return (gen.getClass() == getClass());
         }
-
+        
         @Override
         public UUID generateId(Object forPojo) {
             return UUID.randomUUID();

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/jackson-annotations.git



More information about the pkg-java-commits mailing list