01: package sample.dms;
02:
03: import java.util.ArrayList;
04: import java.util.Iterator;
05: import java.util.List;
06:
07: import org.springframework.util.Assert;
08:
09: /**
10: * @author Ben Alex
11: * @version $Id: AbstractElement.java 1784 2007-02-24 21:00:24Z luke_t $
12: *
13: */
14: public abstract class AbstractElement {
15: /** The name of this token (ie filename or directory segment name */
16: private String name;
17:
18: /** The parent of this token (ie directory, or null if referring to root) */
19: private AbstractElement parent;
20:
21: /** The database identifier for this object (null if not persisted) */
22: private Long id;
23:
24: /**
25: * Constructor to use to represent a root element. A root element has an id of -1.
26: */
27: protected AbstractElement() {
28: this .name = "/";
29: this .parent = null;
30: this .id = new Long(-1);
31: }
32:
33: /**
34: * Constructor to use to represent a non-root element.
35: *
36: * @param name name for this element (required, cannot be "/")
37: * @param parent for this element (required, cannot be null)
38: */
39: protected AbstractElement(String name, AbstractElement parent) {
40: Assert.hasText(name, "Name required");
41: Assert.notNull(parent, "Parent required");
42: Assert
43: .notNull(parent.getId(),
44: "The parent must have been saved in order to create a child");
45: this .name = name;
46: this .parent = parent;
47: }
48:
49: public Long getId() {
50: return id;
51: }
52:
53: /**
54: * @return the name of this token (never null, although will be "/" if root, otherwise it won't include separators)
55: */
56: public String getName() {
57: return name;
58: }
59:
60: public AbstractElement getParent() {
61: return parent;
62: }
63:
64: /**
65: * @return the fully-qualified name of this element, including any parents
66: */
67: public String getFullName() {
68: List strings = new ArrayList();
69: AbstractElement currentElement = this ;
70: while (currentElement != null) {
71: strings.add(0, currentElement.getName());
72: currentElement = currentElement.getParent();
73: }
74:
75: StringBuffer sb = new StringBuffer();
76: String lastCharacter = null;
77: for (Iterator i = strings.iterator(); i.hasNext();) {
78: String token = (String) i.next();
79: if (!"/".equals(lastCharacter) && lastCharacter != null) {
80: sb.append("/");
81: }
82: sb.append(token);
83: lastCharacter = token.substring(token.length() - 1);
84: }
85: return sb.toString();
86: }
87: }
|