All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dyuproject.protostuff.runtime.RuntimeRepeatedFieldFactory Maven / Gradle / Ivy

//========================================================================
//Copyright 2007-2011 David Yu [email protected]
//------------------------------------------------------------------------
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at 
//http://www.apache.org/licenses/LICENSE-2.0
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
//========================================================================

package com.dyuproject.protostuff.runtime;

import java.io.IOException;
import java.util.Collection;
import java.util.EnumSet;
import java.util.Map;

import com.dyuproject.protostuff.CollectionSchema.MessageFactory;
import com.dyuproject.protostuff.GraphInput;
import com.dyuproject.protostuff.Input;
import com.dyuproject.protostuff.Message;
import com.dyuproject.protostuff.Morph;
import com.dyuproject.protostuff.Output;
import com.dyuproject.protostuff.Pipe;
import com.dyuproject.protostuff.Schema;
import com.dyuproject.protostuff.Tag;
import com.dyuproject.protostuff.WireFormat.FieldType;
import com.dyuproject.protostuff.runtime.MappedSchema.Field;

/**
 * Static utility for creating runtime repeated (list/collection) fields.
 *
 * @author David Yu
 * @created Jan 23, 2011
 */
final class RuntimeRepeatedFieldFactory
{
    
    private RuntimeRepeatedFieldFactory() {}
    
    /**
     * For lazy initialization called by {@link RuntimeFieldFactory}.
     */
    static RuntimeFieldFactory> getFactory()
    {
        return REPEATED;
    }
    
    static final Accessor.Factory ACCESSOR_FACTORY = RuntimeFieldFactory.ACCESSOR_FACTORY;
    
    private static  Field createCollectionInlineV(int number, String name, 
            final java.lang.reflect.Field f, final MessageFactory messageFactory, 
            final Delegate inline)
    {
        final Accessor accessor = ACCESSOR_FACTORY.create(f);
        return new Field(inline.getFieldType(), number, name, true, 
                f.getAnnotation(Tag.class))
        {
            protected void mergeFrom(Input input, T message) throws IOException
            {
                final Object value = inline.readFrom(input);
                Collection existing = accessor.get(message);
                if (existing == null)
                    accessor.set(message, existing = messageFactory.newMessage());
                
                existing.add(value);
            }
            protected void writeTo(Output output, T message) throws IOException
            {
                final Collection collection = accessor.get(message);
                
                if(collection != null && !collection.isEmpty())
                {
                    for(Object o : collection)
                    {
                        if(o != null)
                            inline.writeTo(output, number, o, true);
                    }
                }
            }
            protected void transfer(Pipe pipe, Input input, Output output, 
                    boolean repeated) throws IOException
            {
                inline.transfer(pipe, input, output, number, repeated);
            }
        };
    }
    
    private static  Field createCollectionEnumV(int number, String name, 
            final java.lang.reflect.Field f, final MessageFactory messageFactory,  
            final Class genericType, IdStrategy strategy)
    {
        final EnumIO eio = strategy.getEnumIO(genericType);
        final Accessor accessor = ACCESSOR_FACTORY.create(f);
        return new Field(FieldType.ENUM, number, name, true, 
                f.getAnnotation(Tag.class))
        {
            protected void mergeFrom(Input input, T message) throws IOException
            {
                final Enum value = eio.readFrom(input);
                Collection> existing = accessor.get(message);
                if (existing == null)
                    accessor.set(message, existing = messageFactory.newMessage());
                
                existing.add(value);
            }
            protected void writeTo(Output output, T message) throws IOException
            {
                final Collection> collection = accessor.get(message);
                
                if(collection != null && !collection.isEmpty())
                {
                    for(Enum en : collection)
                    {
                        if (en != null)
                            EnumIO.writeTo(output, number, true, en);
                    }
                }
            }
            protected void transfer(Pipe pipe, Input input, Output output, 
                    boolean repeated) throws IOException
            {
                EnumIO.transfer(pipe, input, output, number, repeated);
            }
        };
    }
    
    private static  Field createCollectionPojoV(int number, String name, 
            final java.lang.reflect.Field f, final MessageFactory messageFactory,  
            final Class genericType, IdStrategy strategy)
    {
        final Accessor accessor = ACCESSOR_FACTORY.create(f);
        return new RuntimeMessageField(
                genericType, strategy.getSchemaWrapper(genericType, true), 
                FieldType.MESSAGE, number, name, true, 
                f.getAnnotation(Tag.class))
        {
            protected void mergeFrom(Input input, T message) throws IOException
            {
                final Object value = input.mergeObject(null, getSchema());
                Collection existing = accessor.get(message);
                if (existing == null)
                    accessor.set(message, existing = messageFactory.newMessage());
                
                existing.add(value);
            }
            protected void writeTo(Output output, T message) throws IOException
            {
                final Collection collection = accessor.get(message);
                
                if(collection != null && !collection.isEmpty())
                {
                    final Schema schema = getSchema();
                    for(Object o : collection)
                    {
                        if(o != null)
                            output.writeObject(number, o, schema, true);
                    }
                }
            }
            protected void transfer(Pipe pipe, Input input, Output output, 
                    boolean repeated) throws IOException
            {
                output.writeObject(number, pipe, getPipeSchema(), repeated);
            }
            @Override
            protected Field copy(IdStrategy strategy)
            {
                return createCollectionPojoV(number, name, f, 
                        messageFactory, genericType, strategy);
            }
        };
    }
    
    private static  Field createCollectionPolymorphicV(int number, String name, 
            final java.lang.reflect.Field f, final MessageFactory messageFactory,  
            final Class genericType, IdStrategy strategy)
    {
        final Accessor accessor = ACCESSOR_FACTORY.create(f);
        return new RuntimeDerivativeField(
                genericType, FieldType.MESSAGE, number, name, true, 
                f.getAnnotation(Tag.class), 
                strategy)
        {
            protected void mergeFrom(Input input, T message) throws IOException
            {
                final Object value = input.mergeObject(message, schema);
                if(input instanceof GraphInput && 
                        ((GraphInput)input).isCurrentMessageReference())
                {
                    // a reference from polymorphic+cyclic graph deser
                    Collection existing = accessor.get(message);
                    if (existing == null)
                        accessor.set(message, existing = messageFactory.newMessage());
                    
                    existing.add(value);
                }
            }
            protected void writeTo(Output output, T message) throws IOException
            {
                final Collection existing = accessor.get(message);
                
                if(existing != null && !existing.isEmpty())
                {
                    for(Object o : existing)
                    {
                        if(o != null)
                            output.writeObject(number, o, schema, true);
                    }
                }
            }
            protected void transfer(Pipe pipe, Input input, Output output, 
                    boolean repeated) throws IOException
            {
                output.writeObject(number, pipe, schema.pipeSchema, repeated);
            }
            protected void doMergeFrom(Input input, Schema schema, 
                    Object message) throws IOException
            {
                final Object value = schema.newMessage();
                if(input instanceof GraphInput)
                {
                    // update the actual reference.
                    ((GraphInput)input).updateLast(value, message);
                }
                
                schema.mergeFrom(input, value);
                
                Collection existing = accessor.get(message);
                if (existing == null)
                    accessor.set(message, existing = messageFactory.newMessage());
                
                existing.add(value);
            }
            @Override
            protected Field copy(IdStrategy strategy)
            {
                return createCollectionPolymorphicV(number, name, f, 
                        messageFactory, genericType, strategy);
            }
        };
    }
    
    private static  Field createCollectionObjectV(int number, String name, 
            final java.lang.reflect.Field f, final MessageFactory messageFactory, 
            final Class genericType, final PolymorphicSchema.Factory factory, IdStrategy strategy)
    {
        final Accessor accessor = ACCESSOR_FACTORY.create(f);
        return new RuntimeObjectField(genericType,
                FieldType.MESSAGE, number, name, true, 
                f.getAnnotation(Tag.class), 
                factory, strategy)
        {
            protected void mergeFrom(Input input, T message) throws IOException
            {
                final Object value = input.mergeObject(message, schema);
                if(input instanceof GraphInput && 
                        ((GraphInput)input).isCurrentMessageReference())
                {
                    // a reference from polymorphic+cyclic graph deser
                    Collection existing = accessor.get(message);
                    if (existing == null)
                        accessor.set(message, existing = messageFactory.newMessage());
                    
                    existing.add(value);
                }
            }
            protected void writeTo(Output output, T message) throws IOException
            {
                final Collection existing = accessor.get(message);
                
                if(existing != null && !existing.isEmpty())
                {
                    for(Object o : existing)
                    {
                        if(o != null)
                            output.writeObject(number, o, schema, true);
                    }
                }
            }
            protected void transfer(Pipe pipe, Input input, Output output, 
                    boolean repeated) throws IOException
            {
                output.writeObject(number, pipe, schema.getPipeSchema(), repeated);
            }
            public void setValue(Object value, Object message)
            {
                Collection existing = accessor.get(message);
                if (existing == null)
                    accessor.set(message, existing = messageFactory.newMessage());
                
                existing.add(value);
            }
            @Override
            protected Field copy(IdStrategy strategy)
            {
                return createCollectionObjectV(number, name, f, 
                        messageFactory, genericType, factory, strategy);
            }
        };
    }
    
    private static final RuntimeFieldFactory> REPEATED = new RuntimeFieldFactory>(RuntimeFieldFactory.ID_COLLECTION)
    {
        @SuppressWarnings("unchecked")
        public  Field create(int number, String name, final java.lang.reflect.Field f, IdStrategy strategy)
        {
            if(null != f.getAnnotation(Morph.class))
            {
                // can be used to override the configured system property:
                // RuntimeEnv.COLLECTION_SCHEMA_ON_REPEATED_FIELDS
                
                // In this context, Morph annotation will force using a collection
                // schema only for this particular field.
                return RuntimeCollectionFieldFactory.getFactory().create(number, 
                        name, f, strategy);
            }
            
            if(EnumSet.class.isAssignableFrom(f.getType()))
            {
                final Class enumType = (Class)getGenericType(f, 0);
                if(enumType == null)
                {
                    // still handle the serialization of EnumSets even without generics
                    return RuntimeFieldFactory.OBJECT.create(number, name, f, strategy);
                }
                
                return createCollectionEnumV(number, name, f, 
                        strategy.getEnumIO(enumType).getEnumSetFactory(), enumType, strategy);
            }
            
            final MessageFactory messageFactory = strategy.getCollectionFactory(f.getType());
            
            final Class genericType = (Class)getGenericType(f, 0);
            if (genericType == null || ((Map.class.isAssignableFrom(genericType) || 
                    Collection.class.isAssignableFrom(genericType)) && 
                    !strategy.isRegistered(genericType)))
            {
                // the value is not a simple parameterized type.
                return createCollectionObjectV(number, name, f, messageFactory, 
                        genericType, PolymorphicSchemaFactories.OBJECT, strategy);
            }
            
            final Delegate inline = getDelegateOrInline(genericType, strategy);
            if(inline != null)
                return createCollectionInlineV(number, name, f, messageFactory, inline);
            
            if(Message.class.isAssignableFrom(genericType))
                return createCollectionPojoV(number, name, f, messageFactory, genericType, strategy);
            
            if(genericType.isEnum())
                return createCollectionEnumV(number, name, f, messageFactory, genericType, strategy);
            
            final PolymorphicSchema.Factory factory = 
                    PolymorphicSchemaFactories.getFactoryFromRepeatedValueGenericType(genericType);
            if(factory != null)
            {
                return createCollectionObjectV(number, name, f, messageFactory, 
                        genericType, factory, strategy);
            }
            
            if(pojo(genericType, f.getAnnotation(Morph.class), strategy))
                return createCollectionPojoV(number, name, f, messageFactory, genericType, strategy);
            
            if(genericType.isInterface())
            {
                return createCollectionObjectV(number, name, f, messageFactory, 
                        genericType, PolymorphicSchemaFactories.OBJECT, strategy);
            }
            
            return createCollectionPolymorphicV(number, name, f, messageFactory, genericType, strategy);
        }
        public void transfer(Pipe pipe, Input input, Output output, int number, 
                boolean repeated) throws IOException
        {
            throw new UnsupportedOperationException();
        }
        public Collection readFrom(Input input) throws IOException
        {
            throw new UnsupportedOperationException();
        }
        public void writeTo(Output output, int number, Collection value, 
                boolean repeated) throws IOException
        {
            throw new UnsupportedOperationException();
        }
        public FieldType getFieldType()
        {
            throw new UnsupportedOperationException();
        }
        public Class typeClass()
        {
            throw new UnsupportedOperationException();
        }
    };

}