Search in sources :

Example 1 with DataSerializable

use of org.spongepowered.api.data.DataSerializable in project LanternServer by LanternPowered.

the class MemoryDataView method setCollection.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void setCollection(String key, Collection<?> value) {
    final ImmutableList.Builder<Object> builder = ImmutableList.builder();
    final LanternDataManager manager = getDataManager();
    for (Object object : value) {
        if (object instanceof DataSerializable) {
            builder.add(((DataSerializable) object).toContainer());
        } else if (object instanceof DataView) {
            if (this.safety == SafetyMode.ALL_DATA_CLONED || this.safety == SafetyMode.CLONED_ON_SET) {
                final MemoryDataView view = new MemoryDataContainer(this.safety);
                final DataView internalView = (DataView) object;
                for (Map.Entry<DataQuery, Object> entry : internalView.getValues(false).entrySet()) {
                    view.set(entry.getKey(), entry.getValue());
                }
                builder.add(view);
            } else {
                builder.add(object);
            }
        } else if (object instanceof CatalogType) {
            builder.add(((CatalogType) object).getId());
        } else if (object instanceof Map) {
            builder.add(ensureSerialization((Map) object));
        } else if (object instanceof Collection) {
            builder.add(ensureSerialization((Collection) object));
        } else {
            final TypeToken<?> typeToken = TypeToken.of(object.getClass());
            final DataTypeSerializer serializer = manager == null ? null : manager.getTypeSerializer(typeToken).orElse(null);
            if (serializer != null) {
                final Object result = serializer.serialize(typeToken, manager.getTypeSerializerContext(), object);
                checkArgument(!result.equals(this), "Cannot insert self-referencing Objects!");
                builder.add(result);
            } else {
                builder.add(object);
            }
        }
    }
    this.map.put(key, builder.build());
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) DataSerializable(org.spongepowered.api.data.DataSerializable) DataView(org.spongepowered.api.data.DataView) CatalogType(org.spongepowered.api.CatalogType) Collection(java.util.Collection) DataTypeSerializer(org.lanternpowered.server.data.persistence.DataTypeSerializer) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 2 with DataSerializable

use of org.spongepowered.api.data.DataSerializable in project LanternServer by LanternPowered.

the class MemoryDataView method set.

@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public DataView set(DataQuery path, Object value) {
    checkNotNull(path, "path");
    checkNotNull(value, "value");
    final LanternDataManager manager = getDataManager();
    final List<String> parts = path.getParts();
    final String key = parts.get(0);
    if (parts.size() > 1) {
        final DataQuery subQuery = of(key);
        final Optional<DataView> subViewOptional = getUnsafeView(subQuery);
        final DataView subView;
        if (!subViewOptional.isPresent()) {
            createView(subQuery);
            subView = (DataView) this.map.get(key);
        } else {
            subView = subViewOptional.get();
        }
        subView.set(path.popFirst(), value);
        return this;
    }
    Optional<DataTypeSerializer> optDataTypeSerializer;
    TypeToken typeToken;
    if (value instanceof DataView) {
        checkArgument(value != this, "Cannot set a DataView to itself.");
        // always have to copy a data view to avoid overwriting existing
        // views and to set the interior path correctly.
        copyDataView(path, (DataView) value);
    } else if (value instanceof DataSerializable) {
        final DataContainer valueContainer = ((DataSerializable) value).toContainer();
        checkArgument(!(valueContainer).equals(this), "Cannot insert self-referencing DataSerializable");
        // see above for why this is copied
        copyDataView(path, valueContainer);
    } else if (value instanceof CatalogType) {
        return set(path, ((CatalogType) value).getId());
    } else if (value instanceof Integer || value instanceof Byte || value instanceof Short || value instanceof Float || value instanceof Double || value instanceof Long || value instanceof String || value instanceof Character || value instanceof Boolean) {
        this.map.put(key, value);
        return this;
    } else if (manager != null && (optDataTypeSerializer = manager.getTypeSerializer(typeToken = TypeToken.of(value.getClass()))).isPresent()) {
        final DataTypeSerializer serializer = optDataTypeSerializer.get();
        final Object serialized = serializer.serialize(typeToken, manager.getTypeSerializerContext(), value);
        if (serialized instanceof DataContainer) {
            final DataContainer container = (DataContainer) serialized;
            checkArgument(!container.equals(this), "Cannot insert self-referencing Objects!");
            // see above for why this is copied
            copyDataView(path, container);
        } else {
            this.map.put(key, serialized);
        }
    } else if (value instanceof Collection) {
        setCollection(key, (Collection) value);
    } else if (value instanceof Map) {
        setMap(key, (Map) value);
    } else if (value.getClass().isArray()) {
        if (this.safety == SafetyMode.ALL_DATA_CLONED || this.safety == SafetyMode.CLONED_ON_SET) {
            if (value instanceof byte[]) {
                this.map.put(key, ArrayUtils.clone((byte[]) value));
            } else if (value instanceof short[]) {
                this.map.put(key, ArrayUtils.clone((short[]) value));
            } else if (value instanceof int[]) {
                this.map.put(key, ArrayUtils.clone((int[]) value));
            } else if (value instanceof long[]) {
                this.map.put(key, ArrayUtils.clone((long[]) value));
            } else if (value instanceof float[]) {
                this.map.put(key, ArrayUtils.clone((float[]) value));
            } else if (value instanceof double[]) {
                this.map.put(key, ArrayUtils.clone((double[]) value));
            } else if (value instanceof boolean[]) {
                this.map.put(key, ArrayUtils.clone((boolean[]) value));
            } else {
                this.map.put(key, ArrayUtils.clone((Object[]) value));
            }
        } else {
            this.map.put(key, value);
        }
    } else {
        this.map.put(key, value);
    }
    return this;
}
Also used : DataSerializable(org.spongepowered.api.data.DataSerializable) DataContainer(org.spongepowered.api.data.DataContainer) DataQuery(org.spongepowered.api.data.DataQuery) DataView(org.spongepowered.api.data.DataView) CatalogType(org.spongepowered.api.CatalogType) TypeToken(com.google.common.reflect.TypeToken) Collection(java.util.Collection) DataTypeSerializer(org.lanternpowered.server.data.persistence.DataTypeSerializer) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

ImmutableMap (com.google.common.collect.ImmutableMap)2 Collection (java.util.Collection)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 DataTypeSerializer (org.lanternpowered.server.data.persistence.DataTypeSerializer)2 CatalogType (org.spongepowered.api.CatalogType)2 DataSerializable (org.spongepowered.api.data.DataSerializable)2 DataView (org.spongepowered.api.data.DataView)2 ImmutableList (com.google.common.collect.ImmutableList)1 TypeToken (com.google.common.reflect.TypeToken)1 DataContainer (org.spongepowered.api.data.DataContainer)1 DataQuery (org.spongepowered.api.data.DataQuery)1