2 * Copyright 2005 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.photos.model;
18 import java.io.Serializable;
21 * Represents a path to a photo.
24 public class Path implements Serializable {
26 static final long serialVersionUID = 1287597039089552731L;
29 * The parts of the path.
31 private String[] _path;
34 * Constructs an empty path.
38 _path = new String[0];
42 * Construcst the path based on a path string.
43 * @param aPath Path, must start with a /
45 public Path(String aPath) {
46 if ( aPath == null ) {
47 throw new IllegalArgumentException("path is null");
49 if ( !aPath.startsWith("/")) {
50 throw new IllegalArgumentException("path must start with / '" + aPath + "'");
52 if ( aPath.length() == 1 ) {
53 _path = new String[0];
56 _path = aPath.substring(1).split("/");
60 * Constructs the path based on an array of the parts.
63 private Path(String[] aPath) {
68 * Returns the child of a path by appending a part.
69 * @param aId Id to append.
71 * @throws IllegalArgumentException In case the id contains a /
73 public Path child(String aId) {
74 if ( aId.matches("/")) {
75 throw new IllegalArgumentException("Id '" + aId + "' contains a /");
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);
84 * Checks if the path is the root path.
85 * @return True if the path is the root.
87 public boolean isRoot() {
88 return _path.length == 0;
92 * Gets the parent of the path.
93 * @return Parent of the path or the root if the path is already at the root.
95 public Path parent() {
99 String[] newpath = new String[_path.length -1];
100 System.arraycopy(_path, 0, newpath, 0, _path.length - 1);
101 return new Path(newpath);
105 * Converts the path to a regular path.
106 * @return A path, always starting with a /
108 public String toString() {
109 if ( _path.length == 0 ) {
113 for (String part: _path) {
114 result += "/" + part;
120 * Gets the id (last part) of the path.
121 * @return Id or null if the path has no parts (root)
123 public String getId() {
124 if ( _path.length == 0 ) {
127 return _path[_path.length -1];
131 * Returns the number of components in the path.
139 * Returns a part of the path.
140 * @param aPart Part number (starts at 0).
143 public String getPart(int aPart) {
148 * Strips the first part of the path and returns the remainder.
151 public Path remainder() {
152 if ( _path.length == 0 ) {
155 String[] result = new String[_path.length-1];
156 for (int i = 0; i < _path.length-1; i++) {
157 result[i] = _path[i+1];
159 return new Path(result);