Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //========================================================================
  //Copyright 2007-2011 David Yu dyuproject@gmail.com
  //------------------------------------------------------------------------
  //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 io.protostuff.runtime;
 
 
Static utility for creating runtime repeated (list/collection) fields.

Author(s):
David Yu
Created:
Jan 23, 2011
 
 {
 
     private RuntimeRepeatedFieldFactory()
     {
     }

    
For lazy initialization called by RuntimeFieldFactory.
 
     {
         return ;
     }
 
     private static <T> Field<T> createCollectionInlineV(int number,
             String namefinal java.lang.reflect.Field f,
             final MessageFactory messageFactoryfinal Delegate<Objectinline)
     {
         return new Field<T>(inline.getFieldType(), numbernametrue,
                 f.getAnnotation(Tag.class))
         {
             {
                 f.setAccessible(true);
             }
 
             @Override
             @SuppressWarnings("unchecked")
             protected void mergeFrom(Input input, T messagethrows IOException
             {
                 final Object value = inline.readFrom(input);
                 try
                 {
                     final Collection<Objectexisting = (Collection<Object>) f
                             .get(message);
                     if (existing == null)
                     {
                         final Collection<Objectcollection = messageFactory
                                 .newMessage();
                         collection.add(value);
                         f.set(messagecollection);
                     }
                     else
                         existing.add(value);
                 }
                 catch (IllegalArgumentException | IllegalAccessException e)
                 {
                     throw new RuntimeException(e);
                 }
             }
 
             @Override
             @SuppressWarnings("unchecked")
             protected void writeTo(Output output, T messagethrows IOException
             {
                 final Collection<Objectcollection;
                 try
                 {
                     collection = (Collection<Object>) f.get(message);
                 }
                 catch (IllegalArgumentException | IllegalAccessException e)
                {
                    throw new RuntimeException(e);
                }
                if (collection != null && !collection.isEmpty())
                {
                    for (Object o : collection)
                    {
                        if (o != null)
                            inline.writeTo(outputotrue);
                    }
                }
            }
            @Override
            protected void transfer(Pipe pipeInput inputOutput output,
                    boolean repeatedthrows IOException
            {
                inline.transfer(pipeinputoutputrepeated);
            }
        };
    }
    private static <T> Field<T> createCollectionEnumV(int numberString name,
            final java.lang.reflect.Field f,
            final MessageFactory messageFactory,
            final Class<ObjectgenericTypeIdStrategy strategy)
    {
        final EnumIO<?> eio = strategy.getEnumIO(genericType);
        return new Field<T>(.numbernametrue,
                f.getAnnotation(Tag.class))
        {
            {
                f.setAccessible(true);
            }
            @Override
            @SuppressWarnings("unchecked")
            protected void mergeFrom(Input input, T messagethrows IOException
            {
                final Enum<?> value = eio.readFrom(input);
                try
                {
                    final Collection<Enum<?>> existing = (Collection<Enum<?>>) f
                            .get(message);
                    if (existing == null)
                    {
                        final Collection<Enum<?>> collection = messageFactory
                                .newMessage();
                        collection.add(value);
                        f.set(messagecollection);
                    }
                    else
                        existing.add(value);
                }
                catch (IllegalArgumentException | IllegalAccessException e)
                {
                    throw new RuntimeException(e);
                }
            }
            @Override
            @SuppressWarnings("unchecked")
            protected void writeTo(Output output, T messagethrows IOException
            {
                final Collection<Enum<?>> collection;
                try
                {
                    collection = (Collection<Enum<?>>) f.get(message);
                }
                catch (IllegalArgumentException | IllegalAccessException e)
                {
                    throw new RuntimeException(e);
                }
                if (collection != null && !collection.isEmpty())
                {
                    for (Enum<?> en : collection)
                        eio.writeTo(outputtrueen);
                }
            }
            @Override
            protected void transfer(Pipe pipeInput inputOutput output,
                    boolean repeatedthrows IOException
            {
                EnumIO.transfer(pipeinputoutputrepeated);
            }
        };
    }
    private static <T> Field<T> createCollectionPojoV(int numberString name,
            final java.lang.reflect.Field f,
            final MessageFactory messageFactory,
            final Class<ObjectgenericTypeIdStrategy strategy)
    {
        return new RuntimeMessageField<T, Object>(genericType,
                strategy.getSchemaWrapper(genericTypetrue),
                .numbernametrue,
                f.getAnnotation(Tag.class))
        {
            {
                f.setAccessible(true);
            }
            @Override
            @SuppressWarnings("unchecked")
            protected void mergeFrom(Input input, T messagethrows IOException
            {
                final Object value = input.mergeObject(nullgetSchema());
                try
                {
                    final Collection<Objectexisting = (Collection<Object>) f
                            .get(message);
                    if (existing == null)
                    {
                        final Collection<Objectcollection = messageFactory
                                .newMessage();
                        collection.add(value);
                        f.set(messagecollection);
                    }
                    else
                        existing.add(value);
                }
                catch (IllegalArgumentException | IllegalAccessException e)
                {
                    throw new RuntimeException(e);
                }
            }
            @Override
            @SuppressWarnings("unchecked")
            protected void writeTo(Output output, T messagethrows IOException
            {
                final Collection<Objectcollection;
                try
                {
                    collection = (Collection<Object>) f.get(message);
                }
                catch (IllegalArgumentException | IllegalAccessException e)
                {
                    throw new RuntimeException(e);
                }
                if (collection != null && !collection.isEmpty())
                {
                    final Schema<Objectschema = getSchema();
                    for (Object o : collection)
                    {
                        if (o != null)
                            output.writeObject(oschematrue);
                    }
                }
            }
            @Override
            protected void transfer(Pipe pipeInput inputOutput output,
                    boolean repeatedthrows IOException
            {
                output.writeObject(pipegetPipeSchema(), repeated);
            }
        };
    }
    private static <T> Field<T> createCollectionPolymorphicV(int number,
            String namefinal java.lang.reflect.Field f,
            final MessageFactory messageFactory,
            final Class<ObjectgenericTypeIdStrategy strategy)
    {
        return new RuntimeDerivativeField<T>(genericType.,
                numbernametruef.getAnnotation(Tag.class), strategy)
        {
            {
                f.setAccessible(true);
            }
            @Override
            @SuppressWarnings("unchecked")
            protected void mergeFrom(Input input, T messagethrows IOException
            {
                final Object value = input.mergeObject(message);
                if (input instanceof GraphInput
                        && ((GraphInputinput).isCurrentMessageReference())
                {
                    // a reference from polymorphic+cyclic graph deser
                    try
                    {
                        final Collection<Objectexisting = (Collection<Object>) f
                                .get(message);
                        if (existing == null)
                        {
                            final Collection<Objectcollection = messageFactory
                                    .newMessage();
                            collection.add(value);
                            f.set(messagecollection);
                        }
                        else
                            existing.add(value);
                    }
                    catch (IllegalArgumentException | IllegalAccessException e)
                    {
                        throw new RuntimeException(e);
                    }
                }
            }
            @Override
            @SuppressWarnings("unchecked")
            protected void writeTo(Output output, T messagethrows IOException
            {
                final Collection<Objectexisting;
                try
                {
                    existing = (Collection<Object>) f.get(message);
                }
                catch (IllegalArgumentException | IllegalAccessException e)
                {
                    throw new RuntimeException(e);
                }
                if (existing != null && !existing.isEmpty())
                {
                    for (Object o : existing)
                    {
                        if (o != null)
                            output.writeObject(otrue);
                    }
                }
            }
            @Override
            protected void transfer(Pipe pipeInput inputOutput output,
                    boolean repeatedthrows IOException
            {
                output.writeObject(pipe.repeated);
            }
            @Override
            @SuppressWarnings("unchecked")
            protected void doMergeFrom(Input inputSchema<Objectschema,
                    Object messagethrows IOException
            {
                final Object value = schema.newMessage();
                if (input instanceof GraphInput)
                {
                    // update the actual reference.
                    ((GraphInputinput).updateLast(valuemessage);
                }
                schema.mergeFrom(inputvalue);
                try
                {
                    final Collection<Objectexisting = (Collection<Object>) f
                            .get(message);
                    if (existing == null)
                    {
                        final Collection<Objectcollection = messageFactory
                                .newMessage();
                        collection.add(value);
                        f.set(messagecollection);
                    }
                    else
                        existing.add(value);
                }
                catch (IllegalArgumentException | IllegalAccessException e)
                {
                    throw new RuntimeException(e);
                }
            }
        };
    }
    private static <T> Field<T> createCollectionObjectV(int number,
            String namefinal java.lang.reflect.Field f,
            final MessageFactory messageFactoryClass<ObjectgenericType,
            PolymorphicSchema.Factory factoryIdStrategy strategy)
    {
        return new RuntimeObjectField<T>(genericType.,
                numbernametruef.getAnnotation(Tag.class), factory,
                strategy)
        {
            {
                f.setAccessible(true);
            }
            @Override
            @SuppressWarnings("unchecked")
            protected void mergeFrom(Input input, T messagethrows IOException
            {
                final Object value = input.mergeObject(message);
                if (input instanceof GraphInput
                        && ((GraphInputinput).isCurrentMessageReference())
                {
                    // a reference from polymorphic+cyclic graph deser
                    try
                    {
                        final Collection<Objectexisting = (Collection<Object>) f
                                .get(message);
                        if (existing == null)
                        {
                            final Collection<Objectcollection = messageFactory
                                    .newMessage();
                            collection.add(value);
                            f.set(messagecollection);
                        }
                        else
                            existing.add(value);
                    }
                    catch (IllegalArgumentException | IllegalAccessException e)
                    {
                        throw new RuntimeException(e);
                    }
                }
            }
            @Override
            @SuppressWarnings("unchecked")
            protected void writeTo(Output output, T messagethrows IOException
            {
                final Collection<Objectexisting;
                try
                {
                    existing = (Collection<Object>) f.get(message);
                }
                catch (IllegalArgumentException | IllegalAccessException e)
                {
                    throw new RuntimeException(e);
                }
                if (existing != null && !existing.isEmpty())
                {
                    for (Object o : existing)
                    {
                        if (o != null)
                            output.writeObject(otrue);
                    }
                }
            }
            @Override
            protected void transfer(Pipe pipeInput inputOutput output,
                    boolean repeatedthrows IOException
            {
                output.writeObject(pipe.getPipeSchema(),
                        repeated);
            }
            @Override
            @SuppressWarnings("unchecked")
            public void setValue(Object valueObject message)
            {
                try
                {
                    final Collection<Objectexisting = (Collection<Object>) f
                            .get(message);
                    if (existing == null)
                    {
                        final Collection<Objectcollection = messageFactory
                                .newMessage();
                        collection.add(value);
                        f.set(messagecollection);
                    }
                    else
                        existing.add(value);
                }
                catch (IllegalArgumentException | IllegalAccessException e)
                {
                    throw new RuntimeException(e);
                }
            }
        };
    }
    private static final RuntimeFieldFactory<Collection<?>> REPEATED = new RuntimeFieldFactory<Collection<?>>(
            .)
    {
        @Override
        @SuppressWarnings("unchecked")
        public <T> Field<T> create(int numberString name,
                final java.lang.reflect.Field fIdStrategy 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(
                        numbernamefstrategy);
            }
            if (EnumSet.class.isAssignableFrom(f.getType()))
            {
                final Class<ObjectenumType = (Class<Object>) getGenericType(
                        f, 0);
                if (enumType == null)
                {
                    // still handle the serialization of EnumSets even without
                    // generics
                    return ..create(numbernamef,
                            strategy);
                }
                return createCollectionEnumV(numbernamefstrategy
                        .getEnumIO(enumType).getEnumSetFactory(), enumType,
                        strategy);
            }
            final MessageFactory messageFactory = strategy
                    .getCollectionFactory(f.getType());
            final Class<ObjectgenericType = (Class<Object>) getGenericType(f,
                    0);
            if (genericType == null)
            {
                // the value is not a simple parameterized type.
                return createCollectionObjectV(numbernamefmessageFactory,
                        genericType.,
                        strategy);
            }
            final Delegate<Objectinline = getDelegateOrInline(genericType,
                    strategy);
            if (inline != null)
                return createCollectionInlineV(numbernamefmessageFactory,
                        inline);
            if (Message.class.isAssignableFrom(genericType))
                return createCollectionPojoV(numbernamefmessageFactory,
                        genericTypestrategy);
            if (genericType.isEnum())
                return createCollectionEnumV(numbernamefmessageFactory,
                        genericTypestrategy);
            final PolymorphicSchema.Factory factory = PolymorphicSchemaFactories
                    .getFactoryFromRepeatedValueGenericType(genericType);
            if (factory != null)
            {
                return createCollectionObjectV(numbernamefmessageFactory,
                        genericTypefactorystrategy);
            }
            if (pojo(genericTypef.getAnnotation(Morph.class), strategy))
                return createCollectionPojoV(numbernamefmessageFactory,
                        genericTypestrategy);
            if (genericType.isInterface())
            {
                return createCollectionObjectV(numbernamefmessageFactory,
                        genericType.,
                        strategy);
            }
            return createCollectionPolymorphicV(numbernamef,
                    messageFactorygenericTypestrategy);
        }
        @Override
        public void transfer(Pipe pipeInput inputOutput outputint number,
                boolean repeatedthrows IOException
        {
            throw new UnsupportedOperationException();
        }
        @Override
        public Collection<?> readFrom(Input inputthrows IOException
        {
            throw new UnsupportedOperationException();
        }
        @Override
        public void writeTo(Output outputint numberCollection<?> value,
                boolean repeatedthrows IOException
        {
            throw new UnsupportedOperationException();
        }
        @Override
        public FieldType getFieldType()
        {
            throw new UnsupportedOperationException();
        }
        @Override
        public Class<?> typeClass()
        {
            throw new UnsupportedOperationException();
        }
    };
New to GrepCode? Check out our FAQ X