|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
AbstractResource.java | 92.9% | 100% | 100% | 97.9% |
|
1 |
// Copyright 2004, 2005 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
14 |
|
|
15 |
package org.apache.hivemind.util;
|
|
16 |
|
|
17 |
import java.util.Locale;
|
|
18 |
|
|
19 |
import org.apache.hivemind.Resource;
|
|
20 |
|
|
21 |
/**
|
|
22 |
* Abstract implementation of {@link org.apache.hivemind.Resource}.
|
|
23 |
*
|
|
24 |
* <p>Resource instances act as a kind of factory for new instances
|
|
25 |
* of the same type, via {@link #newResource(String)}.
|
|
26 |
*
|
|
27 |
* @author Howard Lewis Ship
|
|
28 |
*/
|
|
29 |
public abstract class AbstractResource implements Resource |
|
30 |
{ |
|
31 |
private String _path;
|
|
32 |
private String _name;
|
|
33 |
private String _folderPath;
|
|
34 |
private Locale _locale;
|
|
35 |
|
|
36 | 290 |
protected AbstractResource(String path)
|
37 |
{ |
|
38 | 290 |
this(path, null); |
39 |
} |
|
40 |
|
|
41 | 414 |
protected AbstractResource(String path, Locale locale)
|
42 |
{ |
|
43 | 414 |
_path = path; |
44 | 414 |
_locale = locale; |
45 |
} |
|
46 |
|
|
47 | 11 |
public String getName()
|
48 |
{ |
|
49 | 11 |
if (_name == null) |
50 | 11 |
split(); |
51 |
|
|
52 | 11 |
return _name;
|
53 |
} |
|
54 |
|
|
55 | 33 |
public Resource getRelativeResource(String name)
|
56 |
{ |
|
57 | 33 |
if (name.startsWith("/")) |
58 |
{ |
|
59 | 2 |
if (name.equals(_path))
|
60 | 1 |
return this; |
61 |
|
|
62 | 1 |
return newResource(name);
|
63 |
} |
|
64 |
|
|
65 | 31 |
if (_folderPath == null) |
66 | 11 |
split(); |
67 |
|
|
68 | 31 |
if (name.equals(_name))
|
69 | 1 |
return this; |
70 |
|
|
71 | 30 |
return newResource(_folderPath + name);
|
72 |
} |
|
73 |
|
|
74 | 160 |
public String getPath()
|
75 |
{ |
|
76 | 160 |
return _path;
|
77 |
} |
|
78 |
|
|
79 | 3 |
public Locale getLocale()
|
80 |
{ |
|
81 | 3 |
return _locale;
|
82 |
} |
|
83 |
|
|
84 |
|
|
85 |
protected abstract Resource newResource(String path);
|
|
86 |
|
|
87 | 22 |
private void split() |
88 |
{ |
|
89 | 22 |
int lastSlashx = _path.lastIndexOf('/');
|
90 |
|
|
91 | 22 |
_folderPath = _path.substring(0, lastSlashx + 1); |
92 | 22 |
_name = _path.substring(lastSlashx + 1); |
93 |
} |
|
94 |
|
|
95 |
|
|
96 |
/**
|
|
97 |
* Returns true if the other object is an instance of the
|
|
98 |
* same class, and the paths are equal.
|
|
99 |
*
|
|
100 |
*/
|
|
101 |
|
|
102 | 34 |
public boolean equals(Object obj) |
103 |
{ |
|
104 | 34 |
if (obj == null) |
105 | 1 |
return false; |
106 |
|
|
107 | 33 |
if (obj.getClass().equals(getClass()))
|
108 |
{ |
|
109 | 32 |
AbstractResource otherLocation = (AbstractResource) obj; |
110 |
|
|
111 | 32 |
return _path.equals(otherLocation._path);
|
112 |
} |
|
113 |
|
|
114 | 1 |
return false; |
115 |
} |
|
116 |
} |
|
117 |
|
|