1 19 package jcckit.util; 20 21 import java.util.Vector ; 22 23 31 public class Format implements TicLabelFormat { 32 42 public static Format create(ConfigParameters config, String key) { 43 Format result = null; 44 String format = config.get(key, null); 45 if (format != null && format.length() > 0) { 46 try { 47 result = new Format(format); 48 } catch (Exception e) { 49 throw new FactoryException(config, key, e); 50 } 51 } 52 return result; 53 } 54 55 private final FormatElement[] _formatElements; 56 private final Vector _staticParts; 57 58 97 public Format(String formatString) { 98 _staticParts = new Vector (); 99 Vector formatElements = new Vector (); 100 StringBuffer part = new StringBuffer (); 101 boolean insideFormatElement = false; 102 boolean atPercentSymbol = false; 103 for (int i = 0, n = formatString.length(); i < n; i++) { 104 char c = formatString.charAt(i); 105 if (insideFormatElement) { 106 part.append(c); 107 if (FormatElement.DESCRIPTORS.indexOf(c) >= 0) { 108 formatElements.addElement(new String (part)); 109 part.setLength(0); 110 insideFormatElement = false; 111 } 112 } else if (atPercentSymbol) { 113 atPercentSymbol = false; 114 if (c != '%') { 115 _staticParts.addElement(new String (part)); 116 part.setLength(0); 117 insideFormatElement = true; 118 } 119 part.append(c); 120 if (FormatElement.DESCRIPTORS.indexOf(c) >= 0) { 121 formatElements.addElement(new String (part)); 122 part.setLength(0); 123 insideFormatElement = false; 124 } 125 } else { 126 if (c == '%') { 127 atPercentSymbol = true; 128 } else { 129 part.append(c); 130 } 131 } 132 } 133 if (insideFormatElement) { 134 formatElements.addElement(new String (part)); 135 } else { 136 _staticParts.addElement(new String (part)); 137 } 138 139 _formatElements = new FormatElement[formatElements.size()]; 140 for (int i = 0; i < _formatElements.length; i++) { 141 _formatElements[i] 142 = new FormatElement((String ) formatElements.elementAt(i)); 143 } 144 } 145 146 155 public String form(long number) { 156 StringBuffer result = new StringBuffer (); 157 result.append(_staticParts.elementAt(0)); 158 if (_formatElements.length > 0) { 159 _formatElements[0].form(result, number); 160 } 161 return appendRest(result); 162 } 163 164 173 public String form(double number) { 174 StringBuffer result = new StringBuffer (); 175 result.append(_staticParts.elementAt(0)); 176 if (_formatElements.length > 0) { 177 _formatElements[0].form(result, number); 178 } 179 return appendRest(result); 180 } 181 182 private String appendRest(StringBuffer buffer) { 183 for (int i = 1, n = _staticParts.size(); i < n; i++) { 184 buffer.append(_staticParts.elementAt(i)); 185 } 186 return new String (buffer); 187 } 188 189 198 public String form(double[] numbers) { 199 StringBuffer result = new StringBuffer (); 200 for (int i = 0, n = _staticParts.size(); i < n; i++) { 201 result.append(_staticParts.elementAt(i)); 202 if (i < _formatElements.length && i < numbers.length) { 203 _formatElements[i].form(result, numbers[i]); 204 } 205 } 206 return new String (result); 207 } 208 } 209 | Popular Tags |