com.dyuproject.protostuff.runtime.PolymorphicSchemaFactories 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 com.dyuproject.protostuff.runtime;
import java.util.Collection;
import java.util.Map;
import com.dyuproject.protostuff.runtime.PolymorphicSchema.Handler;
/**
* Polymorphic types.
*
* @author David Yu
* @created Apr 30, 2012
*/
public enum PolymorphicSchemaFactories implements PolymorphicSchema.Factory
{
ARRAY
{
public PolymorphicSchema newSchema(IdStrategy strategy, final Handler handler)
{
return new ArraySchema(strategy)
{
protected void setValue(Object value, Object owner)
{
handler.setValue(value, owner);
}
};
}
},
NUMBER
{
public PolymorphicSchema newSchema(IdStrategy strategy, final Handler handler)
{
return new NumberSchema(strategy)
{
protected void setValue(Object value, Object owner)
{
handler.setValue(value, owner);
}
};
}
},
CLASS
{
public PolymorphicSchema newSchema(IdStrategy strategy, final Handler handler)
{
return new ClassSchema(strategy)
{
protected void setValue(Object value, Object owner)
{
handler.setValue(value, owner);
}
};
}
},
ENUM
{
public PolymorphicSchema newSchema(IdStrategy strategy, final Handler handler)
{
return new PolymorphicEnumSchema(strategy)
{
protected void setValue(Object value, Object owner)
{
handler.setValue(value, owner);
}
};
}
},
COLLECTION
{
public PolymorphicSchema newSchema(IdStrategy strategy, final Handler handler)
{
return new PolymorphicCollectionSchema(strategy)
{
protected void setValue(Object value, Object owner)
{
handler.setValue(value, owner);
}
};
}
},
MAP
{
public PolymorphicSchema newSchema(IdStrategy strategy, final Handler handler)
{
return new PolymorphicMapSchema(strategy)
{
protected void setValue(Object value, Object owner)
{
handler.setValue(value, owner);
}
};
}
},
THROWABLE
{
public PolymorphicSchema newSchema(IdStrategy strategy, final Handler handler)
{
return new PolymorphicThrowableSchema(strategy)
{
protected void setValue(Object value, Object owner)
{
handler.setValue(value, owner);
}
};
}
},
OBJECT
{
public PolymorphicSchema newSchema(IdStrategy strategy, final Handler handler)
{
return new ObjectSchema(strategy)
{
protected void setValue(Object value, Object owner)
{
handler.setValue(value, owner);
}
};
}
}
;
public static PolymorphicSchema.Factory getFactoryFromField(Class> clazz)
{
if(clazz.isArray())
return ARRAY;
if(Number.class == clazz)
return NUMBER;
if(Class.class == clazz)
return CLASS;
if(Enum.class == clazz)
return ENUM;
if(Map.class.isAssignableFrom(clazz))
return MAP;
if(Collection.class.isAssignableFrom(clazz))
return COLLECTION;
if(Throwable.class.isAssignableFrom(clazz))
return THROWABLE;
return OBJECT;
}
public static PolymorphicSchema.Factory getFactoryFromRepeatedValueGenericType(
Class> clazz)
{
if(clazz.isArray())
return ARRAY;
if(Number.class == clazz)
return NUMBER;
if(Class.class == clazz)
return CLASS;
if(Enum.class == clazz)
return ENUM;
if(Throwable.class.isAssignableFrom(clazz))
return THROWABLE;
if(Object.class == clazz)
return OBJECT;
return null;
}
public static PolymorphicSchema getSchemaFromCollectionOrMapGenericType(
Class> clazz, IdStrategy strategy)
{
if(clazz.isArray())
return strategy.ARRAY_ELEMENT_SCHEMA;
if(Number.class == clazz)
return strategy.NUMBER_ELEMENT_SCHEMA;
if(Class.class == clazz)
return strategy.CLASS_ELEMENT_SCHEMA;
if(Enum.class == clazz)
return strategy.POLYMORPHIC_ENUM_ELEMENT_SCHEMA;
if(Throwable.class.isAssignableFrom(clazz))
return strategy.POLYMORPHIC_THROWABLE_ELEMENT_SCHEMA;
if(Object.class == clazz)
return strategy.OBJECT_ELEMENT_SCHEMA;
return null;
}
}