io.protostuff.CollectionSchema Java Examples

The following examples show how to use io.protostuff.CollectionSchema. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FastIdStrategy.java    From turbo-rpc with Apache License 2.0 6 votes vote down vote up
@Override
protected CollectionSchema.MessageFactory getCollectionFactory(Class<?> clazz) {
	final String className = clazz.getName();
	CollectionSchema.MessageFactory factory = collectionMapping.get(className);
	if (factory == null) {
		if (className.startsWith("java.util")) {
			factory = CollectionSchema.MessageFactories.valueOf(clazz.getSimpleName());
		} else {
			factory = new RuntimeCollectionFactory(clazz);
			CollectionSchema.MessageFactory f = collectionMapping.putIfAbsent(className, factory);
			if (f != null)
				factory = f;
		}
	}

	return factory;
}
 
Example #2
Source File: FastIdStrategy.java    From turbo-rpc with Apache License 2.0 6 votes vote down vote up
@Override
protected CollectionSchema.MessageFactory resolveCollectionFrom(Input input) throws IOException {
	final String className = input.readString();
	CollectionSchema.MessageFactory factory = collectionMapping.get(className);
	if (factory == null) {
		if (className.indexOf('.') == -1) {
			factory = CollectionSchema.MessageFactories.valueOf(className);
		} else {
			factory = new RuntimeCollectionFactory(RuntimeEnv.loadClass(className));
			CollectionSchema.MessageFactory f = collectionMapping.putIfAbsent(className, factory);
			if (f != null)
				factory = f;
		}
	}

	return factory;
}
 
Example #3
Source File: ExplicitIdStrategy.java    From protostuff with Apache License 2.0 6 votes vote down vote up
/**
 * Collection ids start at 1.
 */
@Override
public <T extends Collection<?>> Registry registerCollection(
        CollectionSchema.MessageFactory factory, int id)
{
    if (id < 1)
        throw new IllegalArgumentException("collection ids start at 1.");

    if (id >= strategy.collections.size())
        grow(strategy.collections, id + 1);
    else if (strategy.collections.get(id) != null)
    {
        throw new IllegalArgumentException("Duplicate id registration: " + id +
                " (" + factory.typeClass() + ")");
    }

    RegisteredCollectionFactory rf = new RegisteredCollectionFactory(id, factory);
    strategy.collections.set(id, rf);
    // just in case
    if (strategy.collectionMapping.put(factory.typeClass(), rf) != null)
        throw new IllegalArgumentException("Duplicate registration for: " + factory.typeClass());

    return this;
}
 
Example #4
Source File: EnumIO.java    From protostuff with Apache License 2.0 6 votes vote down vote up
private static <E extends Enum<E>> CollectionSchema.MessageFactory newEnumSetFactory(
        final EnumIO<E> eio)
{
    return new CollectionSchema.MessageFactory()
    {
        @Override
        @SuppressWarnings("unchecked")
        public <V> Collection<V> newMessage()
        {
            return (Collection<V>) eio.newEnumSet();
        }

        @Override
        public Class<?> typeClass()
        {
            return EnumSet.class;
        }
    };
}
 
Example #5
Source File: IncrementalIdStrategy.java    From protostuff with Apache License 2.0 6 votes vote down vote up
/**
 * Collection ids start at 1.
 */
@Override
public <T extends Collection<?>> Registry registerCollection(
        CollectionSchema.MessageFactory factory, int id)
{
    if (id < 1)
        throw new IllegalArgumentException("collection ids start at 1.");

    if (id >= strategy.collectionIdStart)
        throw new IllegalArgumentException("collection ids must be lesser than " + strategy.collectionIdStart);
    else if (strategy.collections.get(id) != null)
    {
        throw new IllegalArgumentException("Duplicate id registration: " + id +
                " (" + factory.typeClass() + ")");
    }

    RuntimeCollectionFactory rf = new RuntimeCollectionFactory();
    rf.id = id;
    rf.factory = factory;
    strategy.collections.set(id, rf);
    // just in case
    if (strategy.collectionMapping.put(factory.typeClass(), rf) != null)
        throw new IllegalArgumentException("Duplicate registration for: " + factory.typeClass());

    return this;
}
 
Example #6
Source File: DefaultIdStrategy.java    From protostuff with Apache License 2.0 6 votes vote down vote up
@Override
protected CollectionSchema.MessageFactory getCollectionFactory(
        Class<?> clazz)
{
    final String className = clazz.getName();
    CollectionSchema.MessageFactory factory = collectionMapping
            .get(className);
    if (factory == null)
    {
        if (className.startsWith("java.util") && CollectionSchema.MessageFactories.accept(clazz.getSimpleName()))
        {
            factory = CollectionSchema.MessageFactories.valueOf(clazz
                    .getSimpleName());
        }
        else
        {
            factory = new RuntimeCollectionFactory(clazz);
            CollectionSchema.MessageFactory f = collectionMapping
                    .putIfAbsent(className, factory);
            if (f != null)
                factory = f;
        }
    }

    return factory;
}
 
Example #7
Source File: DefaultIdStrategy.java    From protostuff with Apache License 2.0 6 votes vote down vote up
@Override
protected void writeCollectionIdTo(Output output, int fieldNumber,
        Class<?> clazz) throws IOException
{
    final CollectionSchema.MessageFactory factory = collectionMapping
            .get(clazz.getName());
    if (factory == null && clazz.getName().startsWith("java.util")
        && CollectionSchema.MessageFactories.accept(clazz.getSimpleName()))
    {
        // jdk collection
        // better not to register the jdk collection if using this strategy
        // as it saves space by not writing the full package
        output.writeString(fieldNumber, clazz.getSimpleName(), false);
    }
    else
    {
        output.writeString(fieldNumber, clazz.getName(), false);
    }
}
 
Example #8
Source File: DefaultIdStrategy.java    From protostuff with Apache License 2.0 6 votes vote down vote up
@Override
protected CollectionSchema.MessageFactory resolveCollectionFrom(Input input)
        throws IOException
{
    final String className = input.readString();
    CollectionSchema.MessageFactory factory = collectionMapping
            .get(className);
    if (factory == null)
    {
        if (className.indexOf('.') == -1 && CollectionSchema.MessageFactories.accept(className))
        {
            factory = CollectionSchema.MessageFactories.valueOf(className);
        }
        else
        {
            factory = new RuntimeCollectionFactory(
                    RuntimeEnv.loadClass(className));
            CollectionSchema.MessageFactory f = collectionMapping
                    .putIfAbsent(className, factory);
            if (f != null)
                factory = f;
        }
    }

    return factory;
}
 
Example #9
Source File: IncrementalRuntimeObjectSchemaTest.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
public void postCreate()
{
    r.registerCollection(CollectionSchema.MessageFactories.ArrayList, 1)
            .registerCollection(CollectionSchema.MessageFactories.HashSet, 2)
            .registerCollection(CustomArrayList.MESSAGE_FACTORY, 3);

    r.registerMap(MapSchema.MessageFactories.HashMap, 1)
            .registerMap(MapSchema.MessageFactories.LinkedHashMap, 2)
            .registerMap(CustomHashMap.MESSAGE_FACTORY, 3);

    r.registerEnum(Size.class, 1)
            .registerEnum(GuitarPickup.class, 2);

    r.registerPojo(AcousticGuitar.class, 1)
            .registerPojo(BassGuitar.class, 2)
            .registerPojo(Pojo.class, 3)
            .registerPojo(PojoWithArray.class, 4)
            .registerPojo(PojoWithArray2D.class, 5)
            .registerPojo(PojoWithCollection.class, 6)
            .registerPojo(PojoWithMap.class, 7)
            .registerPojo(Bat.SCHEMA, Bat.PIPE_SCHEMA, 8)
            .registerPojo(WrapsBat.class, 9)
            .registerPojo(PojoWithCustomArrayListAndHashMap.class, 10);

    r.registerDelegate(new ShortArrayDelegate(), 1);
    r.registerDelegate(SINGLETON_DELEGATE, 2);

    r = null;
}
 
Example #10
Source File: ExplicitIdStrategy.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
protected CollectionSchema.MessageFactory resolveCollectionFrom(Input input)
        throws IOException
{
    final int id = input.readUInt32();

    final CollectionSchema.MessageFactory factory = id < collections.size() ?
            collections.get(id) : null;
    if (factory == null)
        throw new UnknownTypeException("collection id: " + id + " (Outdated registry)");

    return factory;
}
 
Example #11
Source File: ExplicitIdStrategy.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
protected CollectionSchema.MessageFactory getCollectionFactory(Class<?> clazz)
{
    final RegisteredCollectionFactory rf = collectionMapping.get(clazz);
    if (rf == null)
    {
        if (clazz.getName().startsWith("java.util"))
            return CollectionSchema.MessageFactories.valueOf(clazz.getSimpleName());

        throw new UnknownTypeException("collection: " + clazz);
    }

    return rf;
}
 
Example #12
Source File: IncrementalIdStrategy.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
protected CollectionSchema.MessageFactory resolveCollectionFrom(Input input)
        throws IOException
{
    final int id = input.readUInt32();

    final RuntimeCollectionFactory factory = id < collections.size() ?
            collections.get(id) : null;
    if (factory == null)
        throw new UnknownTypeException("Unknown collection id: " + id);

    return factory;
}
 
Example #13
Source File: IncrementalIdStrategy.java    From protostuff with Apache License 2.0 5 votes vote down vote up
private RuntimeCollectionFactory getRuntimeCollectionFactory(Class<?> clazz)
{
    RuntimeCollectionFactory rfactory = collectionMapping.get(clazz);
    if (rfactory == null)
    {
        rfactory = new RuntimeCollectionFactory();
        RuntimeCollectionFactory f = collectionMapping.putIfAbsent(
                clazz, rfactory);
        if (f != null)
            rfactory = f;
        else
        {
            if (clazz.getName().startsWith("java.util")
                && CollectionSchema.MessageFactories.accept(clazz.getSimpleName()))
            {
                rfactory.factory = CollectionSchema.MessageFactories.valueOf(
                        clazz.getSimpleName());
            }
            else
            {
                rfactory.instantiator = RuntimeEnv.newInstantiator(clazz);
                rfactory.collectionClass = clazz;
            }

            int id = collectionId.getAndIncrement();
            collections.set(id, rfactory);

            // commit
            rfactory.id = id;
        }
    }

    return rfactory;
}
 
Example #14
Source File: IncrementalIdStrategy.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
public IdStrategy create()
{
    return new IncrementalIdStrategy(
            CollectionSchema.MessageFactories.values().length, 1,
            MapSchema.MessageFactories.values().length, 1,
            16, 1, // enums
            64, 1); // pojos
}
 
Example #15
Source File: RuntimeCollectionField.java    From protostuff with Apache License 2.0 5 votes vote down vote up
public RuntimeCollectionField(FieldType type, int number, String name,
        Tag tag, MessageFactory messageFactory, boolean allowNullElement)
{
    super(type, number, name, false, tag);
    schema = new CollectionSchema<V>(messageFactory, allowNullElement)
    {
        @Override
        protected void addValueFrom(Input input, Collection<V> collection)
                throws IOException
        {
            RuntimeCollectionField.this.addValueFrom(input, collection);
        }

        @Override
        protected void writeValueTo(Output output, int fieldNumber,
                V value, boolean repeated) throws IOException
        {
            RuntimeCollectionField.this.writeValueTo(output, fieldNumber,
                    value, repeated);
        }

        @Override
        protected void transferValue(Pipe pipe, Input input, Output output,
                int number, boolean repeated) throws IOException
        {
            RuntimeCollectionField.this.transferValue(pipe, input, output,
                    number, repeated);
        }
    };
}
 
Example #16
Source File: EnumIO.java    From protostuff with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the factory for an EnumSet (lazy).
 */
public CollectionSchema.MessageFactory getEnumSetFactory()
{
    CollectionSchema.MessageFactory enumSetFactory = this.enumSetFactory;
    if (enumSetFactory == null)
    {
        synchronized (this)
        {
            if ((enumSetFactory = this.enumSetFactory) == null)
                this.enumSetFactory = enumSetFactory = newEnumSetFactory(this);
        }
    }
    return enumSetFactory;
}
 
Example #17
Source File: FastIdStrategy.java    From turbo-rpc with Apache License 2.0 5 votes vote down vote up
@Override
protected void writeCollectionIdTo(Output output, int fieldNumber, Class<?> clazz) throws IOException {
	@SuppressWarnings("unlikely-arg-type")
	final CollectionSchema.MessageFactory factory = collectionMapping.get(clazz);
	if (factory == null && clazz.getName().startsWith("java.util")) {
		// jdk collection
		// better not to register the jdk collection if using this strategy
		// as it saves space by not writing the full package
		output.writeString(fieldNumber, clazz.getSimpleName(), false);
	} else {
		output.writeString(fieldNumber, clazz.getName(), false);
	}
}
 
Example #18
Source File: AbstractRuntimeObjectSchemaTest.java    From protostuff with Apache License 2.0 4 votes vote down vote up
public HashMapInnerKeySetDelegate(CollectionSchema<T> schema)
{
    this.schema = schema;
}
 
Example #19
Source File: AbstractRuntimeObjectSchemaTest.java    From protostuff with Apache License 2.0 4 votes vote down vote up
public ImmutableListAsDelegate(CollectionSchema<T> schema)
{
    this.schema = schema;
}
 
Example #20
Source File: DefaultIdStrategy.java    From protostuff with Apache License 2.0 4 votes vote down vote up
/**
 * Registers a collection. Returns true if registration is successful.
 */
public boolean registerCollection(CollectionSchema.MessageFactory factory)
{
    return null == collectionMapping.putIfAbsent(factory.typeClass()
            .getName(), factory);
}
 
Example #21
Source File: IdStrategy.java    From protostuff with Apache License 2.0 4 votes vote down vote up
protected abstract CollectionSchema.MessageFactory resolveCollectionFrom(
Input input) throws IOException;
 
Example #22
Source File: IncrementalIdStrategy.java    From protostuff with Apache License 2.0 4 votes vote down vote up
@Override
protected CollectionSchema.MessageFactory getCollectionFactory(Class<?> clazz)
{
    return getRuntimeCollectionFactory(clazz);
}
 
Example #23
Source File: IdStrategy.java    From Jupiter with Apache License 2.0 4 votes vote down vote up
protected abstract CollectionSchema.MessageFactory resolveCollectionFrom(
Input input) throws IOException;
 
Example #24
Source File: ExplicitIdStrategy.java    From protostuff with Apache License 2.0 4 votes vote down vote up
public RegisteredCollectionFactory(int id, CollectionSchema.MessageFactory factory)
{
    this.id = id;
    this.factory = factory;
}
 
Example #25
Source File: ExplicitRuntimeObjectSchemaTest.java    From protostuff with Apache License 2.0 4 votes vote down vote up
@Override
public void postCreate()
{
    r.registerCollection(CollectionSchema.MessageFactories.ArrayList, 1)
            .registerCollection(CollectionSchema.MessageFactories.HashSet, 2)
            .registerCollection(CustomArrayList.MESSAGE_FACTORY, 3)
            .registerCollection(CollectionSchema.MessageFactories.LinkedList, 4)
            .registerCollection(CollectionSchema.MessageFactories.TreeSet, 5);

    r.registerMap(MapSchema.MessageFactories.HashMap, 1)
            .registerMap(MapSchema.MessageFactories.LinkedHashMap, 2)
            .registerMap(CustomHashMap.MESSAGE_FACTORY, 3)
            .registerMap(MapSchema.MessageFactories.TreeMap, 4);

    r.registerEnum(Size.class, 1)
            .registerEnum(GuitarPickup.class, 2);

    r.registerPojo(AcousticGuitar.class, 1)
            .registerPojo(BassGuitar.class, 2)
            .registerPojo(Pojo.class, 3)
            .registerPojo(PojoWithArray.class, 4)
            .registerPojo(PojoWithArray2D.class, 5)
            .registerPojo(PojoWithCollection.class, 6)
            .registerPojo(PojoWithMap.class, 7)
            .registerPojo(Bat.SCHEMA, Bat.PIPE_SCHEMA, 8)
            .registerPojo(WrapsBat.class, 9)
            .registerPojo(PojoWithCustomArrayListAndHashMap.class, 10)
            .registerPojo(PojoWithClassFields.class, 11)
            .registerPojo(PojoWithObjectCollectionFields.class, 12)
            .registerPojo(PojoWithObjectCollectionNullKV.class, 13)
            .registerPojo(PojoWithObjectMapFields.class, 14)
            .registerPojo(PojoWithSingletonMapNullKV.class, 15)
            .registerPojo(PojoWithThrowable.class, 16)
            .registerPojo(Throwable.class, 17)
            .registerPojo(Exception.class, 18)
            .registerPojo(RuntimeException.class, 19)
            .registerPojo(PojoWithThrowableArray.class, 20)
            .registerPojo(PojoWithSingletonAsDelegate.class, 21)
            .registerPojo(PojoWithShortArrayAsDelegate.class, 22)
            .registerPojo(StackTraceElement.class, 23);

    r.registerDelegate(new ShortArrayDelegate(), 1);
    r.registerDelegate(SINGLETON_DELEGATE, 2);

    r = null;
}
 
Example #26
Source File: FastIdStrategy.java    From turbo-rpc with Apache License 2.0 4 votes vote down vote up
/**
 * Registers a collection. Returns true if registration is successful.
 */
public boolean registerCollection(CollectionSchema.MessageFactory factory) {
	return null == collectionMapping.putIfAbsent(factory.typeClass().getName(), factory);
}
 
Example #27
Source File: IdStrategy.java    From protostuff with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the {@link CollectionSchema.MessageFactory}. The callers (internal field factories) are responsible that
 * the class provided implements {@link Collection}.
 */
protected abstract CollectionSchema.MessageFactory getCollectionFactory(
        Class<?> clazz);
 
Example #28
Source File: IdStrategy.java    From Jupiter with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the {@link CollectionSchema.MessageFactory}. The callers (internal field factories) are responsible that
 * the class provided implements {@link Collection}.
 */
protected abstract CollectionSchema.MessageFactory getCollectionFactory(
        Class<?> clazz);
 
Example #29
Source File: NumericIdStrategy.java    From protostuff with Apache License 2.0 2 votes vote down vote up
/**
 * Collection ids start at 1.
 */
public <T extends Collection<?>> Registry registerCollection(
        CollectionSchema.MessageFactory factory, int id);