use of org.spongepowered.api.data.DataView in project SpongeCommon by SpongePowered.
the class DataUpdaterDelegate method update.
@Override
public DataView update(DataView content) {
// backup
final DataView copied = content.copy();
DataView updated = copied;
for (DataContentUpdater updater : this.updaters) {
try {
updated = updater.update(updated);
} catch (Exception e) {
Exception exception = new RuntimeException("There was error attempting to update some data for the content updater:" + updater.getClass().getName() + "\nThe original data is being returned, possibly causing " + "issues later on, \nbut the original data should not be lost. Please notify the developer " + "of this exception with the stacktrace.", e);
exception.printStackTrace();
return copied;
}
}
return updated;
}
use of org.spongepowered.api.data.DataView in project SpongeCommon by SpongePowered.
the class MemoryDataView method createView.
@Override
public DataView createView(DataQuery path, Map<?, ?> map) {
checkNotNull(path, "path");
DataView section = createView(path);
for (Map.Entry<?, ?> entry : map.entrySet()) {
if (entry.getValue() instanceof Map) {
section.createView(of('.', entry.getKey().toString()), (Map<?, ?>) entry.getValue());
} else {
section.set(of('.', entry.getKey().toString()), entry.getValue());
}
}
return section;
}
use of org.spongepowered.api.data.DataView in project SpongeCommon by SpongePowered.
the class MemoryDataView method get.
@Override
public Optional<Object> get(DataQuery path) {
checkNotNull(path, "path");
List<String> queryParts = path.getParts();
int sz = queryParts.size();
if (sz == 0) {
return Optional.<Object>of(this);
}
String key = queryParts.get(0);
if (sz == 1) {
final Object object = this.map.get(key);
if (object == null) {
return Optional.empty();
}
if (this.safety == SafetyMode.ALL_DATA_CLONED) {
if (object.getClass().isArray()) {
if (object instanceof byte[]) {
return Optional.<Object>of(ArrayUtils.clone((byte[]) object));
} else if (object instanceof short[]) {
return Optional.<Object>of(ArrayUtils.clone((short[]) object));
} else if (object instanceof int[]) {
return Optional.<Object>of(ArrayUtils.clone((int[]) object));
} else if (object instanceof long[]) {
return Optional.<Object>of(ArrayUtils.clone((long[]) object));
} else if (object instanceof float[]) {
return Optional.<Object>of(ArrayUtils.clone((float[]) object));
} else if (object instanceof double[]) {
return Optional.<Object>of(ArrayUtils.clone((double[]) object));
} else if (object instanceof boolean[]) {
return Optional.<Object>of(ArrayUtils.clone((boolean[]) object));
} else {
return Optional.<Object>of(ArrayUtils.clone((Object[]) object));
}
}
}
return Optional.of(object);
}
Optional<DataView> subViewOptional = this.getUnsafeView(key);
if (!subViewOptional.isPresent()) {
return Optional.empty();
}
DataView subView = subViewOptional.get();
return subView.get(path.popFirst());
}
use of org.spongepowered.api.data.DataView 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);
}
use of org.spongepowered.api.data.DataView in project SpongeCommon by SpongePowered.
the class MemoryDataView method setCollection.
@SuppressWarnings({ "unchecked", "rawtypes" })
private void setCollection(String key, Collection<?> value) {
ImmutableList.Builder<Object> builder = ImmutableList.builder();
@Nullable DataManager manager;
try {
manager = Sponge.getDataManager();
} catch (Exception e) {
manager = null;
}
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) {
MemoryDataView view = new MemoryDataContainer(this.safety);
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 {
if (manager != null) {
final Optional<? extends DataTranslator<?>> translatorOptional = manager.getTranslator(object.getClass());
if (translatorOptional.isPresent()) {
DataTranslator translator = translatorOptional.get();
final DataContainer container = translator.translate(object);
checkArgument(!container.equals(this), "Cannot insert self-referencing Objects!");
builder.add(container);
} else {
builder.add(object);
}
} else {
builder.add(object);
}
}
}
this.map.put(key, builder.build());
}
Aggregations