Turn ApiPath into a record
[netconf.git] / protocol / restconf-api / src / main / java / org / opendaylight / restconf / api / ApiPath.java
index 8157a3296fb262134e1e864f35f41d22744cdaa8..9c4c41c76676cf59117d2ba105d837d894d261bd 100644 (file)
@@ -15,11 +15,18 @@ import com.google.common.base.MoreObjects.ToStringHelper;
 import com.google.common.collect.ImmutableList;
 import com.google.common.escape.Escaper;
 import com.google.common.escape.Escapers;
+import java.io.IOException;
+import java.io.NotSerializableException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.ObjectStreamException;
 import java.text.ParseException;
 import java.util.HexFormat;
+import java.util.List;
 import java.util.Objects;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.yangtools.concepts.HierarchicalIdentifier;
 import org.opendaylight.yangtools.concepts.Immutable;
 import org.opendaylight.yangtools.yang.common.UnresolvedQName;
 import org.opendaylight.yangtools.yang.common.UnresolvedQName.Unqualified;
@@ -30,7 +37,10 @@ import org.opendaylight.yangtools.yang.common.UnresolvedQName.Unqualified;
  * as a series of {@link Step}s.
  */
 @NonNullByDefault
-public final class ApiPath implements Immutable {
+public record ApiPath(ImmutableList<Step> steps) implements HierarchicalIdentifier<ApiPath> {
+    @java.io.Serial
+    private static final long serialVersionUID = 1L;
+
     /**
      * A single step in an {@link ApiPath}.
      */
@@ -156,8 +166,6 @@ public final class ApiPath implements Immutable {
             // Reserved characters as per https://tools.ietf.org/html/rfc3986#section-2.2
             ':', '/', '?', '#', '[', ']', '@',
             '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=',
-            // FIXME: this space should not be here, but that was a day-0 bug and we have asserts on this
-            ' '
         }) {
             builder.addEscape(ch, "%" + hexFormat.toHighHexDigit(ch) + hexFormat.toLowHexDigit(ch));
         }
@@ -166,10 +174,8 @@ public final class ApiPath implements Immutable {
 
     private static final ApiPath EMPTY = new ApiPath(ImmutableList.of());
 
-    private final ImmutableList<Step> steps;
-
-    private ApiPath(final ImmutableList<Step> steps) {
-        this.steps = requireNonNull(steps);
+    public ApiPath {
+        requireNonNull(steps);
     }
 
     /**
@@ -181,6 +187,10 @@ public final class ApiPath implements Immutable {
         return EMPTY;
     }
 
+    public static ApiPath of(final List<Step> steps) {
+        return steps.isEmpty() ? EMPTY : new ApiPath(ImmutableList.copyOf(steps));
+    }
+
     /**
      * Parse an {@link ApiPath} from a raw Request URI fragment or another source. The string is expected to contain
      * percent-encoded bytes. Any sequence of such bytes is interpreted as a {@code UTF-8}-encoded string. Invalid
@@ -209,16 +219,57 @@ public final class ApiPath implements Immutable {
         return str.isEmpty() ? EMPTY : parseString(ApiPathParser.newUrl(), str);
     }
 
+    /**
+     * Return the {@link Step}s of this path.
+     *
+     * @return Path steps
+     */
     public ImmutableList<Step> steps() {
         return steps;
     }
 
+    @Override
+    public boolean contains(final ApiPath other) {
+        if (this == other) {
+            return true;
+        }
+        final var oit = other.steps.iterator();
+        for (var step : steps) {
+            if (!oit.hasNext() || !step.equals(oit.next())) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    /**
+     * Returns the index of a Step in this path matching specified module and identifier. This method is equivalent to
+     * {@code indexOf(module, identifier, 0)}.
+     *
+     * @param module Requested {@link Step#module()}
+     * @param identifier Requested {@link Step#identifier()}
+     * @return Index of the step in {@link #steps}, or {@code -1} if a matching step is not found
+     * @throws NullPointerException if {@code identifier} is {@code null}
+     */
     public int indexOf(final String module, final String identifier) {
-        final var m = requireNonNull(module);
+        return indexOf(module, identifier, 0);
+    }
+
+    /**
+     * Returns the index of a Step in this path matching specified module and identifier, starting search at specified
+     * index.
+     *
+     * @param module Requested {@link Step#module()}
+     * @param identifier Requested {@link Step#identifier()}
+     * @param fromIndex index from which to search
+     * @return Index of the step in {@link #steps}, or {@code -1} if a matching step is not found
+     * @throws NullPointerException if {@code identifier} is {@code null}
+     */
+    public int indexOf(final String module, final String identifier, final int fromIndex) {
         final var id = requireNonNull(identifier);
-        for (int i = 0, size = steps.size(); i < size; ++i) {
+        for (int i = fromIndex, size = steps.size(); i < size; ++i) {
             final var step = steps.get(i);
-            if (m.equals(step.module) && id.equals(step.identifier.getLocalName())) {
+            if (id.equals(step.identifier.getLocalName()) && Objects.equals(module, step.module)) {
                 return i;
             }
         }
@@ -231,13 +282,7 @@ public final class ApiPath implements Immutable {
 
     public ApiPath subPath(final int fromIndex, final int toIndex) {
         final var subList = steps.subList(fromIndex, toIndex);
-        if (subList == steps) {
-            return this;
-        } else if (subList.isEmpty()) {
-            return EMPTY;
-        } else {
-            return new ApiPath(subList);
-        }
+        return subList == steps ? this : of(subList);
     }
 
     @Override
@@ -268,8 +313,31 @@ public final class ApiPath implements Immutable {
         return sb.toString();
     }
 
+    @java.io.Serial
+    Object writeReplace() throws ObjectStreamException {
+        return new APv1(toString());
+    }
+
+    @java.io.Serial
+    private void writeObject(final ObjectOutputStream stream) throws IOException {
+        throw nse();
+    }
+
+    @java.io.Serial
+    private void readObject(final ObjectInputStream stream) throws IOException, ClassNotFoundException {
+        throw nse();
+    }
+
+    @java.io.Serial
+    private void readObjectNoData() throws ObjectStreamException {
+        throw nse();
+    }
+
+    private NotSerializableException nse() {
+        return new NotSerializableException(getClass().getName());
+    }
+
     private static ApiPath parseString(final ApiPathParser parser, final String str) throws ParseException {
-        final var steps = parser.parseSteps(str);
-        return steps.isEmpty() ? EMPTY : new ApiPath(steps);
+        return of(parser.parseSteps(str));
     }
 }