use of org.spongepowered.api.data.Key in project SpongeCommon by SpongePowered.
the class VanillaDataStore method serialize.
@Override
@SuppressWarnings(value = { "unchecked", "rawtypes" })
public DataView serialize(DataManipulator dataManipulator, DataView view) {
for (Map.Entry<Key<?>, Tuple<BiConsumer<DataView, ?>, Function<DataView, Optional<?>>>> entry : this.queriesByKey.entrySet()) {
final BiConsumer serializer = entry.getValue().first();
dataManipulator.get((Key) entry.getKey()).ifPresent(value -> serializer.accept(view, value));
}
return view;
}
use of org.spongepowered.api.data.Key in project SpongeCommon by SpongePowered.
the class SpongeKeyBuilder method build0.
@Override
public Key<V> build0() {
Objects.requireNonNull(this.valueType, "The value type must be set");
Objects.requireNonNull(this.elementType, "The element type must be set");
BiPredicate<? super E, ? super E> includesTester = this.includesTester;
if (includesTester == null) {
includesTester = (e, e2) -> false;
}
Comparator<? super E> comparator = this.comparator;
if (comparator == null) {
if (Comparable.class.isAssignableFrom(GenericTypeReflector.erase(this.elementType))) {
// noinspection unchecked
comparator = Comparator.comparing(o -> ((Comparable) o));
} else {
comparator = (o1, o2) -> {
if (o1.equals(o2))
return 0;
// There could be collisions, but yeah, what can you do about that..
if (o1.hashCode() > o2.hashCode())
return 1;
return -1;
};
}
}
Supplier<E> defaultValueSupplier = () -> null;
final Class<?> rawType = GenericTypeReflector.erase(this.valueType);
if (ListValue.class.isAssignableFrom(rawType)) {
defaultValueSupplier = () -> (E) new ArrayList();
} else if (SetValue.class.isAssignableFrom(rawType)) {
defaultValueSupplier = () -> (E) new HashSet();
} else if (WeightedCollectionValue.class.isAssignableFrom(rawType)) {
defaultValueSupplier = () -> (E) new WeightedTable();
} else if (MapValue.class.isAssignableFrom(rawType)) {
defaultValueSupplier = () -> (E) new HashMap<>();
}
final SpongeKey<Value<E>, E> key = new SpongeKey<>(this.key, this.valueType, this.elementType, comparator, includesTester, defaultValueSupplier);
KeyProvider.INSTANCE.register(this.key, (Key<Value<?>>) (Object) key);
return (Key<V>) key;
}
use of org.spongepowered.api.data.Key in project SpongeCommon by SpongePowered.
the class SpongeQuery method reduce.
protected Map<Lens, Integer> reduce(Fabric fabric, Lens lens, Map<Lens, Integer> matches) {
if (matches.isEmpty()) {
return Collections.emptyMap();
}
// Check if all matches are the direct children of this lens
List<Lens> lensSlots = lens.getChildren();
if (lensSlots.size() == matches.size() && matches.keySet().containsAll(lensSlots)) {
// return parent lens instead of constructing a new for the query result
matches.clear();
matches.put(lens, 0);
return matches;
}
// Remove duplicate slot-lenses
Map<SlotLens, Map<Key<?>, Object>> lenses = new LinkedHashMap<>();
Map<Lens, Integer> toRemove = new HashMap<>();
for (Map.Entry<Lens, Integer> entry : matches.entrySet()) {
final Lens slotLens = entry.getKey();
if (slotLens.slotCount() == 1) {
// Remove Lens with one slot
toRemove.put(slotLens, matches.get(slotLens));
// Find SlotLens for that Lens
final SlotLens sl = slotLens.getSlotLens(fabric, 0);
final Lens parent = slotLens.getParent();
final Map<Key<?>, Object> dataAt = parent == null ? Collections.emptyMap() : parent.getDataFor(slotLens);
// Collect all data for the SlotLens
lenses.computeIfAbsent(sl, k -> new HashMap<>()).putAll(dataAt);
}
}
// remove all single-slot lenses
matches.keySet().removeAll(toRemove.keySet());
for (Map.Entry<SlotLens, Map<Key<?>, Object>> entry : lenses.entrySet()) {
final Map<Key<?>, Object> data = entry.getValue();
if (data.isEmpty()) {
// add back slot-lenses
matches.put(entry.getKey(), toRemove.getOrDefault(entry.getKey(), 0));
} else {
// with data if found
final QueriedSlotLens delegatingSlotLens = new QueriedSlotLens(entry.getKey(), data);
matches.put(delegatingSlotLens, toRemove.getOrDefault(entry.getKey(), 0));
}
}
return matches;
}
Aggregations