Skip to content

Commit

Permalink
Merge pull request #105 from SpongePowered/fix/compatibility
Browse files Browse the repository at this point in the history
Revert compatibility break with NodePath and ConfigurationTransformation
  • Loading branch information
kashike authored Jun 26, 2018
2 parents 6eb89b9 + b43e351 commit 5f4b2f1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
*/
package ninja.leaping.configurate.transformation;

import com.google.common.collect.Iterators;
import ninja.leaping.configurate.ConfigurationNode;
import org.checkerframework.checker.nullness.qual.NonNull;

import java.util.Arrays;
import java.util.Iterator;
import java.util.SortedMap;
import java.util.TreeMap;

Expand Down Expand Up @@ -172,4 +174,36 @@ public ConfigurationTransformation build() {
return new VersionedTransformation(versionKey, versions);
}
}

/**
* Implementation of {@link ninja.leaping.configurate.transformation.NodePath} used by this class.
*/
// TODO Remove usages of this class in favour of the NodePath interface (breaking change for 4.0)
public static final class NodePath implements ninja.leaping.configurate.transformation.NodePath {
Object[] arr;

NodePath() {
}

@Override
public Object get(int i) {
return arr[i];
}

@Override
public int size() {
return arr.length;
}

@Override
public Object[] getArray() {
return Arrays.copyOf(arr, arr.length);
}

@NonNull
@Override
public Iterator<Object> iterator() {
return Iterators.forArray(arr);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@
*/
package ninja.leaping.configurate.transformation;

import com.google.common.collect.Iterators;
import ninja.leaping.configurate.ConfigurationNode;
import org.checkerframework.checker.nullness.qual.NonNull;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

Expand All @@ -35,13 +33,13 @@ class SingleConfigurationTransformation extends ConfigurationTransformation {
private final Map<Object[], TransformAction> actions;

/**
* Thread local {@link NodePath} instance - used so we don't have to create lots of NodePath
* Thread local {@link ConfigurationTransformation.NodePath} instance - used so we don't have to create lots of NodePath
* instances.
*
* As such, data within paths is only guaranteed to be the same during a run of
* a transform function.
*/
private final ThreadLocal<NodePathImpl> sharedPath = ThreadLocal.withInitial(NodePathImpl::new);
private final ThreadLocal<ConfigurationTransformation.NodePath> sharedPath = ThreadLocal.withInitial(ConfigurationTransformation.NodePath::new);

SingleConfigurationTransformation(Map<Object[], TransformAction> actions, MoveStrategy strategy) {
this.actions = actions;
Expand Down Expand Up @@ -85,7 +83,7 @@ private void applySingleAction(ConfigurationNode start, Object[] path, int start
}

// apply action
NodePathImpl nodePath = sharedPath.get();
ConfigurationTransformation.NodePath nodePath = sharedPath.get();
nodePath.arr = path;

Object[] transformedPath = action.visitPath(nodePath, node);
Expand All @@ -94,35 +92,4 @@ private void applySingleAction(ConfigurationNode start, Object[] path, int start
node.setValue(null);
}
}

/**
* Implementation of {@link NodePath} used by this class.
*/
static class NodePathImpl implements NodePath {
Object[] arr;

NodePathImpl() {
}

@Override
public Object get(int i) {
return arr[i];
}

@Override
public int size() {
return arr.length;
}

@Override
public Object[] getArray() {
return Arrays.copyOf(arr, arr.length);
}

@NonNull
@Override
public Iterator<Object> iterator() {
return Iterators.forArray(arr);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ public interface TransformAction {
* @return A modified path, or null if the path is to stay the same
*/
@Nullable
Object[] visitPath(@NonNull NodePath inputPath, @NonNull ConfigurationNode valueAtPath);
Object[] visitPath(ConfigurationTransformation.@NonNull NodePath inputPath, @NonNull ConfigurationNode valueAtPath);

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.collect.ImmutableList;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.SimpleConfigurationNode;
import ninja.leaping.configurate.transformation.ConfigurationTransformation.NodePath;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.junit.Test;
Expand Down

0 comments on commit 5f4b2f1

Please sign in to comment.