just before adding authorization service.
[photos] / src / main / java / org / wamblee / photos / model / Path.java
1 /*
2  * Copyright 2005 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */ 
16 package org.wamblee.photos.model;
17
18 import java.io.Serializable;
19
20 /**
21  * Represents a path to a photo. 
22  *
23  */
24 public class Path implements Serializable {
25
26         static final long serialVersionUID = 1287597039089552731L;
27         
28     /**
29      * The parts of the path.
30      */
31         private String[] _path; 
32         
33     /**
34      * Constructs an empty path. 
35      *
36      */
37         public Path() {
38                 _path = new String[0];
39         }
40
41     /**
42      * Construcst the path based on a path string. 
43      * @param aPath Path, must start with a / 
44      */
45         public Path(String aPath) {
46                 if ( aPath == null ) {
47                         throw new IllegalArgumentException("path is null"); 
48                 }
49                 if ( !aPath.startsWith("/")) {
50                         throw new IllegalArgumentException("path must start with / '" + aPath + "'");
51                 }
52                 if ( aPath.length() == 1 ) { 
53                         _path = new String[0];
54                         return;
55                 }
56                 _path = aPath.substring(1).split("/");
57         }
58         
59     /**
60      * Constructs the path based on an array of the parts. 
61      * @param aPath Path. 
62      */
63         private Path(String[] aPath) {
64                 _path = aPath; 
65         }
66         
67     /**
68      * Returns the child of a path by appending a part. 
69      * @param aId Id to append.  
70      * @return Child. 
71      * @throws IllegalArgumentException In case the id contains a / 
72      */
73         public Path child(String aId) {
74         if ( aId.matches("/")) { 
75             throw new IllegalArgumentException("Id '" + aId + "' contains a /");
76         }
77             String[] newpath = new String[_path.length + 1];
78             System.arraycopy(_path, 0, newpath, 0, _path.length); 
79             newpath[_path.length] = aId; 
80             return new Path(newpath);
81         }
82         
83     /**
84      * Checks if the path is the root path. 
85      * @return True if the path is the root. 
86      */
87         public boolean isRoot() {
88                 return _path.length == 0; 
89         }
90         
91     /**
92      * Gets the parent of the path. 
93      * @return Parent of the path or the root if the path is already at the root. 
94      */
95         public Path parent() {
96                 if ( isRoot() ) {
97                         return this;
98                 }
99                 String[] newpath = new String[_path.length -1];
100                 System.arraycopy(_path, 0, newpath, 0, _path.length - 1);
101                 return new Path(newpath);
102         }
103         
104     /**
105      * Converts the path to a regular path.
106      * @return A path, always starting with a /
107      */
108         public String toString() {
109                 if ( _path.length == 0 ) {
110                         return "/";
111                 }
112                 String result = "";
113                 for (String part: _path) {
114                         result += "/" + part; 
115                 }
116         return result; 
117         }
118
119     /**
120      * Gets the id (last part) of the path. 
121      * @return Id or null if the path has no parts (root)
122      */
123         public String getId() {
124                 if ( _path.length == 0 ) {
125                         return null; 
126                 }
127                 return _path[_path.length -1];
128         }
129
130     /**
131      * Returns the number of components in the path. 
132      * @return Size. 
133      */
134     public int size() { 
135         return _path.length; 
136     }
137    
138     /**
139      * Returns a part of the path. 
140      * @param aPart Part number (starts at 0).
141      * @return Part. 
142      */
143     public String getPart(int aPart) { 
144         return _path[aPart];
145     }
146     
147     /**
148      * Strips the first part of the path and returns the remainder.
149      * @return Remainder.
150      */
151     public Path remainder() { 
152         if ( _path.length == 0 ) { 
153             return null; 
154         }
155         String[] result = new String[_path.length-1];
156         for (int i = 0; i < _path.length-1; i++) { 
157             result[i] = _path[i+1]; 
158         }
159         return new Path(result);
160     }
161 }