当前位置: 首页>>代码示例>>Java>>正文


Java DataSerializable类代码示例

本文整理汇总了Java中org.spongepowered.api.data.DataSerializable的典型用法代码示例。如果您正苦于以下问题:Java DataSerializable类的具体用法?Java DataSerializable怎么用?Java DataSerializable使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


DataSerializable类属于org.spongepowered.api.data包,在下文中一共展示了DataSerializable类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: ensureSerialization

import org.spongepowered.api.data.DataSerializable; //导入依赖的package包/类
@SuppressWarnings("rawtypes")
private ImmutableMap<?, ?> ensureSerialization(Map<?, ?> map) {
    final ImmutableMap.Builder<Object, Object> builder = ImmutableMap.builder();
    map.forEach((key, value) -> {
        if (value instanceof Map) {
            builder.put(key, ensureSerialization((Map) value));
        } else if (value instanceof DataSerializable) {
            builder.put(key, ((DataSerializable) value).toContainer());
        } else if (value instanceof Collection) {
            builder.put(key, ensureSerialization((Collection) value));
        } else {
            builder.put(key, value);
        }
    });
    return builder.build();
}
 
开发者ID:LanternPowered,项目名称:LanternServer,代码行数:17,代码来源:MemoryDataView.java

示例2: getSerializableList

import org.spongepowered.api.data.DataSerializable; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public <T extends DataSerializable> Optional<List<T>> getSerializableList(DataQuery path, Class<T> clazz) {
    checkNotNull(path, "path");
    checkNotNull(clazz, "clazz");
    return Stream.<Supplier<Optional<List<T>>>>of(
            () -> {
                if (clazz.isAssignableFrom(CatalogType.class)) {
                    return (Optional<List<T>>) (Optional<?>) getCatalogTypeList(path, (Class<? extends CatalogType>) clazz);
                }
                return Optional.empty();
            },
            () -> getViewList(path)
                    .flatMap(list -> Sponge.getDataManager().getBuilder(clazz)
                            .map(builder -> list.stream()
                            .map(builder::build)
                            .filter(Optional::isPresent)
                            .map(Optional::get)
                            .collect(Collectors.toList()))))
            .map(Supplier::get)
            .filter(Optional::isPresent)
            .map(Optional::get)
            .findFirst();
}
 
开发者ID:LanternPowered,项目名称:LanternServer,代码行数:25,代码来源:MemoryDataView.java

示例3: registerBuilder

import org.spongepowered.api.data.DataSerializable; //导入依赖的package包/类
@Override
public <T extends DataSerializable> void registerBuilder(Class<T> clazz, DataBuilder<T> builder) {
    checkNotNull(clazz);
    checkNotNull(builder);
    checkState(this.allowRegistrations);
    if (!this.builders.containsKey(clazz)) {
        if (!(builder instanceof AbstractDataBuilder)) {
            this.logger.warn("A custom DataBuilder is not extending AbstractDataBuilder! It is recommended that "
                    + "the custom data builder does extend it to gain automated content versioning updates and maintain "
                    + "simplicity. The offending builder's class is: {}", builder.getClass());
        }
        this.builders.put(clazz, builder);
    } else {
        this.logger.warn("A DataBuilder has already been registered for {}. Attempted to register {} instead.", clazz,
                builder.getClass());
    }
}
 
开发者ID:LanternPowered,项目名称:LanternServer,代码行数:18,代码来源:LanternDataManager.java

示例4: buildContent

import org.spongepowered.api.data.DataSerializable; //导入依赖的package包/类
@Override
protected Optional<ParticleEffect> buildContent(DataView container) throws InvalidDataException {
    if (!container.contains(DataQueries.PARTICLE_TYPE, DataQueries.PARTICLE_OPTIONS)) {
        return Optional.empty();
    }
    ParticleType particleType = container.getCatalogType(DataQueries.PARTICLE_TYPE, ParticleType.class).get();
    Map<ParticleOption<?>, Object> options = new HashMap<>();
    container.getViewList(DataQueries.PARTICLE_OPTIONS).get().forEach(view -> {
        ParticleOption option = view.getCatalogType(DataQueries.PARTICLE_OPTION_KEY, ParticleOption.class).get();
        Object value;
        if (option.getValueType().isAssignableFrom(DataSerializable.class)) {
            value = view.getSerializable(DataQueries.PARTICLE_OPTION_VALUE, option.getValueType()).get();
        } else {
            value = view.getObject(DataQueries.PARTICLE_OPTION_VALUE, option.getValueType()).get();
        }
        options.put(option, value);
    });
    return Optional.of(new LanternParticleEffect((LanternParticleType) particleType, options));
}
 
开发者ID:LanternPowered,项目名称:LanternServer,代码行数:20,代码来源:LanternParticleEffectBuilder.java

示例5: serialize

import org.spongepowered.api.data.DataSerializable; //导入依赖的package包/类
public static String serialize(DataSerializable serializable) throws IOException {
    DataTranslator<ConfigurationNode> translator = DataTranslators.CONFIGURATION_NODE;
    ConfigurationNode node = translator.translate(serializable.toContainer());

    StringWriter strWriter = new StringWriter();
    final BufferedWriter sink = new BufferedWriter(strWriter);
    HoconConfigurationLoader.builder().setSink(() -> sink).build().save(node);

    return strWriter.toString();
}
 
开发者ID:caseif,项目名称:Inferno,代码行数:11,代码来源:SerializationHelper.java

示例6: deserialize

import org.spongepowered.api.data.DataSerializable; //导入依赖的package包/类
public static <T extends DataSerializable> T deserialize(Class<T> clazz, String serial) throws IOException {
    BufferedReader source = new BufferedReader(new StringReader(serial));
    ConfigurationNode node = HoconConfigurationLoader.builder().setSource(() -> source).build().load();

    DataManager manager = Sponge.getDataManager();
    DataTranslator<ConfigurationNode> translator = DataTranslators.CONFIGURATION_NODE;

    Optional<T> snapshot = manager.deserialize(clazz, translator.translate(node));
    if (snapshot.isPresent()) {
        return snapshot.get();
    } else {
        throw new IOException("Failed to deserialize to " + clazz.getSimpleName());
    }
}
 
开发者ID:caseif,项目名称:Inferno,代码行数:15,代码来源:SerializationHelper.java

示例7: setCollection

import org.spongepowered.api.data.DataSerializable; //导入依赖的package包/类
@SuppressWarnings({"unchecked", "rawtypes"})
private void setCollection(String key, Collection<?> value) {
    final ImmutableList.Builder<Object> builder = ImmutableList.builder();
    final LanternDataManager manager = Lantern.getGame().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.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());
}
 
开发者ID:LanternPowered,项目名称:LanternServer,代码行数:40,代码来源:MemoryDataView.java

示例8: getSerializable

import org.spongepowered.api.data.DataSerializable; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public <T extends DataSerializable> Optional<T> getSerializable(DataQuery path, Class<T> clazz) {
    checkNotNull(path, "path");
    checkNotNull(clazz, "clazz");
    if (clazz.isAssignableFrom(CatalogType.class)) {
        final Optional<T> catalog = (Optional<T>) getCatalogType(path, ((Class<? extends CatalogType>) clazz));
        if (catalog.isPresent()) {
            return catalog;
        }
    }
    return getUnsafeView(path).flatMap(view -> Sponge.getDataManager().getBuilder(clazz)
            .flatMap(builder -> builder.build(view)));
}
 
开发者ID:LanternPowered,项目名称:LanternServer,代码行数:15,代码来源:MemoryDataView.java

示例9: write

import org.spongepowered.api.data.DataSerializable; //导入依赖的package包/类
public static void write(JsonWriter writer, @Nullable Object value) throws IOException {
    if (value == null) {
        writer.nullValue();
    } else if (value instanceof Boolean) {
        writer.value((Boolean) value);
    } else if (value instanceof Number) {
        if (value instanceof Double) {
            String dbl = Double.toString((Double) value);
            if (dbl.indexOf('.') == -1) {
                dbl += DOUBLE_SUFFIX_UNTYPED;
            }
            // Writes a raw json value, without quotes
            writer.jsonValue(dbl);
        } else if (value instanceof Float) {
            writer.value(value + FLOAT_SUFFIX);
        } else if (value instanceof Long) {
            writer.value(value + LONG_SUFFIX);
        } else if (value instanceof Byte) {
            writer.value(value + BYTE_SUFFIX);
        } else if (value instanceof Short) {
            writer.value(value + SHORT_SUFFIX);
        } else {
            writer.value((Number) value);
        }
    } else if (value instanceof String) {
        writer.value((String) value);
    } else if (value instanceof Iterable) {
        writeArray(writer, (Iterable<?>) value);
    } else if (value instanceof Map) {
        writeMap(writer, (Map<?, ?>) value);
    } else if (value instanceof DataSerializable) {
        writeView(writer, ((DataSerializable) value).toContainer());
    } else if (value instanceof DataView) {
        writeView(writer, (DataView) value);
    } else {
        throw new IllegalArgumentException("Unable to translate object to JSON: " + value);
    }
}
 
开发者ID:LanternPowered,项目名称:LanternServer,代码行数:39,代码来源:JsonDataFormat.java

示例10: registerSerializers

import org.spongepowered.api.data.DataSerializable; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public static void registerSerializers(LanternDataManager dataManager) {
    dataManager.registerTypeSerializer(new TypeToken<Multimap<?,?>>() {}, new MultimapSerializer());
    dataManager.registerTypeSerializer(new TypeToken<Map<?,?>>() {}, new MapSerializer());
    dataManager.registerTypeSerializer(new TypeToken<List<?>>() {}, new ListSerializer());
    dataManager.registerTypeSerializer(new TypeToken<Set<?>>() {}, new SetSerializer());
    dataManager.registerTypeSerializer(new TypeToken<Optional<?>>() {}, new OptionalSerializer());
    dataManager.registerTypeSerializer((TypeToken) new TypeToken<Enum<?>>() {}, new EnumSerializer());
    dataManager.registerTypeSerializer(TypeToken.of(CatalogType.class), new CatalogTypeSerializer());
    dataManager.registerTypeSerializer(TypeToken.of(Number.class), new NumberSerializer());
    dataManager.registerTypeSerializer(TypeToken.of(String.class), new StringSerializer());
    dataManager.registerTypeSerializer(TypeToken.of(Boolean.class), new BooleanSerializer());
    dataManager.registerTypeSerializer(TypeToken.of(DataSerializable.class), new DataSerializableSerializer());
    dataManager.registerTypeSerializer(TypeToken.of(DataView.class), new DataViewSerializer());
}
 
开发者ID:LanternPowered,项目名称:LanternServer,代码行数:16,代码来源:DataTypeSerializers.java

示例11: deserialize

import org.spongepowered.api.data.DataSerializable; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public DataSerializable deserialize(TypeToken<?> type, DataTypeSerializerContext ctx, DataView data) throws InvalidDataException {
    final DataBuilder<DataSerializable> dataBuilder = (DataBuilder<DataSerializable>) Lantern.getGame().getDataManager()
            .getBuilder((Class<? extends DataSerializable>) type.getRawType())
            .orElseThrow(() -> new IllegalStateException("Wasn't able to find a DataBuilder for the DataSerializable: " + type));
    return dataBuilder.build(data).orElseThrow(() -> new InvalidDataException("Unable to deserializer the " + type));
}
 
开发者ID:LanternPowered,项目名称:LanternServer,代码行数:9,代码来源:DataTypeSerializers.java

示例12: registerContentUpdater

import org.spongepowered.api.data.DataSerializable; //导入依赖的package包/类
@Override
public <T extends DataSerializable> void registerContentUpdater(Class<T> clazz, DataContentUpdater dataContentUpdater) {
    checkNotNull(dataContentUpdater, "dataContentUpdater");
    checkNotNull(clazz, "clazz");
    final List<DataContentUpdater> updaters = this.updatersMap.computeIfAbsent(clazz, key -> new ArrayList<>());
    updaters.add(dataContentUpdater);
    updaters.sort(dataContentUpdaterComparator);
}
 
开发者ID:LanternPowered,项目名称:LanternServer,代码行数:9,代码来源:LanternDataManager.java

示例13: getWrappedContentUpdater

import org.spongepowered.api.data.DataSerializable; //导入依赖的package包/类
@Override
public <T extends DataSerializable> Optional<DataContentUpdater> getWrappedContentUpdater(Class<T> clazz, int fromVersion, int toVersion) {
    checkArgument(fromVersion != toVersion, "Attempting to convert to the same version!");
    checkArgument(fromVersion < toVersion, "Attempting to backwards convert data! This isn't supported!");
    final List<DataContentUpdater> updaters = this.updatersMap.get(checkNotNull(clazz, "DataSerializable class was null!"));
    if (updaters == null) {
        return Optional.empty();
    }
    final ImmutableList.Builder<DataContentUpdater> builder = ImmutableList.builder();
    int version = fromVersion;
    for (DataContentUpdater updater : updaters) {
        if (updater.getInputVersion() == version) {
            if (updater.getOutputVersion() > toVersion) {
                continue;
            }
            version = updater.getOutputVersion();
            builder.add(updater);
        }
    }
    if (version < toVersion || version > toVersion) { // There wasn't a registered updater for the version being requested
        final Exception e = new IllegalStateException("The requested content version for: " + clazz.getSimpleName() + " was requested, "
                + "\nhowever, the versions supplied: from " + fromVersion + " to " + toVersion + " is impossible"
                + "\nas the latest version registered is: " + version + ". Please notify the developer of"
                + "\nthe requested consumed DataSerializable of this error.");
        e.printStackTrace();
        return Optional.empty();
    }
    return Optional.of(new DataUpdaterDelegate(builder.build(), fromVersion, toVersion));
}
 
开发者ID:LanternPowered,项目名称:LanternServer,代码行数:30,代码来源:LanternDataManager.java

示例14: getBuilder

import org.spongepowered.api.data.DataSerializable; //导入依赖的package包/类
@SuppressWarnings({"unchecked", "SuspiciousMethodCalls"})
@Override
public <T extends DataSerializable> Optional<DataBuilder<T>> getBuilder(Class<T> objectClass) {
    checkNotNull(objectClass, "objectClass");
    if (this.builders.containsKey(objectClass)) {
        return Optional.of((DataBuilder<T>) this.builders.get(objectClass));
    } else if (this.builderMap.containsKey(objectClass)) {
        return Optional.of((DataBuilder<T>) this.builderMap.get(objectClass));
    } else if (this.immutableDataBuilderMap.containsKey(objectClass)) {
        return Optional.of((DataBuilder<T>) this.immutableDataBuilderMap.get(objectClass));
    } else {
        return Optional.empty();
    }
}
 
开发者ID:LanternPowered,项目名称:LanternServer,代码行数:15,代码来源:LanternDataManager.java

示例15: dataSerializableConfig

import org.spongepowered.api.data.DataSerializable; //导入依赖的package包/类
@Override
public <T extends FilteredTriggerConfiguration & DataSerializable> Trigger.Builder<T> dataSerializableConfig(Class<T> dataConfigClass) {
    checkNotNull(dataConfigClass, "dataConfigClass");
    this.configType = (Class<C>) dataConfigClass;
    this.constructor = new DataSerializableConstructor(dataConfigClass);
    return (Trigger.Builder<T>) this;
}
 
开发者ID:LanternPowered,项目名称:LanternServer,代码行数:8,代码来源:LanternTriggerBuilder.java


注:本文中的org.spongepowered.api.data.DataSerializable类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。