| Home >> All >> com >> aendvari >> cerberus >> component >> [ descriptor Javadoc ] |
Source code: com/aendvari/cerberus/component/descriptor/MultiPartValue.java
1 /* 2 * MultiPartValue.java 3 * 4 * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved. 5 * 6 * See the file LICENSE for terms of use. 7 * 8 */ 9 10 package com.aendvari.cerberus.component.descriptor; 11 12 import java.util.ArrayList; 13 import java.util.Collection; 14 import java.util.Iterator; 15 16 /** 17 * <p>Defines a multi-part value. The value is built from a series of literals and attribute. 18 * Attributes allow portions of the property to be specified during component instantiation.</p> 19 * 20 * @author Trevor Milne 21 * 22 */ 23 24 public class MultiPartValue 25 { 26 /** The various parts of the value. */ 27 protected ArrayList parts; 28 29 30 /* Constructors. */ 31 32 33 /** 34 * Constructs a <code>MultiPartValue</code> instance. 35 * 36 */ 37 38 public MultiPartValue() 39 { 40 parts = new ArrayList(); 41 } 42 43 /** 44 * Constructs a <code>MultiPartValue</code> instance as a copy of the one supplied. 45 * 46 * @param value The <code>MultiPartValue</code> to copy. 47 * 48 */ 49 50 public MultiPartValue(MultiPartValue value) 51 { 52 parts = new ArrayList(); 53 54 // copy value parts 55 Iterator partIterator = value.getParts().iterator(); 56 57 while (partIterator.hasNext()) 58 { 59 Part copyPart = (Part)partIterator.next(); 60 Part newPart = new Part(copyPart); 61 62 parts.add(newPart); 63 } 64 } 65 66 67 /* Accessors. */ 68 69 70 /* Value. */ 71 72 /** 73 * Adds a literal to this value. 74 * 75 * @param literal A literal value to add to the value. 76 * 77 */ 78 79 public void addLiteral(String literal) 80 { 81 parts.add(new Part(Part.Type.Literal, literal)); 82 } 83 84 /** 85 * Adds an attribute to this value. 86 * 87 * @param attribute A name of a attribute to add to the value. 88 * 89 */ 90 91 public void addAttribute(String attribute) 92 { 93 parts.add(new Part(Part.Type.Attribute, attribute)); 94 } 95 96 /** 97 * Returns the parts of this value. 98 * 99 * @return A <code>Collection</code> of {@link Part Parts}. 100 * 101 */ 102 103 public Collection getParts() 104 { 105 return parts; 106 } 107 108 /** 109 * Clears this multi part value. 110 * 111 */ 112 113 public void clear() 114 { 115 parts = new ArrayList(); 116 } 117 118 119 /* Debug. */ 120 121 122 public String toString() 123 { 124 StringBuffer buffer = new StringBuffer(); 125 126 buffer.append("MultiPartValue=["); 127 128 Iterator partIterator = parts.iterator(); 129 130 while (partIterator.hasNext()) 131 { 132 Part part = (Part)partIterator.next(); 133 134 if (part.getType() == Part.Type.Literal) 135 { 136 buffer.append(part.getValue()); 137 } 138 else 139 { 140 buffer.append("$"); 141 buffer.append(part.getValue()); 142 buffer.append("$"); 143 } 144 } 145 146 buffer.append("]"); 147 148 return buffer.toString(); 149 } 150 151 152 /* Inner classes. */ 153 154 155 /** 156 * <p>Describes a single part of the value. A part may be either a literal 157 * or a attribute.<p> 158 * 159 */ 160 161 public static class Part 162 { 163 /** The type of the part. */ 164 protected int type; 165 166 /** The value of the part. */ 167 protected String value; 168 169 170 /* Constants. */ 171 172 173 /** Constants for the type of the part. */ 174 public interface Type 175 { 176 /** This part is a constant value. */ 177 public static final int Literal = 1; 178 179 /** This part is an attribute. */ 180 public static final int Attribute = 2; 181 } 182 183 184 /* Constructors. */ 185 186 187 /** 188 * Constructs a <code>Part</code> instance. 189 * 190 * @param setType The type of the part. 191 * @param setValue The value of the part. 192 * 193 */ 194 195 public Part(int setType, String setValue) 196 { 197 type = setType; 198 value = setValue; 199 } 200 201 /** 202 * Constructs a <code>Part</code> instance from the one supplied. 203 * 204 * @param part The <code>Part</code> to copy. 205 * 206 */ 207 208 public Part(Part part) 209 { 210 type = part.getType(); 211 value = part.getValue(); 212 } 213 214 215 /* Accessors. */ 216 217 218 /* Type. */ 219 220 public int getType() 221 { 222 return type; 223 } 224 225 /* Value. */ 226 227 public String getValue() 228 { 229 return value; 230 } 231 } 232 } 233