Turn ApiPath into a record
[netconf.git] / protocol / restconf-api / src / main / java / org / opendaylight / restconf / api / ApiPath.java
index 635e397cec520c811c4b2dd287e42176921d6eb3..9c4c41c76676cf59117d2ba105d837d894d261bd 100644 (file)
@@ -22,6 +22,7 @@ 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;
@@ -36,7 +37,7 @@ import org.opendaylight.yangtools.yang.common.UnresolvedQName.Unqualified;
  * as a series of {@link Step}s.
  */
 @NonNullByDefault
-public final class ApiPath implements HierarchicalIdentifier<ApiPath> {
+public record ApiPath(ImmutableList<Step> steps) implements HierarchicalIdentifier<ApiPath> {
     @java.io.Serial
     private static final long serialVersionUID = 1L;
 
@@ -173,10 +174,8 @@ public final class ApiPath implements HierarchicalIdentifier<ApiPath> {
 
     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);
     }
 
     /**
@@ -188,6 +187,10 @@ public final class ApiPath implements HierarchicalIdentifier<ApiPath> {
         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
@@ -279,13 +282,7 @@ public final class ApiPath implements HierarchicalIdentifier<ApiPath> {
 
     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
@@ -341,7 +338,6 @@ public final class ApiPath implements HierarchicalIdentifier<ApiPath> {
     }
 
     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));
     }
 }