io.protostuff.MapSchema Java Examples

The following examples show how to use io.protostuff.MapSchema. 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 MapSchema.MessageFactory getMapFactory(Class<?> clazz) {
	final String className = clazz.getName();
	MapSchema.MessageFactory factory = mapMapping.get(className);
	if (factory == null) {
		if (className.startsWith("java.util")) {
			factory = MapSchema.MessageFactories.valueOf(clazz.getSimpleName());
		} else {
			factory = new RuntimeMapFactory(clazz);
			MapSchema.MessageFactory f = mapMapping.putIfAbsent(className, factory);
			if (f != null)
				factory = f;
		}
	}

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

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

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

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

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

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

    return this;
}
 
Example #4
Source File: DefaultIdStrategy.java    From protostuff with Apache License 2.0 6 votes vote down vote up
@Override
protected MapSchema.MessageFactory resolveMapFrom(Input input)
        throws IOException
{
    final String className = input.readString();
    MapSchema.MessageFactory factory = mapMapping.get(className);
    if (factory == null)
    {
        if (className.indexOf('.') == -1 && MapSchema.MessageFactories.accept(className))
        {
            factory = MapSchema.MessageFactories.valueOf(className);
        }
        else
        {
            factory = new RuntimeMapFactory(RuntimeEnv.loadClass(className));
            MapSchema.MessageFactory f = mapMapping.putIfAbsent(className,
                    factory);
            if (f != null)
                factory = f;
        }
    }

    return factory;
}
 
Example #5
Source File: DefaultIdStrategy.java    From protostuff with Apache License 2.0 6 votes vote down vote up
@Override
protected void writeMapIdTo(Output output, int fieldNumber, Class<?> clazz)
        throws IOException
{
    final MapSchema.MessageFactory factory = mapMapping.get(clazz);
    if (factory == null && clazz.getName().startsWith("java.util")
        && MapSchema.MessageFactories.accept(clazz.getSimpleName()))
    {
        // jdk map
        // better not to register the jdk map 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 #6
Source File: DefaultIdStrategy.java    From protostuff with Apache License 2.0 6 votes vote down vote up
@Override
protected MapSchema.MessageFactory getMapFactory(Class<?> clazz)
{
    final String className = clazz.getName();
    MapSchema.MessageFactory factory = mapMapping.get(className);
    if (factory == null)
    {
        if (className.startsWith("java.util") && MapSchema.MessageFactories.accept(clazz.getSimpleName()))
        {
            factory = MapSchema.MessageFactories.valueOf(clazz
                    .getSimpleName());
        }
        else
        {
            factory = new RuntimeMapFactory(clazz);
            MapSchema.MessageFactory f = mapMapping.putIfAbsent(className,
                    factory);
            if (f != null)
                factory = f;
        }
    }

    return factory;
}
 
Example #7
Source File: EnumIO.java    From protostuff with Apache License 2.0 6 votes vote down vote up
private static <E extends Enum<E>> MapSchema.MessageFactory newEnumMapFactory(
        final EnumIO<E> eio)
{
    return new MapSchema.MessageFactory()
    {
        @Override
        @SuppressWarnings("unchecked")
        public <K, V> Map<K, V> newMessage()
        {
            return (Map<K, V>) eio.newEnumMap();
        }

        @Override
        public Class<?> typeClass()
        {
            return EnumMap.class;
        }
    };
}
 
Example #8
Source File: FastIdStrategy.java    From turbo-rpc with Apache License 2.0 6 votes vote down vote up
@Override
protected MapSchema.MessageFactory resolveMapFrom(Input input) throws IOException {
	final String className = input.readString();
	MapSchema.MessageFactory factory = mapMapping.get(className);
	if (factory == null) {
		if (className.indexOf('.') == -1) {
			factory = MapSchema.MessageFactories.valueOf(className);
		} else {
			factory = new RuntimeMapFactory(RuntimeEnv.loadClass(className));
			MapSchema.MessageFactory f = mapMapping.putIfAbsent(className, factory);
			if (f != null)
				factory = f;
		}
	}

	return factory;
}
 
Example #9
Source File: FastIdStrategy.java    From turbo-rpc with Apache License 2.0 5 votes vote down vote up
@Override
protected void writeMapIdTo(Output output, int fieldNumber, Class<?> clazz) throws IOException {
	@SuppressWarnings("unlikely-arg-type")
	final MapSchema.MessageFactory factory = mapMapping.get(clazz);
	if (factory == null && clazz.getName().startsWith("java.util")) {
		// jdk map
		// better not to register the jdk map 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 #10
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 #11
Source File: ExplicitIdStrategy.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
protected MapSchema.MessageFactory resolveMapFrom(Input input)
        throws IOException
{
    final int id = input.readUInt32();

    final MapSchema.MessageFactory factory = id < maps.size() ? maps.get(id) : null;
    if (factory == null)
        throw new UnknownTypeException("map id: " + id + " (Outdated registry)");

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

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

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

    final RuntimeMapFactory factory = id < maps.size() ? maps.get(id) : null;
    if (factory == null)
        throw new UnknownTypeException("Unknown map id: " + id);

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

            int id = mapId.getAndIncrement();
            maps.set(id, rfactory);

            // commit
            rfactory.id = id;
        }
    }

    return rfactory;
}
 
Example #15
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 #16
Source File: EnumIO.java    From protostuff with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the factory for an EnumMap (lazy).
 */
public MapSchema.MessageFactory getEnumMapFactory()
{
    MapSchema.MessageFactory enumMapFactory = this.enumMapFactory;
    if (enumMapFactory == null)
    {
        synchronized (this)
        {
            if ((enumMapFactory = this.enumMapFactory) == null)
                this.enumMapFactory = enumMapFactory = newEnumMapFactory(this);
        }
    }
    return enumMapFactory;
}
 
Example #17
Source File: IdStrategy.java    From Jupiter with Apache License 2.0 4 votes vote down vote up
protected abstract MapSchema.MessageFactory resolveMapFrom(Input input)
throws IOException;
 
Example #18
Source File: DefaultIdStrategy.java    From protostuff with Apache License 2.0 4 votes vote down vote up
/**
 * Registers a map. Returns true if registration is successful.
 */
public boolean registerMap(MapSchema.MessageFactory factory)
{
    return null == mapMapping.putIfAbsent(factory.typeClass().getName(),
            factory);
}
 
Example #19
Source File: IncrementalIdStrategy.java    From protostuff with Apache License 2.0 4 votes vote down vote up
@Override
protected MapSchema.MessageFactory getMapFactory(Class<?> clazz)
{
    return getRuntimeMapFactory(clazz);
}
 
Example #20
Source File: IdStrategy.java    From protostuff with Apache License 2.0 4 votes vote down vote up
protected abstract MapSchema.MessageFactory resolveMapFrom(Input input)
throws IOException;
 
Example #21
Source File: FastIdStrategy.java    From turbo-rpc with Apache License 2.0 4 votes vote down vote up
/**
 * Registers a map. Returns true if registration is successful.
 */
public boolean registerMap(MapSchema.MessageFactory factory) {
	return null == mapMapping.putIfAbsent(factory.typeClass().getName(), factory);
}
 
Example #22
Source File: RuntimeMapField.java    From protostuff with Apache License 2.0 4 votes vote down vote up
public RuntimeMapField(FieldType type, int number, String name, Tag tag,
        MessageFactory messageFactory)
{
    super(type, number, name, false, tag);
    schema = new MapSchema<K, V>(messageFactory)
    {
        @Override
        protected K readKeyFrom(Input input, MapWrapper<K, V> wrapper)
                throws IOException
        {
            return kFrom(input, wrapper);
        }

        @Override
        protected void putValueFrom(Input input, MapWrapper<K, V> wrapper,
                K key) throws IOException
        {
            vPutFrom(input, wrapper, key);
        }

        @Override
        protected void writeKeyTo(Output output, int fieldNumber, K key,
                boolean repeated) throws IOException
        {
            kTo(output, fieldNumber, key, repeated);
        }

        @Override
        protected void writeValueTo(Output output, int fieldNumber, V val,
                boolean repeated) throws IOException
        {
            vTo(output, fieldNumber, val, repeated);
        }

        @Override
        protected void transferKey(Pipe pipe, Input input, Output output,
                int number, boolean repeated) throws IOException
        {
            kTransfer(pipe, input, output, number, repeated);
        }

        @Override
        protected void transferValue(Pipe pipe, Input input, Output output,
                int number, boolean repeated) throws IOException
        {
            vTransfer(pipe, input, output, number, repeated);
        }
    };
}
 
Example #23
Source File: ExplicitIdStrategy.java    From protostuff with Apache License 2.0 4 votes vote down vote up
public RegisteredMapFactory(int id, MapSchema.MessageFactory factory)
{
    this.id = id;
    this.factory = factory;
}
 
Example #24
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 #25
Source File: IdStrategy.java    From Jupiter with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the {@link MapSchema.MessageFactory}. The callers (internal field factories}) are responsible that the
 * class provided implements {@link Map}.
 */
protected abstract MapSchema.MessageFactory getMapFactory(Class<?> clazz);
 
Example #26
Source File: IdStrategy.java    From protostuff with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the {@link MapSchema.MessageFactory}. The callers (internal field factories}) are responsible that the
 * class provided implements {@link Map}.
 */
protected abstract MapSchema.MessageFactory getMapFactory(Class<?> clazz);
 
Example #27
Source File: NumericIdStrategy.java    From protostuff with Apache License 2.0 2 votes vote down vote up
/**
 * Map ids start at 1.
 */
public <T extends Map<?, ?>> Registry registerMap(
        MapSchema.MessageFactory factory, int id);