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;
* 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;
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);
}
/**
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
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
}
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));
}
}