Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.annotation;
  
  import java.util.UUID;

Container class for standard ObjectIdGenerator implementations.
  
  public class ObjectIdGenerators
  {
     /*
     /**********************************************************
     /* Shared base class for concrete implementations
     /**********************************************************
      */

    
Helper class for implementations contained.
 
     private abstract static class Base<T> extends ObjectIdGenerator<T>
     {
         protected final Class<?> _scope;
 
         protected Base(Class<?> scope) {
              = scope;
         }
 
         @Override
         public final Class<?> getScope() {
             return ;
         }
         
         @Override
         public boolean canUseFor(ObjectIdGenerator<?> gen) {
             return (gen.getClass() == getClass()) && (gen.getScope() == );
         }
         
         @Override
         public abstract T generateId(Object forPojo);
     }
 
     /*
     /**********************************************************
     /* Implementation classes
     /**********************************************************
      */
    
    
Abstract marker class used to allow explicitly specifying that no generator is used; which also implies that no Object Id is to be included or used.
 
     public abstract static class None extends ObjectIdGenerator<Object> { }
    
    
Abstract place-holder class which is used to denote case where Object Identifier to use comes from a POJO property (getter method or field). If so, value is written directly during serialization, and used as-is during deserialization.

Actual implementation class is part of databind package.

 
     public abstract static class PropertyGenerator extends Base<Object> {
         protected PropertyGenerator(Class<?> scope) { super(scope); }
     }
    
    
Simple sequence-number based generator, which uses basic Java ints (starting with value 1) as Object Identifiers.
 
     public final static class IntSequenceGenerator extends Base<Integer>
     {
         protected int _nextValue;
 
         public IntSequenceGenerator() { this(Object.class, -1); }
         public IntSequenceGenerator(Class<?> scopeint fv) {
             super(scope);
              = fv;
         }
 
         protected int initialValue() { return 1; }
         
         @Override
         public ObjectIdGenerator<IntegerforScope(Class<?> scope) {
             return ( == scope) ? this : new IntSequenceGenerator(scope);
         }
         
         @Override
         public ObjectIdGenerator<IntegernewForSerialization(Object context) {
             return new IntSequenceGenerator(initialValue());
         }
 
         @Override
         public IdKey key(Object key) {
             return new IdKey(getClass(), key);
         }
         
         @Override
         public Integer generateId(Object forPojo) {
            int id = ;
            ++;
            return id;
        }
    }

    
Implementation that just uses java.util.UUIDs as reliably unique identifiers: downside is that resulting String is 36 characters long.

One difference to other generators is that scope is always set as Object.class (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); }
        private UUIDGenerator(Class<?> scope) {
            super(Object.class);
        }

        
Can just return base instance since this is essentially scopeless
        @Override
        public ObjectIdGenerator<UUIDforScope(Class<?> scope) {
            return this;
        }
        
        
Can just return base instance since this is essentially scopeless
        @Override
        public ObjectIdGenerator<UUIDnewForSerialization(Object context) {
            return this;
        }
        @Override
        public UUID generateId(Object forPojo) {
            return UUID.randomUUID();
        }
        @Override
        public IdKey key(Object key) {
            return new IdKey(getClass(), nullkey);
        }

        
Since UUIDs are always unique, let's fully ignore scope definition
        @Override
        public boolean canUseFor(ObjectIdGenerator<?> gen) {
            return (gen.getClass() == getClass());
        }
    }
New to GrepCode? Check out our FAQ X