summaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/path/Path.java
blob: a15ebacc4cfc915946cb809a187b73462c07b32d (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.path;

import com.google.common.annotations.Beta;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

// TODO: Remove and replace usage by java.nio.file.Path

/**
 * Represents a path represented by a list of elements. Immutable
 *
 * @author lulf
 * @since 5.1
 */
@Beta
public final class Path {

    private final String delimiter;
    private final List<String> elements = new ArrayList<>();

    /**
     * Create an empty path.
     */
    private Path(String delimiter) {
        this(new ArrayList<>(), delimiter);
    }

    /**
     * Create a new path as a copy of the provided path.
     * @param rhs the path to copy.
     */
    private Path(Path rhs) {
        this(rhs.elements, rhs.delimiter);
    }

    /**
     * Create path with given elements.
     * @param elements a list of path elements
     */
    private Path(List<String> elements, String delimiter) {
        this.elements.addAll(elements);
        this.delimiter = delimiter;
    }

    /** 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 void addElementsFromString(String path) {
        String[] pathElements = path.split(delimiter);
        if (pathElements != null) {
            for (String elem : pathElements) {
                if (!"".equals(elem)) {
                    elements.add(elem);
                }
            }
        }
    }

    /**
     * Append an element to the path. Returns a new path with this element appended.
     * @param name name of element to append.
     * @return this, for chaining
     */
    public Path append(String name) {
        Path path = new Path(this);
        path.addElementsFromString(name);
        return path;
    }

    /**
     * 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) {
        Path newPath = new Path(this);
        newPath.elements.addAll(path.elements);
        return newPath;
    }

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

    /**
     * Get a string representation of the path represented by this.
     * @return a path string.
     */
    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();
    }

    /**
     * Get the parent path (all elements except last).
     * @return the parent path.
     */
    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);
    }

    /**
     * Get string representation of path represented from the root node.
     * @return string representation of path
     */
    public String getAbsolute() {
        return delimiter + getRelative();
    }

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

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

    /**
     * Convert to string.
     *
     * @return string representation of relative path
     */
    @Override
    public String toString() {
        // TODO: This and the relative/absolute thing is wrong. The Path either *is* relative or absolute
        //       and should return accordingly here. getAbsolute/relative should be replaced by an asRelative/absolute
        //       returning another Path
        return getRelative();
    }

    /**
     * Create 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) {
        Path pathObj = new Path(delimiter);
        pathObj.addElementsFromString(path);
        return pathObj;
    }

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

    @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;
    }
}