com.tangosol.io.pof.reflect.Codecs Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of coherence Show documentation
Show all versions of coherence Show documentation
Oracle Coherence Community Edition
/*
* Copyright (c) 2000, 2020, Oracle and/or its affiliates.
*
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
*/
package com.tangosol.io.pof.reflect;
import com.tangosol.io.pof.PofReader;
import com.tangosol.io.pof.PofWriter;
import com.tangosol.util.Base;
import com.tangosol.util.LongArray;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;
/**
* Codecs is a container for accessing default {@link Codec} implementations.
*
* @author hr
* @since 3.7.1
*/
public class Codecs
{
// ----- factory methods ------------------------------------------------
/**
* Return a {@link Codec} based on the provided Class.
*
* The provided Class should implement one of:
*
* - Codec - the Codec implementation should have a no-arg constructor
* which will be instantiated and returned.
* - Collection - the provided class should implement the {@link Collection}
* interface and have a no-arg constructor. A Codec
* supporting Collections will be returned
* - Map - the provided class should implement the {@link Map} interface
* and have a no-arg constructor. A Codec supporting Maps
* will be returned
* - LongArray - the provided class should implement the {@link LongArray} interface
* and have a no-arg constructor. A Codec supporting LongArrays
* will be returned
* - T[] - the provided class should be an array and the component
* type of the array should have a no-arg constructor. A Codec
* supporting arrays will be returned
*
*
* @param clz a Class that implements Codec or is one of the supported
* types
*
* @return a Codec that supports encoding and decoding of objects of the
* specified type
*/
@SuppressWarnings("unchecked")
public static Codec getCodec(Class> clz)
{
if (DefaultCodec.class.equals(clz))
{
return DEFAULT_CODEC;
}
if (Codec.class.isAssignableFrom(clz))
{
try
{
return (Codec) clz.newInstance();
}
catch (Exception e)
{
throw Base.ensureRuntimeException(e, "Unable to instantiate class (" + clz +
") that should implement Codec and have a no-arg constructor");
}
}
if (clz.isArray())
{
return new ArrayCodec(clz.getComponentType());
}
if (Collection.class.isAssignableFrom(clz))
{
return new CollectionCodec((Class extends Collection