Skip to content

Commit

Permalink
core: Move NodePath out of the transformation package
Browse files Browse the repository at this point in the history
  • Loading branch information
zml2008 committed Nov 14, 2020
1 parent bc60b50 commit 522a263
Show file tree
Hide file tree
Showing 26 changed files with 109 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.configurate.serialize.SerializationException;
import org.spongepowered.configurate.serialize.TypeSerializer;
import org.spongepowered.configurate.transformation.NodePath;
import org.spongepowered.configurate.util.UnmodifiableCollections;

import java.lang.reflect.Type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.spongepowered.configurate;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.configurate.transformation.NodePath;

import java.io.IOException;
import java.util.function.Supplier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.spongepowered.configurate.serialize.Scalars;
import org.spongepowered.configurate.serialize.SerializationException;
import org.spongepowered.configurate.serialize.TypeSerializer;
import org.spongepowered.configurate.transformation.NodePath;

import java.lang.reflect.Type;
import java.util.Collection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.spongepowered.configurate.transformation;
package org.spongepowered.configurate;

import java.util.Collection;
import java.util.Iterator;
Expand All @@ -34,7 +34,11 @@ public interface NodePath extends Iterable<Object> {
* @since 4.0.0
*/
static NodePath of(Object[] path) {
return new NodePathImpl(path, true);
if (path.length == 0) {
return NodePathImpl.EMPTY;
} else {
return new NodePathImpl(path, true);
}
}

/**
Expand All @@ -45,7 +49,11 @@ static NodePath of(Object[] path) {
* @since 4.0.0
*/
static NodePath of(Collection<?> path) {
return new NodePathImpl(path.toArray(), false);
if (path.isEmpty()) {
return NodePathImpl.EMPTY;
} else {
return new NodePathImpl(path.toArray(), false);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.spongepowered.configurate.transformation;
package org.spongepowered.configurate;

import static java.util.Objects.requireNonNull;

import com.google.errorprone.annotations.concurrent.LazyInit;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.Arrays;
import java.util.Iterator;
Expand All @@ -27,11 +27,7 @@ final class NodePathImpl implements NodePath {

static final NodePath EMPTY = new NodePathImpl(new Object[0], false);

@LazyInit Object[] arr;

NodePathImpl() {

}
private final Object[] arr;

NodePathImpl(final Object[] arr, final boolean copy) {
requireNonNull(arr);
Expand Down Expand Up @@ -88,16 +84,16 @@ public NodePath copy() {
}

@Override
public boolean equals(final Object o) {
if (this == o) {
public boolean equals(final @Nullable Object other) {
if (this == other) {
return true;
}

if (o == null || getClass() != o.getClass()) {
if (other == null || getClass() != other.getClass()) {
return false;
}

final NodePathImpl that = (NodePathImpl) o;
final NodePathImpl that = (NodePathImpl) other;
return Arrays.equals(this.arr, that.arr);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.NodePath;
import org.spongepowered.configurate.ScopedConfigurationNode;
import org.spongepowered.configurate.loader.ConfigurationLoader;
import org.spongepowered.configurate.reactive.Publisher;
import org.spongepowered.configurate.reactive.TransactionalSubscriber;
import org.spongepowered.configurate.serialize.SerializationException;
import org.spongepowered.configurate.transformation.NodePath;

import java.nio.file.Path;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.NodePath;
import org.spongepowered.configurate.ScopedConfigurationNode;
import org.spongepowered.configurate.loader.ConfigurationLoader;
import org.spongepowered.configurate.reactive.Processor;
import org.spongepowered.configurate.reactive.Publisher;
import org.spongepowered.configurate.serialize.SerializationException;
import org.spongepowered.configurate.transformation.NodePath;

import java.util.Map;
import java.util.concurrent.Executor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.leangen.geantyref.TypeToken;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.NodePath;
import org.spongepowered.configurate.ScopedConfigurationNode;
import org.spongepowered.configurate.reactive.Disposable;
import org.spongepowered.configurate.reactive.Publisher;
Expand All @@ -27,7 +28,6 @@
import org.spongepowered.configurate.reference.ConfigurationReference.ErrorPhase;
import org.spongepowered.configurate.serialize.SerializationException;
import org.spongepowered.configurate.serialize.TypeSerializer;
import org.spongepowered.configurate.transformation.NodePath;
import org.spongepowered.configurate.util.UnmodifiableCollections;

import java.util.concurrent.Executor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.transformation.NodePath;
import org.spongepowered.configurate.NodePath;

import java.lang.reflect.Type;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.checkerframework.checker.nullness.qual.NonNull;
import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.NodePath;

import java.util.NavigableMap;
import java.util.TreeMap;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Configurate
* Copyright (C) zml and Configurate contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.spongepowered.configurate.transformation;

import com.google.errorprone.annotations.concurrent.LazyInit;
import org.spongepowered.configurate.NodePath;

import java.util.Arrays;
import java.util.Iterator;

/**
* Mutable implementation of {@link NodePath} for use with transform instances.
*/
final class MutableNodePath implements NodePath {

@LazyInit Object[] arr;

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

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

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

@Override
public NodePath withAppendedChild(final Object childKey) {
return copy().withAppendedChild(childKey);
}

@Override
public NodePath with(final int index, final Object value) throws IndexOutOfBoundsException {
return copy().with(index, value);
}

@Override
public Iterator<Object> iterator() {
return Arrays.asList(this.arr).iterator();
}

@Override
public NodePath copy() {
return NodePath.path(this.arr);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import static org.spongepowered.configurate.transformation.ConfigurationTransformation.WILDCARD_OBJECT;

import org.spongepowered.configurate.NodePath;

import java.util.Comparator;

final class NodePathComparator implements Comparator<NodePath> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.NodePath;

import java.util.Arrays;
import java.util.List;
Expand All @@ -42,7 +43,7 @@ final class SingleConfigurationTransformation implements ConfigurationTransforma
* <p>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<MutableNodePath> sharedPath = ThreadLocal.withInitial(MutableNodePath::new);

SingleConfigurationTransformation(final Map<NodePath, TransformAction> actions, final MoveStrategy strategy) {
this.actions = actions;
Expand Down Expand Up @@ -117,7 +118,7 @@ private void applySingleAction(final ConfigurationNode start, final Object[] pat
}

// apply action
final NodePathImpl nodePath = this.sharedPath.get();
final MutableNodePath nodePath = this.sharedPath.get();
nodePath.arr = path;

final Object @Nullable [] transformedPath = action.visitPath(nodePath, node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.NodePath;

import java.util.function.Supplier;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.NodePath;

import java.util.Map;
import java.util.NavigableMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.junit.jupiter.api.Test;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.serialize.SerializationException;
import org.spongepowered.configurate.transformation.NodePath;
import org.spongepowered.configurate.util.UnmodifiableCollections;

import java.lang.reflect.Type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.spongepowered.configurate.transformation.NodePath.path;
import static org.spongepowered.configurate.NodePath.path;

import org.junit.jupiter.api.Test;
import org.spongepowered.configurate.BasicConfigurationNode;
import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.NodePath;

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -50,7 +51,7 @@ private void doTestComparator(final ConfigurationNode node) throws ConfigurateEx
path("a", "b", "c", "d"),
path("a", "c"),
path("a", "b", "b")
);
);
final List<NodePath> autoSortedKeys = new ArrayList<>();
final List<NodePath> expectedSortedKeys = Arrays.asList(
path("a", "b", "b"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package org.spongepowered.configurate.examples;

import static org.spongepowered.configurate.transformation.NodePath.path;
import static org.spongepowered.configurate.NodePath.path;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.configurate.ConfigurateException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.configurate.CommentedConfigurationNode;
import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.NodePath;
import org.spongepowered.configurate.hocon.HoconConfigurationLoader;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.reference.ConfigurationReference;
import org.spongepowered.configurate.reference.ValueReference;
import org.spongepowered.configurate.reference.WatchServiceListener;
import org.spongepowered.configurate.transformation.NodePath;

import java.io.IOException;
import java.nio.file.Path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.NodePath;
import org.spongepowered.configurate.transformation.ConfigurationTransformation;
import org.spongepowered.configurate.transformation.NodePath;

import java.util.HashSet;
import java.util.Set;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.NodePath;
import org.spongepowered.configurate.transformation.ConfigurationTransformation;
import org.spongepowered.configurate.transformation.NodePath;

import java.util.HashSet;
import java.util.Set;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
package org.spongepowered.configurate.kotlin.extensions

import org.spongepowered.configurate.ConfigurationNode
import org.spongepowered.configurate.NodePath
import org.spongepowered.configurate.ScopedConfigurationNode
import org.spongepowered.configurate.kotlin.typeTokenOf
import org.spongepowered.configurate.serialize.SerializationException
import org.spongepowered.configurate.transformation.NodePath
import kotlin.reflect.KClass

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package org.spongepowered.configurate.kotlin.extensions

import org.spongepowered.configurate.transformation.NodePath
import org.spongepowered.configurate.NodePath

/**
* Concatenate [this] with another [NodePath].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.ConfigurationVisitor;
import org.spongepowered.configurate.transformation.NodePath;
import org.spongepowered.configurate.NodePath;

import java.io.IOException;

Expand Down
Loading

0 comments on commit 522a263

Please sign in to comment.