Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //========================================================================
  //Copyright 2007-2009 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;
 
 import java.util.Map;
 
Base class for schemas that maps fields by number and name. For fast initialization, the last field number is provided in the constructor.

Author(s):
David Yu
Created:
Nov 10, 2009
 
 public abstract class MappedSchema<T> implements Schema<T>
 {
 
     protected final Class<T> typeClass;
     protected final Field<T>[] fieldsfieldsByNumber;
     protected final Map<StringField<T>> fieldsByName;
     protected final Pipe.Schema<T> pipeSchema;
 
     @SuppressWarnings("unchecked")
     public MappedSchema(Class<T> typeClass, Field<T>[] fields,
             int lastFieldNumber)
     {
         if (fields.length == 0)
             throw new IllegalStateException("At least one field is required.");
 
         this. = typeClass;
         this. = fields;
          = new HashMap<>();
          = (Field<T>[]) new Field<?>[lastFieldNumber + 1];
         for (Field<T> f : fields)
         {
             Field<T> last = this..put(f.namef);
             if (last != null)
             {
                 throw new IllegalStateException(last + " and " + f
                         + " cannot have the same name.");
             }
             if ([f.number] != null)
             {
                 throw new IllegalStateException([f.number]
                         + " and " + f + " cannot have the same number.");
             }
 
             [f.number] = f;
             // f.owner = this;
         }
 
          = new RuntimePipeSchema<>(this);
     }
 
     @SuppressWarnings("unchecked")
     public MappedSchema(Class<T> typeClassCollection<Field<T>> fields,
             int lastFieldNumber)
     {
         this. = typeClass;
          = new HashMap<>();
          = (Field<T>[]) new Field<?>[lastFieldNumber + 1];
         for (Field<T> f : fields)
         {
             Field<T> last = this..put(f.namef);
             if (last != null)
             {
                 throw new IllegalStateException(last + " and " + f
                         + " cannot have the same name.");
             }
             if ([f.number] != null)
             {
                 throw new IllegalStateException([f.number]
                         + " and " + f + " cannot have the same number.");
             }
 
             [f.number] = f;
             // f.owner = this;
         }
        this. = (Field<T>[]) new Field<?>[fields.size()];
        for (int i = 1, j = 0; i < .i++)
        {
            if ([i] != null)
                this.[j++] = [i];
        }
         = new RuntimePipeSchema<>(this);
    }
    @SuppressWarnings("unchecked")
    public MappedSchema(Class<T> typeClassMap<StringField<T>> fieldsByName,
            int lastFieldNumber)
    {
        this. = typeClass;
        this. = fieldsByName;
        Collection<Field<T>> fields = fieldsByName.values();
         = (Field<T>[]) new Field<?>[lastFieldNumber + 1];
        for (Field<T> f : fields)
        {
            if ([f.number] != null)
            {
                throw new IllegalStateException([f.number]
                        + " and " + f + " cannot have the same number.");
            }
            [f.number] = f;
            // f.owner = this;
        }
        this. = (Field<T>[]) new Field<?>[fields.size()];
        for (int i = 1, j = 0; i < .i++)
        {
            if ([i] != null)
                this.[j++] = [i];
        }
         = new RuntimePipeSchema<>(this);
    }

    
Returns the message's total number of fields.
    public int getFieldCount()
    {
        return .;
    }
    @Override
    public Class<T> typeClass()
    {
        return ;
    }
    @Override
    public String messageName()
    {
        return .getSimpleName();
    }
    @Override
    public String messageFullName()
    {
        return .getName();
    }
    @Override
    public String getFieldName(int number)
    {
        // only called on writes
        final Field<T> field = number < . ? [number]
                : null;
        return field == null ? null : field.name;
    }
    @Override
    public int getFieldNumber(String name)
    {
        final Field<T> field = .get(name);
        return field == null ? 0 : field.number;
    }
    @Override
    public final void mergeFrom(Input input, T messagethrows IOException
    {
        for (int number = input.readFieldNumber(this); number != 0; number = input
                .readFieldNumber(this))
        {
            final Field<T> field = number < . ? [number]
                    : null;
            if (field == null)
                input.handleUnknownField(numberthis);
            else
                field.mergeFrom(inputmessage);
        }
    }
    @Override
    public final void writeTo(Output output, T messagethrows IOException
    {
        for (Field<T> f : )
            f.writeTo(outputmessage);
    }

    
Returns the pipe schema linked to this.
    public Pipe.Schema<T> getPipeSchema()
    {
        return ;
    }

    
Represents a field of a message/pojo.
    public static abstract class Field<T>
    {
        public final FieldType type;
        public final int number;
        public final String name;
        public final boolean repeated;
        public final int groupFilter;
        // public final Tag tag;
        public Field(FieldType typeint numberString nameboolean repeated,
                Tag tag)
        {
            this. = type;
            this. = number;
            this. = name;
            this. = repeated;
            this. = tag == null ? 0 : tag.groupFilter();
            // this.tag = tag;
        }
        public Field(FieldType typeint numberString nameTag tag)
        {
            this(typenumbernamefalsetag);
        }

        
Writes the value of a field to the output.
        protected abstract void writeTo(Output output, T message)
                throws IOException;

        
Reads the field value into the message.
        protected abstract void mergeFrom(Input input, T message)
                throws IOException;

        
Transfer the input field to the output field.
        protected abstract void transfer(Pipe pipeInput inputOutput output,
                boolean repeatedthrows IOException;
    }
New to GrepCode? Check out our FAQ X