Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //================================================================================
  //Copyright (c) 2012, David Yu
  //All rights reserved.
  //--------------------------------------------------------------------------------
  // Redistribution and use in source and binary forms, with or without
  // modification, are permitted provided that the following conditions are met:
  // 1. Redistributions of source code must retain the above copyright notice,
  //    this list of conditions and the following disclaimer.
  // 2. Redistributions in binary form must reproduce the above copyright notice,
 //    this list of conditions and the following disclaimer in the documentation
 //    and/or other materials provided with the distribution.
 // 3. Neither the name of protostuff nor the names of its contributors may be used
 //    to endorse or promote products derived from this software without
 //    specific prior written permission.
 //
 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 // POSSIBILITY OF SUCH DAMAGE.
 //================================================================================
 
 package io.protostuff.runtime;
 
 import java.util.Map;
 
Polymorphic types.

Author(s):
David Yu
Created:
Apr 30, 2012
 
 {
 
     ARRAY
     {
         @Override
         public PolymorphicSchema newSchema(Class<?> typeClass,
                 IdStrategy strategyfinal Handler handler)
         {
             @SuppressWarnings("unchecked")
             Class<Objectct = (Class<Object>) typeClass.getComponentType();
 
             RuntimeFieldFactory<?> rff = RuntimeFieldFactory.getFieldFactory(
                     ctstrategy);
 
             if (rff == .)
             {
                 // delegate
                 return strategy.getDelegateWrapper(ct).newSchema(typeClass,
                         strategyhandler);
             }
 
             if (rff.id > 0 && rff.id < 15)
             {
                 // scalar
                 return ArraySchemas.newSchema(rff.idcttypeClassstrategy,
                         handler);
             }
 
             if (ct.isEnum())
             {
                 // enum
                 return strategy.getEnumIO(ct).newSchema(typeClassstrategy,
                         handler);
             }
 
             if (rff == .
                     || (rff == . && RuntimeFieldFactory
                             .pojo(ctnullstrategy)))
             {
                 // pojo
                 return strategy.getSchemaWrapper(cttrue).newSchema(typeClass,
                         strategyhandler);
             }
 
             return new ArraySchema(strategy)
             {
                 @Override
                 protected void setValue(Object valueObject owner)
                 {
                     handler.setValue(valueowner);
                 }
             };
         }
     },
     NUMBER
     {
         @Override
        public PolymorphicSchema newSchema(Class<?> typeClass,
                IdStrategy strategyfinal Handler handler)
        {
            return new NumberSchema(strategy)
            {
                protected void setValue(Object valueObject owner)
                {
                    handler.setValue(valueowner);
                }
            };
        }
    },
    CLASS
    {
        public PolymorphicSchema newSchema(Class<?> typeClass,
                IdStrategy strategyfinal Handler handler)
        {
            return new ClassSchema(strategy)
            {
                @Override
                protected void setValue(Object valueObject owner)
                {
                    handler.setValue(valueowner);
                }
            };
        }
    },
    ENUM
    {
        @Override
        public PolymorphicSchema newSchema(Class<?> typeClass,
                IdStrategy strategyfinal Handler handler)
        {
            return new PolymorphicEnumSchema(strategy)
            {
                @Override
                protected void setValue(Object valueObject owner)
                {
                    handler.setValue(valueowner);
                }
            };
        }
    },
    COLLECTION
    {
        @Override
        public PolymorphicSchema newSchema(Class<?> typeClass,
                IdStrategy strategyfinal Handler handler)
        {
            return new PolymorphicCollectionSchema(strategy)
            {
                protected void setValue(Object valueObject owner)
                {
                    handler.setValue(valueowner);
                }
            };
        }
    },
    MAP
    {
        @Override
        public PolymorphicSchema newSchema(Class<?> typeClass,
                IdStrategy strategyfinal Handler handler)
        {
            return new PolymorphicMapSchema(strategy)
            {
                protected void setValue(Object valueObject owner)
                {
                    handler.setValue(valueowner);
                }
            };
        }
    },
    THROWABLE
    {
        public PolymorphicSchema newSchema(Class<?> typeClass,
                IdStrategy strategyfinal Handler handler)
        {
            return new PolymorphicThrowableSchema(strategy)
            {
                @Override
                protected void setValue(Object valueObject owner)
                {
                    handler.setValue(valueowner);
                }
            };
        }
    },
    OBJECT
    {
        @Override
        public PolymorphicSchema newSchema(Class<?> typeClass,
                IdStrategy strategyfinal Handler handler)
        {
            return new ObjectSchema(strategy)
            {
                @Override
                protected void setValue(Object valueObject owner)
                {
                    handler.setValue(valueowner);
                }
            };
        }
    };
    public static PolymorphicSchema.Factory getFactoryFromField(Class<?> clazz)
    {
        if (clazz.isArray())
            return ;
        if (Number.class == clazz)
            return ;
        if (Class.class == clazz)
            return ;
        if (Enum.class == clazz)
            return ;
        if (Map.class.isAssignableFrom(clazz))
            return ;
        if (Collection.class.isAssignableFrom(clazz))
            return ;
        if (Throwable.class.isAssignableFrom(clazz))
            return ;
        return ;
    }
            Class<?> clazz)
    {
        if (clazz.isArray())
            return ;
        if (Number.class == clazz)
            return ;
        if (Class.class == clazz)
            return ;
        if (Enum.class == clazz)
            return ;
        if (Throwable.class.isAssignableFrom(clazz))
            return ;
        if (Object.class == clazz)
            return ;
        return null;
    }
            Class<?> clazzIdStrategy strategy)
    {
        if (clazz.isArray())
        {
            @SuppressWarnings("unchecked")
            Class<Objectct = (Class<Object>) clazz.getComponentType();
            RuntimeFieldFactory<?> rff = RuntimeFieldFactory.getFieldFactory(
                    ctstrategy);
            if (rff == .)
            {
                // delegate
                return strategy.getDelegateWrapper(ct).;
            }
            if (rff.id > 0 && rff.id < 15)
            {
                // scalar
                return ArraySchemas.getGenericElementSchema(rff.id);
            }
            if (ct.isEnum())
            {
                // enum
                return strategy.getEnumIO(ct).;
            }
            if (rff == .
                    || (rff == . && RuntimeFieldFactory
                            .pojo(ctnullstrategy)))
            {
                // pojo
                return strategy.getSchemaWrapper(cttrue).;
            }
            return strategy.ARRAY_ELEMENT_SCHEMA;
        }
        if (Number.class == clazz)
            return strategy.NUMBER_ELEMENT_SCHEMA;
        if (Class.class == clazz)
            return strategy.CLASS_ELEMENT_SCHEMA;
        if (Enum.class == clazz)
            return strategy.POLYMORPHIC_ENUM_ELEMENT_SCHEMA;
        if (Throwable.class.isAssignableFrom(clazz))
            return strategy.POLYMORPHIC_THROWABLE_ELEMENT_SCHEMA;
        if (Object.class == clazz)
            return strategy.OBJECT_ELEMENT_SCHEMA;
        return null;
    }
New to GrepCode? Check out our FAQ X