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
|
/**
* Copyright (c) 2014, University of Warsaw
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted
* provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package pl.edu.mimuw.cloudatlas.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
* Represent a fully qualified name of a zone, also known as a global name or a path name. Objects of this class are immutable.
*/
public class PathName implements Serializable {
/**
* Object representing the name of the root zone (/).
*/
public static final PathName ROOT = new PathName("/");
private final List<String> components;
private final String name;
/**
* Creates a <code>PathName</code> object representing the specified path. For the root zone, there are three
* accepted forms: <code>null</code> reference, empty string or slash. Any other zone is represented by a string
* starting with slash and containing names of zones at each level of the hierarchy, separated by slashes. Zone names
* must contain only letters and digits.
*
* @param name path name of a zone, for instance: <code>/warsaw/uw/violet07</code>
* @throws IllegalArgumentException if the <code>name</code> is incorrect
*/
public PathName(String name) {
// we accept null and "/" as names of a root zone, however, we convert all of them to ""
name = name == null || name.equals("/")? "" : name.trim();
if(!name.matches("(/\\w+)*"))
throw new IllegalArgumentException("Incorrect fully qualified name: " + name + ".");
this.name = name;
components = name.equals("")? new ArrayList<String>() : Arrays.asList(name.substring(1).split("/"));
}
/**
* Creates a <code>PathName</code> object from a collection of zone names. Every zone name must contain only
* letters and digits.
*
* @param components a collection of zone names at subsequent levels of hierarchy (starting from root); an empty
* collection represents the root zone
* @throws IllegalArgumentException if any zone name is incorrect
*/
public PathName(Collection<String> components) {
this.components = new ArrayList<String>(components);
if(components.isEmpty())
this.name = "";
else {
String currentName = "";
for(String c : components) {
currentName += "/" + c;
if(!c.matches("\\w+"))
throw new IllegalArgumentException("Incorrect component " + c + ".");
}
this.name = currentName;
}
}
/**
* Gets zone names at subsequent levels of hierarchy, starting from the root. For the root zone, this method returns an
* empty collection. Modifying the returned list will throw an exception.
*
* @return a collection of zones names
*/
public List<String> getComponents() {
return Collections.unmodifiableList(components);
}
/**
* Gets a full path name. For the root zone, this method returns an empty string.
*
* @return a path name represented by this object
* @see #toString()
*/
public String getName() {
return name;
}
/**
* Gets a name one level up in the hierarchy. For the root zone, this method returns a new instance of the same zone.
*
* @return a new <code>PathName</code> object representing a zone one level up in the hierarchy
*/
public PathName levelUp() {
List<String> componentsUp = new ArrayList<String>(components);
if(!componentsUp.isEmpty())
componentsUp.remove(componentsUp.size() - 1);
return new PathName(componentsUp);
}
/**
* Gets a name one level down in a hierarchy.
*
* @param son zone name at a lower level
* @return a new <code>PathName</code> object representing a zone one level down in the hierarchy
*/
public PathName levelDown(String son) {
return new PathName(name + "/" + son);
}
/**
* Gets a zone name at the lowest level (highest number) in a hierarchy.
*
* @return a leaf zone
* @throws UnsupportedOperationException if this object represents the root zone
*/
public String getSingletonName() {
try {
return components.get(components.size() - 1);
} catch(IndexOutOfBoundsException exception) {
throw new UnsupportedOperationException("getSingletonName() is not supported for the root zone.");
}
}
/**
* Returns a hash code value for this object. This method returns a hash code of a string representing the full path
* name.
*
* @return a hash code for this object
*/
@Override
public int hashCode() {
return name.hashCode();
}
/**
* Indicates whether this object is equal to another. A <code>PathName</code> object is equal to other objects of
* the same class representing identical path names.
*
* @param object the object to check
* @return whether <code>object</code>'s name is equal to this one's
*/
@Override
public boolean equals(Object object) {
if(object == null)
return false;
if(getClass() != object.getClass())
return false;
return name.equals(((PathName)object).name);
}
/**
* Returns a textual representation for this <code>PathName</code>. For the root zone, unlike {@link #getName()},
* this method returns a slash.
*
* @return a path name for this object
* @see #getName()
*/
@Override
public String toString() {
return name.equals("")? "/" : getName();
}
}
|