Search in sources :

Example 11 with DataQuery

use of org.spongepowered.api.data.DataQuery in project SpongeCommon by SpongePowered.

the class HomeDataImpl method from.

// Only required on mutable implementations
@Override
public Optional<HomeData> from(DataContainer container) {
    if (!container.contains(MyHomes.DEFAULT_HOME, MyHomes.HOMES)) {
        return Optional.empty();
    }
    // Loads the structure defined in toContainer
    this.defaultHome = container.getSerializable(MyHomes.DEFAULT_HOME.getQuery(), Home.class).get();
    // Loads the map of homes
    this.homes = Maps.newHashMap();
    DataView homes = container.getView(MyHomes.HOMES.getQuery()).get();
    for (DataQuery homeQuery : homes.getKeys(false)) {
        homes.getSerializable(homeQuery, Home.class).ifPresent(home -> this.homes.put(homeQuery.toString(), home));
    }
    return Optional.of(this);
}
Also used : DataView(org.spongepowered.api.data.DataView) DataQuery(org.spongepowered.api.data.DataQuery) Home(org.spongepowered.test.myhomes.data.home.Home)

Example 12 with DataQuery

use of org.spongepowered.api.data.DataQuery in project SpongeCommon by SpongePowered.

the class MemoryDataView method createView.

@Override
public DataView createView(DataQuery path) {
    checkNotNull(path, "path");
    List<String> queryParts = path.getParts();
    int sz = queryParts.size();
    checkArgument(sz != 0, "The size of the query must be at least 1");
    String key = queryParts.get(0);
    DataQuery keyQuery = of(key);
    if (sz == 1) {
        DataView result = new MemoryDataView(this, keyQuery, this.safety);
        this.map.put(key, result);
        return result;
    }
    DataQuery subQuery = path.popFirst();
    DataView subView = (DataView) this.map.get(key);
    if (subView == null) {
        subView = new MemoryDataView(this.parent, keyQuery, this.safety);
        this.map.put(key, subView);
    }
    return subView.createView(subQuery);
}
Also used : DataView(org.spongepowered.api.data.DataView) DataQuery(org.spongepowered.api.data.DataQuery)

Example 13 with DataQuery

use of org.spongepowered.api.data.DataQuery in project SpongeCommon by SpongePowered.

the class MemoryDataView method contains.

@Override
public boolean contains(DataQuery path, DataQuery... paths) {
    checkNotNull(path, "DataQuery cannot be null!");
    checkNotNull(paths, "DataQuery varargs cannot be null!");
    if (paths.length == 0) {
        return contains(path);
    }
    List<DataQuery> queries = new ArrayList<>();
    queries.add(path);
    for (DataQuery query : paths) {
        queries.add(checkNotNull(query, "No null queries!"));
    }
    for (DataQuery query : queries) {
        if (!contains(query)) {
            return false;
        }
    }
    return true;
}
Also used : ArrayList(java.util.ArrayList) DataQuery(org.spongepowered.api.data.DataQuery)

Example 14 with DataQuery

use of org.spongepowered.api.data.DataQuery in project core by CubeEngine.

the class DataContainerConverter method toMap.

private Map toMap(MapNode node, ConverterManager manager) throws ConversionException {
    Map<DataQuery, Object> map = new HashMap<>();
    for (Entry<String, Node> entry : node.getMappedNodes().entrySet()) {
        DataQuery key = DataQuery.of('_', node.getOriginalKey(entry.getKey()));
        map.put(key, toObject(entry.getValue(), manager));
    }
    return map;
}
Also used : HashMap(java.util.HashMap) MapNode(org.cubeengine.converter.node.MapNode) ListNode(org.cubeengine.converter.node.ListNode) Node(org.cubeengine.converter.node.Node) DataQuery(org.spongepowered.api.data.DataQuery)

Example 15 with DataQuery

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

the class MemoryDataView method createView.

@Override
public DataView createView(DataQuery path) {
    checkNotNull(path, "path");
    final List<String> queryParts = path.getParts();
    final int sz = queryParts.size();
    checkArgument(sz != 0, "The size of the query must be at least 1");
    final String key = queryParts.get(0);
    final DataQuery keyQuery = of(key);
    if (sz == 1) {
        final DataView result = new MemoryDataView(this, keyQuery, this.safety);
        this.map.put(key, result);
        return result;
    }
    final DataQuery subQuery = path.popFirst();
    final DataView subView = (DataView) this.map.computeIfAbsent(key, key1 -> new MemoryDataView(this.parent, keyQuery, this.safety));
    return subView.createView(subQuery);
}
Also used : Arrays(java.util.Arrays) ArrayUtils(org.apache.commons.lang3.ArrayUtils) TypeToken(com.google.common.reflect.TypeToken) DataQuery(org.spongepowered.api.data.DataQuery) Supplier(java.util.function.Supplier) Key(org.spongepowered.api.data.key.Key) ArrayList(java.util.ArrayList) DataSerializable(org.spongepowered.api.data.DataSerializable) LinkedHashMap(java.util.LinkedHashMap) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Lists(com.google.common.collect.Lists) DataTypeSerializer(org.lanternpowered.server.data.persistence.DataTypeSerializer) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) Objects(com.google.common.base.Objects) Nullable(javax.annotation.Nullable) ImmutableSet(com.google.common.collect.ImmutableSet) DataQuery.of(org.spongepowered.api.data.DataQuery.of) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) BaseValue(org.spongepowered.api.data.value.BaseValue) MoreObjects(com.google.common.base.MoreObjects) CatalogType(org.spongepowered.api.CatalogType) Sponge(org.spongepowered.api.Sponge) DataContainer(org.spongepowered.api.data.DataContainer) Set(java.util.Set) Collectors(java.util.stream.Collectors) Preconditions.checkState(com.google.common.base.Preconditions.checkState) List(java.util.List) Stream(java.util.stream.Stream) DataView(org.spongepowered.api.data.DataView) Lantern(org.lanternpowered.server.game.Lantern) Coerce(org.spongepowered.api.util.Coerce) Optional(java.util.Optional) DataView(org.spongepowered.api.data.DataView) DataQuery(org.spongepowered.api.data.DataQuery)

Aggregations

DataQuery (org.spongepowered.api.data.DataQuery)41 DataContainer (org.spongepowered.api.data.DataContainer)23 DataView (org.spongepowered.api.data.DataView)20 Test (org.junit.Test)14 Map (java.util.Map)12 ArrayList (java.util.ArrayList)7 List (java.util.List)7 DataTypeSerializer (org.lanternpowered.server.data.persistence.DataTypeSerializer)7 ImmutableMap (com.google.common.collect.ImmutableMap)4 TypeToken (com.google.common.reflect.TypeToken)4 Collection (java.util.Collection)4 HashMap (java.util.HashMap)4 Set (java.util.Set)4 Collectors (java.util.stream.Collectors)4 Nullable (javax.annotation.Nullable)4 CatalogType (org.spongepowered.api.CatalogType)4 DataSerializable (org.spongepowered.api.data.DataSerializable)4 Vector3i (com.flowpowered.math.vector.Vector3i)3 Lists (com.google.common.collect.Lists)3 LinkedHashMap (java.util.LinkedHashMap)3