Source code: org/jdaemon/util/AttributeList.java
1 /*
2 * AttributeList.java
3 *
4 * Copyright (C) 2002 [Author Name]
5 * This program is distributed under the terms of the Lesser GNU General Public
6 * License (v2 or later) per the included COPYING.txt file, or see www.fsf.org.
7 *
8 * Created on 17 March 2002, 14:31
9 */
10
11 package org.jdaemon.util;
12
13 import java.util.Comparator;
14
15 /** Simple class for dynamically created list of Name/Value pairs.
16 *
17 * This class is optimised for fast creation rather than fast searches; Currently
18 * implemented using QuickList. However note that, like QuickList, adding an entry
19 * to an AttributeList does not change the original list, but rather returns a new
20 * list which has the additional element added.
21 *
22 * @author Jonathan Essex
23 * @version 0.1
24 */
25 public class AttributeList {
26
27 /** Class for an entry in an AttributeList; Basically just a name/value pair
28 *
29 * @author Jonathan Essex
30 */
31 public static class Entry {
32 /** The name of an attribute */
33 private String name;
34 /** The value of an attribute */
35 private Object value;
36
37 /** Create a new name/value pair
38 *
39 * @param name The name of an attribute
40 * @param value The value of an attribute
41 */
42 public Entry(String name, Object value) {
43 this.name = name;
44 this.value = value;
45 }
46
47 /** Get the name of this attribute
48 *
49 * @return The name of this attribute
50 */
51 public String getName() {
52 return name;
53 }
54
55 /** Get the value of this attribute
56 *
57 * @return The value of this attribute
58 */
59 public Object getValue() {
60 return value;
61 }
62
63 /** Write this attribute as a string: <I>name = "value"</I>
64 *
65 * @return A string representation of this name/value pair
66 */
67 public String toString() {
68 return name + "=\"" + value + "\"";
69 }
70 }
71
72 /** Comparator for Entry objects that works on the name part only
73 */
74 protected static final Comparator NAME_COMPARATOR = new Comparator() {
75
76 /** Assumes a and b are both Entry objects and compares the names
77 *
78 * @param a An Entry object to be compared
79 * @param b An Entry object to be compared
80 * @return -1, 0, or 1 depending upon whether a < b, a = b, or a > b
81 */
82 public int compare(Object a, Object b) {
83 return ((Entry)a).getName().compareTo(((Entry)b).getName());
84 }
85 };
86
87 public static final AttributeList EMPTY = new AttributeList();
88
89 /** Underlying store of Entry objects */
90 private QuickList attributes;
91
92 /** Utility function - checks that <I>name</I> is a valid name */
93 protected static void ValidateName(String name) {
94 if (name == null) throw new IllegalArgumentException("Attribute List cannot have a null name");
95 }
96
97 /** Protected constructor that allows an AttributeList to be created from a QuickList of Entry objects.
98 * *
99 * @param attributes A QuickList of Entry objects that defines the attributes in this list
100 */
101 protected AttributeList(QuickList attributes) {
102 this.attributes = attributes;
103 }
104
105 private AttributeList() {
106 this(QuickList.EMPTY);
107 }
108
109 /** Add an attribute (name,value) pair to an existing attribute list.
110 *
111 * @param name The name of the attribute
112 * @param value The value of the attribute
113 * @return A new AttributeList with the extra attribute added
114 */
115 public AttributeList addAttribute(String name, Object value) {
116 ValidateName(name);
117 return new AttributeList(attributes.add(new Entry(name, value)));
118 }
119
120 /** Find a value for a given name in this attribute list.
121 *
122 * @param name The name of the attribute to search for
123 * @return The value of the attibute with the given name, or null if none found
124 */
125 public Object get(String name) {
126 ValidateName(name);
127 return ((Entry)attributes.find(NAME_COMPARATOR, new Entry(name, null))).getValue();
128 }
129
130 /** Convert this list to a string.
131 *
132 * @return All Entries in the list in the form <I>name="value"</I>, separated by spaces
133 */
134 public String toString() {
135 return attributes.toString(" ");
136 }
137
138 /** get topmost (last added) entry in attribute list.
139 *
140 * @return topmost (last added) entry in attribute list
141 */
142 public Entry head() {
143 return (Entry)attributes.head();
144 }
145
146 /** Get this attribute list less the topmost entry.
147 *
148 * @return this attribute list less the topmost entry
149 */
150 public AttributeList tail() {
151 return new AttributeList(attributes.tail());
152 }
153
154 public boolean isEmpty() {
155 return attributes == QuickList.EMPTY;
156 }
157 }