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;
 
 import static io.protostuff.runtime.RuntimeFieldFactory.ID_POJO;
 import static io.protostuff.runtime.RuntimeFieldFactory.STR_POJO;
 
 
This schema delegates to another schema derived from the input.

Author(s):
David Yu
Created:
Jan 21, 2011
 
 public abstract class DerivativeSchema implements Schema<Object>
 {
 
     public final IdStrategy strategy;
 
     public DerivativeSchema(IdStrategy strategy)
     {
         this. = strategy;
     }
 
     @Override
     public String getFieldName(int number)
     {
         return number ==  ?  : null;
     }
 
     @Override
     public int getFieldNumber(String name)
     {
         return name.length() == 1 && name.charAt(0) == '_' ?  : 0;
     }
 
     @Override
     public boolean isInitialized(Object owner)
     {
         return true;
     }
 
     @Override
     public String messageFullName()
     {
         return Object.class.getName();
     }
 
     @Override
     public String messageName()
     {
         return Object.class.getSimpleName();
     }
 
     @Override
     public Object newMessage()
     {
         // cannot instantiate because the type is dynamic.
         throw new UnsupportedOperationException();
     }
 
     @Override
     public Class<? super ObjecttypeClass()
     {
         return Object.class;
     }

    
Delegates to the schema derived from the input. The owner owns the message (polymorphic) that is tied to this schema.
 
     @Override
     public void mergeFrom(Input inputfinal Object ownerthrows IOException
     {
         final int first = input.readFieldNumber(this);
         if (first != )
             throw new ProtostuffException("order not preserved.");
 
         doMergeFrom(input,
                .resolvePojoFrom(input).getSchema(), owner);
    }

    
Delegates to the schema derived from the value.
    @Override
    @SuppressWarnings("unchecked")
    public void writeTo(final Output outputfinal Object value)
            throws IOException
    {
        final Schema<Objectschema = .writePojoIdTo(output,
                (Class<Object>) value.getClass()).getSchema();
        if (output instanceof StatefulOutput)
        {
            // update using the derived schema.
            ((StatefulOutputoutput).updateLast(schemathis);
        }
        // write the rest of the fields of the exact type
        schema.writeTo(outputvalue);
    }

    
This pipe schema delegates to another schema derived from the input.
    public final Pipe.Schema<ObjectpipeSchema = new Pipe.Schema<Object>(
            DerivativeSchema.this)
    {
        @Override
        public void transfer(Pipe pipeInput inputOutput output)
                throws IOException
        {
            final int first = input.readFieldNumber(DerivativeSchema.this);
            if (first != )
                throw new ProtostuffException("order not preserved.");
            final Pipe.Schema<ObjectpipeSchema = .transferPojoId(
                    inputoutput).getPipeSchema();
            if (output instanceof StatefulOutput)
            {
                // update using the derived schema.
                ((StatefulOutputoutput).updateLast(pipeSchemathis);
            }
            Pipe.transferDirect(pipeSchemapipeinputoutput);
        }
    };
    protected abstract void doMergeFrom(Input input,
            Schema<ObjectderivedSchemaObject ownerthrows IOException;
New to GrepCode? Check out our FAQ X