Skip to content

Commit

Permalink
Fix data update save
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubenicos committed Jan 24, 2024
1 parent eb4b2fb commit 324901b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 27 deletions.
5 changes: 1 addition & 4 deletions src/main/java/com/saicone/settings/SettingsLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,9 @@ public <T extends MapNode> T load(@NotNull SettingsData<T> provider) {
provider.getOptional().load();
} catch (Throwable ignored) { }
}
T updated = updater.update(node, provider.getOptionalLoaded());
if (provider.getDataType().isWriteable() && !node.equals(updated)) {
provider.loaded(updated);
if (updater.update(node, provider.getOptionalLoaded()) && provider.getDataType().isWriteable()) {
provider.save();
}
node = updated;
}
if (parser != null) {
node = (T) parser.parse(node);
Expand Down
26 changes: 13 additions & 13 deletions src/main/java/com/saicone/settings/update/NodeUpdate.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,15 @@ public static NodeUpdate move() {
/**
* Create a node update that aims to do a custom transformation.
*
* @param function the function that accepts a parent map node and probably return itself.
* @param function the function that accepts a parent map node and return the update result.
* @return a node update that do a custom transformation.
*/
@NotNull
public static NodeUpdate custom(@NotNull Function<MapNode, MapNode> function) {
public static NodeUpdate custom(@NotNull Function<MapNode, Boolean> function) {
return new NodeUpdate(UpdateAction.CUSTOM) {
@Override
@SuppressWarnings("unchecked")
public <T extends MapNode> T apply(@NotNull T parent) {
return (T) function.apply(parent);
public boolean apply(@NotNull MapNode parent) {
return function.apply(parent);
}
};
}
Expand Down Expand Up @@ -211,13 +210,12 @@ public NodeUpdate value(@Nullable Object value) {
* Apply this node update into provided map node parent.
*
* @param parent the parent node to apply this update.
* @return the effective map node in this operation, normally the same map node.
* @param <T> the map node type.
* @return true if the node update was applied.
*/
public <T extends MapNode> T apply(@NotNull T parent) {
public boolean apply(@NotNull MapNode parent) {
final SettingsNode node = getNode(parent);
if (node == null) {
return parent;
return false;
}
switch (action) {
case ADD:
Expand All @@ -229,23 +227,25 @@ public <T extends MapNode> T apply(@NotNull T parent) {
node.move(path);
}
}
return true;
}
break;
case DELETE:
node.delete(true);
break;
return node.delete(true).isReal();
case REPLACE:
node.setValue(value);
break;
return true;
case MOVE:
final String[] path = getPath();
if (path != null) {
final boolean result = node.isReal();
node.move(path);
return result;
}
break;
default:
break;
}
return parent;
return false;
}
}
21 changes: 11 additions & 10 deletions src/main/java/com/saicone/settings/update/SettingsUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ public class SettingsUpdater {

private static final SettingsUpdater SIMPLE = new SettingsUpdater(Collections.unmodifiableList(new ArrayList<>())) {
@Override
public <T extends MapNode> @NotNull T update(@NotNull T base, @Nullable MapNode provider) {
if (provider != null) {
public boolean update(@NotNull MapNode base, @Nullable MapNode provider) {
if (provider != null && !base.equals(provider)) {
base.deepMerge(provider.getValue());
return true;
}
return base;
return false;
}
};

Expand Down Expand Up @@ -60,16 +61,16 @@ public SettingsUpdater(@Nullable List<NodeUpdate> nodeUpdates) {
*
* @param base the base node to apply updates into.
* @param provider the provider map node.
* @return the effective map node in this operation, normally the same base node.
* @param <T> the map node type.
* @return true if any update has been applied into base node.
*/
@NotNull
public <T extends MapNode> T update(@NotNull T base, @Nullable MapNode provider) {
T baseResult = base;
public boolean update(@NotNull MapNode base, @Nullable MapNode provider) {
boolean result = false;
for (NodeUpdate nodeUpdate : getNodeUpdates()) {
baseResult = nodeUpdate.apply(baseResult);
if (nodeUpdate.apply(base)) {
result = true;
}
}
return baseResult;
return result;
}

/**
Expand Down

0 comments on commit 324901b

Please sign in to comment.