net.openhft.chronicle.bytes.BytesMarshallable Java Examples

The following examples show how to use net.openhft.chronicle.bytes.BytesMarshallable. 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: ChronicleMapBuilder.java    From Chronicle-Map with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a new {@code ChronicleMapBuilder} instance which is able to {@linkplain #create()
 * create} maps with the specified key and value classes. It makes some assumptions about the size of entries
 * and the marshallers used
 *
 * @param keyClass   class object used to infer key type and discover it's properties via
 *                   reflection
 * @param valueClass class object used to infer value type and discover it's properties via
 *                   reflection
 * @param <K>        key type of the maps, created by the returned builder
 * @param <V>        value type of the maps, created by the returned builder
 * @return a new builder for the given key and value classes
 */
public static <K, V> ChronicleMapBuilder<K, V> simpleMapOf(
        @NotNull Class<K> keyClass, @NotNull Class<V> valueClass) {
    ChronicleMapBuilder<K, V> builder =
            new ChronicleMapBuilder<>(keyClass, valueClass)
                    .putReturnsNull(true)
                    .removeReturnsNull(true);
    builder.name(toCamelCase(valueClass.getSimpleName()));
    if (!builder.isKeySizeKnown())
        builder.averageKeySize(128);
    if (!builder.isValueSizeKnown())
        builder.averageValueSize(512);
    if (BytesMarshallable.class.isAssignableFrom(valueClass)) {
        builder.valueMarshaller(new BytesMarshallableReaderWriter<>((Class) valueClass));
    } else if (Marshallable.class.isAssignableFrom(valueClass)) {
        //noinspection unchecked
        builder.averageValueSize(1024)
                .valueMarshaller(new MarshallableReaderWriter<>((Class) valueClass));
    }
    return builder;
}
 
Example #2
Source File: SerializationBuilder.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private void configureByDefault(Class<T> tClass) {
    if (tClass.isPrimitive()) {
        throw new IllegalArgumentException(
                "Chronicle Map's key or value type cannot be primitive, " + tClass +
                        " type is given");
    }

    if (tClass.isInterface() && Values.isValueInterfaceOrImplClass(tClass)) {
        try {
            // Acquire a model before assigning readers/writers
            // if the interface is not a value interface
            ValueModel valueModel = ValueModel.acquire(tClass);
            reader((BytesReader<T>) new ValueReader<>(tClass));
            dataAccess(new ValueDataAccess<>(tClass));
            sizeMarshaller(constant((long) valueModel.sizeInBytes()));
            return;
        } catch (Exception e) {
            try {
                tClass = Values.nativeClassFor(tClass);
            } catch (Exception ex) {
                // ignore, fall through
            }
            // ignore, fall through
        }
    }

    if (concreteClass(tClass) && Byteable.class.isAssignableFrom(tClass)) {
        reader(new ByteableSizedReader<>((Class) tClass));
        dataAccess((DataAccess<T>) new ByteableDataAccess<>((Class) tClass));
        sizeMarshaller(constant(allocateByteable(tClass).maxSize()));
    } else if (tClass == CharSequence.class) {
        reader((SizedReader<T>) CharSequenceSizedReader.INSTANCE);
        dataAccess((DataAccess<T>) new CharSequenceUtf8DataAccess());
    } else if (tClass == StringBuilder.class) {
        reader((SizedReader<T>) StringBuilderSizedReader.INSTANCE);
        dataAccess((DataAccess<T>) new StringBuilderUtf8DataAccess());
    } else if (tClass == String.class) {
        reader((SizedReader<T>) new StringSizedReader());
        dataAccess((DataAccess<T>) new StringUtf8DataAccess());
    } else if (tClass == Boolean.class) {
        reader((SizedReader<T>) BooleanMarshaller.INSTANCE);
        notReusingWriter((SizedWriter<T>) BooleanMarshaller.INSTANCE);
        sizeMarshaller(constant(1));
    } else if (tClass == Long.class) {
        reader((SizedReader<T>) LongMarshaller.INSTANCE);
        dataAccess((DataAccess<T>) new LongDataAccess());
        sizeMarshaller(constant(8));
    } else if (tClass == Double.class) {
        reader((SizedReader<T>) DoubleMarshaller.INSTANCE);
        dataAccess((DataAccess<T>) new DoubleDataAccess());
        sizeMarshaller(constant(8));
    } else if (tClass == Integer.class) {
        reader((SizedReader<T>) IntegerMarshaller.INSTANCE);
        dataAccess((DataAccess<T>) new IntegerDataAccess_3_13());
        sizeMarshaller(constant(4));
    } else if (tClass == byte[].class) {
        reader((SizedReader<T>) ByteArraySizedReader.INSTANCE);
        dataAccess((DataAccess<T>) new ByteArrayDataAccess());
    } else if (tClass == ByteBuffer.class) {
        reader((SizedReader<T>) ByteBufferSizedReader.INSTANCE);
        dataAccess((DataAccess<T>) new ByteBufferDataAccess());
    } else if (concreteClass(tClass) && BytesMarshallable.class.isAssignableFrom(tClass)) {
        reader((BytesReader<T>) new BytesMarshallableReader<>((Class) tClass));
        dataAccess(new BytesMarshallableDataAccess<>((Class) tClass));
    } else if (concreteClass(tClass) && Externalizable.class.isAssignableFrom(tClass)) {
        reader((BytesReader<T>) new ExternalizableReader<>((Class) tClass));
        dataAccess(new ExternalizableDataAccess<>((Class) tClass));
    } else {
        reader((SizedReader<T>) new SerializableReader<>());
        dataAccess((DataAccess<T>) new SerializableDataAccess<>());
    }
}