aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/path/Path.java
blob: cf94ed5c305163bd93dd426d9f2689cdc3030593 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.path;

import com.yahoo.api.annotations.Beta;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Represents a path as a list of elements. Immutable.
 *
 * @author Ulf Lilleengen
 * @author bratseth
 */
@Beta
public final class Path {

    private final String delimiter;
    private final List<String> elements;

    /** Creates an empty path */
    private Path(String delimiter) {
        this(List.of(), delimiter);
    }

    /**
     * Create path with given elements
     *
     * @param elements a list of path elements
     */
    private Path(List<String> elements, String delimiter) {
        for (String element : elements)
            if ("..".equals(element))
                throw new IllegalArgumentException("'..' is not allowed in path");

        this.elements = List.copyOf(elements);
        this.delimiter = delimiter;
    }

    /** Creates a new path with the given segments. */
    public static Path from(List<String> segments) {
        return new Path(segments, "/");
    }

    /** Returns whether this path is an immediate child of the given path */
    public boolean isChildOf(Path parent) {
        return toString().startsWith(parent.toString()) && this.elements.size() -1 == parent.elements.size();
    }

    /**
     * Add path elements by splitting based on delimiter and appending to elements.
     */
    private static List<String> elementsOf(String path, String delimiter) {
        return Arrays.stream(path.split(delimiter)).filter(e -> !"".equals(e)).toList();
    }

    /**
     * Append an element to the path. Returns a new path with the given path appended.
     *
     * @param path the path to append to this
     * @return the new path
     */
    public Path append(String path) {
        List<String> newElements = new ArrayList<>(this.elements);
        newElements.addAll(elementsOf(path, delimiter));
        return new Path(newElements, delimiter);
    }

    /**
     * Appends a path to another path, thereby creating a new path with the provided path
     * appended to this.
     *
     * @param path the path to append
     * @return a new path with argument appended to it
     */
    public Path append(Path path) {
        List<String> newElements = new ArrayList<>(this.elements);
        newElements.addAll(path.elements());
        return new Path(newElements, delimiter);
    }

    /** Returns the name of this path element, typically the last element in the path string */
    public String getName() {
        if (elements.isEmpty()) return "";
        return elements.get(elements.size() - 1);
    }

    /** Returns a string representation of the path represented by this */
    public String getRelative() {
        if (elements.isEmpty()) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        sb.append(elements.get(0));
        for (int i = 1; i < elements.size(); i++) {
            sb.append(delimiter);
            sb.append(elements.get(i));
        }
        return sb.toString();
    }

    /** Returns the parent path: A path containing all elements of this except the last */
    public Path getParentPath() {
        ArrayList<String> parentElements = new ArrayList<>();
        if (elements.size() > 1) {
            for (int i = 0; i < elements.size() - 1; i++) {
                parentElements.add(elements.get(i));
            }
        }
        return new Path(parentElements, delimiter);
    }

    /** Returns the child path: A path containing all elements of this except the first */
    public Path getChildPath() {
        ArrayList<String> childElements = new ArrayList<>();
        if (elements.size() > 1) {
            for (int i = 1; i < elements.size(); i++) {
                childElements.add(elements.get(i));
            }
        }
        return new Path(childElements, delimiter);
    }

    /** Returns the last element in this, or the empty string if this path is empty */
    public String last() {
        if (elements.isEmpty()) return "";
        return elements.get(elements.size() - 1);
    }

    /**
     * Returns a new path with the last element replaced by the given element.
     *
     * @throws IllegalStateException if this path is empty
     */
    public Path withLast(String element) {
        if (element.contains(delimiter)) throw new IllegalArgumentException("single element cannot contain delimiter " + delimiter);
        if (element.isEmpty()) throw new IllegalStateException("Cannot set the last element of an empty path");
        List<String> newElements = new ArrayList<>(elements);
        newElements.set(newElements.size() -1, element);
        return new Path(newElements, delimiter);
    }

    /** Returns a string representation of this path where the delimiter is prepended */
    public String getAbsolute() {
        return delimiter + getRelative();
    }

    public boolean isRoot() {
        return elements.isEmpty();
    }

    public Iterator<String> iterator() { return elements.iterator(); }

    /** Returns an immutable list of the elements of this path in order */
    public List<String> elements() { return elements; }

    /** Returns this as a string */
    @Override
    public String toString() {
        return getRelative();
    }

    /**
     * Creates a path from a string. The string is treated as a relative path, and all redundant '/'-characters are
     * stripped.
     *
     * @param path the relative path that this path should represent
     * @return a path object that may be used with the application package
     */
    public static Path fromString(String path) {
        return fromString(path, "/");
    }

    /**
     * Create a path from a string. The string is treated as a relative path, and all redundant delimiter-characters are
     * stripped.
     *
     * @param path the relative path that this path should represent
     * @return a path object that may be used with the application package
     */
    public static Path fromString(String path, String delimiter) {
        return new Path(elementsOf(path, delimiter), delimiter);
    }

    /**
     * Create an empty root path with '/' delimiter.
     *
     * @return an empty root path that can be appended
     */
    public static Path createRoot() {
        return createRoot("/");
    }

    /**
     * Create an empty root path with delimiter.
     *
     * @return an empty root path that can be appended
     */
    public static Path createRoot(String delimiter) {
        return new Path(delimiter);
    }

    public File toFile() { return new File(toString()); }

    @Override
    public int hashCode() {
        return elements.hashCode();
    }

    @Override
    public boolean equals(Object other) {
        if (other instanceof Path) {
            return getRelative().equals(((Path) other).getRelative());
        }
        return false;
    }

}