io.protostuff.runtime.NumberSchema Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of protostuff-runtime Show documentation
Show all versions of protostuff-runtime Show documentation
protobuf serialization for pre-existing objects
//================================================================================
//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 static io.protostuff.runtime.RuntimeFieldFactory.BIGDECIMAL;
import static io.protostuff.runtime.RuntimeFieldFactory.BIGINTEGER;
import static io.protostuff.runtime.RuntimeFieldFactory.BYTE;
import static io.protostuff.runtime.RuntimeFieldFactory.DOUBLE;
import static io.protostuff.runtime.RuntimeFieldFactory.FLOAT;
import static io.protostuff.runtime.RuntimeFieldFactory.ID_BIGDECIMAL;
import static io.protostuff.runtime.RuntimeFieldFactory.ID_BIGINTEGER;
import static io.protostuff.runtime.RuntimeFieldFactory.ID_BYTE;
import static io.protostuff.runtime.RuntimeFieldFactory.ID_DOUBLE;
import static io.protostuff.runtime.RuntimeFieldFactory.ID_FLOAT;
import static io.protostuff.runtime.RuntimeFieldFactory.ID_INT32;
import static io.protostuff.runtime.RuntimeFieldFactory.ID_INT64;
import static io.protostuff.runtime.RuntimeFieldFactory.ID_POJO;
import static io.protostuff.runtime.RuntimeFieldFactory.ID_SHORT;
import static io.protostuff.runtime.RuntimeFieldFactory.INT32;
import static io.protostuff.runtime.RuntimeFieldFactory.INT64;
import static io.protostuff.runtime.RuntimeFieldFactory.SHORT;
import static io.protostuff.runtime.RuntimeFieldFactory.STR_BIGDECIMAL;
import static io.protostuff.runtime.RuntimeFieldFactory.STR_BIGINTEGER;
import static io.protostuff.runtime.RuntimeFieldFactory.STR_BYTE;
import static io.protostuff.runtime.RuntimeFieldFactory.STR_DOUBLE;
import static io.protostuff.runtime.RuntimeFieldFactory.STR_FLOAT;
import static io.protostuff.runtime.RuntimeFieldFactory.STR_INT32;
import static io.protostuff.runtime.RuntimeFieldFactory.STR_INT64;
import static io.protostuff.runtime.RuntimeFieldFactory.STR_POJO;
import static io.protostuff.runtime.RuntimeFieldFactory.STR_SHORT;
import java.io.IOException;
import io.protostuff.GraphInput;
import io.protostuff.Input;
import io.protostuff.Output;
import io.protostuff.Pipe;
import io.protostuff.ProtostuffException;
import io.protostuff.Schema;
import io.protostuff.StatefulOutput;
/**
* Used when the type is {@link java.lang.Number}.
*
* @author David Yu
* @created Apr 30, 2012
*/
public abstract class NumberSchema extends PolymorphicSchema
{
static String name(int number)
{
switch (number)
{
case ID_BYTE:
return STR_BYTE;
case ID_SHORT:
return STR_SHORT;
case ID_INT32:
return STR_INT32;
case ID_INT64:
return STR_INT64;
case ID_FLOAT:
return STR_FLOAT;
case ID_DOUBLE:
return STR_DOUBLE;
case ID_BIGDECIMAL:
return STR_BIGDECIMAL;
case ID_BIGINTEGER:
return STR_BIGINTEGER;
// AtomicInteger and AtomicLong
case ID_POJO:
return STR_POJO;
default:
return null;
}
}
static int number(String name)
{
if (name.length() != 1)
return 0;
switch (name.charAt(0))
{
case '_':
return 127;
case 'b':
return 2;
case 'd':
return 4;
case 'e':
return 5;
case 'f':
return 6;
case 'g':
return 7;
case 'h':
return 8;
case 'l':
return 12;
case 'm':
return 13;
default:
return 0;
}
}
protected final Pipe.Schema