Search in sources :

Example 1 with DataContentUpdater

use of org.spongepowered.api.data.persistence.DataContentUpdater 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;
}
Also used : DataView(org.spongepowered.api.data.DataView) DataContentUpdater(org.spongepowered.api.data.persistence.DataContentUpdater)

Example 2 with DataContentUpdater

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

the class LanternDataManager method getWrappedContentUpdater.

@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));
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) DataContentUpdater(org.spongepowered.api.data.persistence.DataContentUpdater)

Example 3 with DataContentUpdater

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

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;
}
Also used : DataView(org.spongepowered.api.data.DataView) DataContentUpdater(org.spongepowered.api.data.persistence.DataContentUpdater)

Example 4 with DataContentUpdater

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

the class SpongeDataManager method getWrappedContentUpdater.

@Override
public <T extends DataSerializable> Optional<DataContentUpdater> getWrappedContentUpdater(Class<T> clazz, final int fromVersion, final 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();
    }
    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
        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));
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) DataContentUpdater(org.spongepowered.api.data.persistence.DataContentUpdater)

Aggregations

DataContentUpdater (org.spongepowered.api.data.persistence.DataContentUpdater)4 ImmutableList (com.google.common.collect.ImmutableList)2 DataView (org.spongepowered.api.data.DataView)2