1 16 package org.joda.time.format; 17 18 import java.io.IOException ; 19 import java.io.Writer ; 20 import java.util.Locale ; 21 22 import org.joda.time.MutablePeriod; 23 import org.joda.time.Period; 24 import org.joda.time.PeriodType; 25 import org.joda.time.ReadWritablePeriod; 26 import org.joda.time.ReadablePeriod; 27 28 67 public class PeriodFormatter { 68 69 70 private final PeriodPrinter iPrinter; 71 72 private final PeriodParser iParser; 73 74 private final Locale iLocale; 75 76 private final PeriodType iParseType; 77 78 85 public PeriodFormatter( 86 PeriodPrinter printer, PeriodParser parser) { 87 super(); 88 iPrinter = printer; 89 iParser = parser; 90 iLocale = null; 91 iParseType = null; 92 } 93 94 102 private PeriodFormatter( 103 PeriodPrinter printer, PeriodParser parser, 104 Locale locale, PeriodType type) { 105 super(); 106 iPrinter = printer; 107 iParser = parser; 108 iLocale = locale; 109 iParseType = type; 110 } 111 112 118 public boolean isPrinter() { 119 return (iPrinter != null); 120 } 121 122 127 public PeriodPrinter getPrinter() { 128 return iPrinter; 129 } 130 131 136 public boolean isParser() { 137 return (iParser != null); 138 } 139 140 145 public PeriodParser getParser() { 146 return iParser; 147 } 148 149 160 public PeriodFormatter withLocale(Locale locale) { 161 if (locale == getLocale() || (locale != null && locale.equals(getLocale()))) { 162 return this; 163 } 164 return new PeriodFormatter(iPrinter, iParser, locale, iParseType); 165 } 166 167 172 public Locale getLocale() { 173 return iLocale; 174 } 175 176 186 public PeriodFormatter withParseType(PeriodType type) { 187 if (type == iParseType) { 188 return this; 189 } 190 return new PeriodFormatter(iPrinter, iParser, iLocale, type); 191 } 192 193 198 public PeriodType getParseType() { 199 return iParseType; 200 } 201 202 209 public void printTo(StringBuffer buf, ReadablePeriod period) { 210 checkPrinter(); 211 checkPeriod(period); 212 213 getPrinter().printTo(buf, period, iLocale); 214 } 215 216 222 public void printTo(Writer out, ReadablePeriod period) throws IOException { 223 checkPrinter(); 224 checkPeriod(period); 225 226 getPrinter().printTo(out, period, iLocale); 227 } 228 229 235 public String print(ReadablePeriod period) { 236 checkPrinter(); 237 checkPeriod(period); 238 239 PeriodPrinter printer = getPrinter(); 240 StringBuffer buf = new StringBuffer (printer.calculatePrintedLength(period, iLocale)); 241 printer.printTo(buf, period, iLocale); 242 return buf.toString(); 243 } 244 245 250 private void checkPrinter() { 251 if (iPrinter == null) { 252 throw new UnsupportedOperationException ("Printing not supported"); 253 } 254 } 255 256 261 private void checkPeriod(ReadablePeriod period) { 262 if (period == null) { 263 throw new IllegalArgumentException ("Period must not be null"); 264 } 265 } 266 267 287 public int parseInto(ReadWritablePeriod period, String text, int position) { 288 checkParser(); 289 checkPeriod(period); 290 291 return getParser().parseInto(period, text, position, iLocale); 292 } 293 294 301 public Period parsePeriod(String text) { 302 checkParser(); 303 304 return parseMutablePeriod(text).toPeriod(); 305 } 306 307 314 public MutablePeriod parseMutablePeriod(String text) { 315 checkParser(); 316 317 MutablePeriod period = new MutablePeriod(0, iParseType); 318 int newPos = getParser().parseInto(period, text, 0, iLocale); 319 if (newPos >= 0) { 320 if (newPos >= text.length()) { 321 return period; 322 } 323 } else { 324 newPos = ~newPos; 325 } 326 throw new IllegalArgumentException (FormatUtils.createErrorMessage(text, newPos)); 327 } 328 329 334 private void checkParser() { 335 if (iParser == null) { 336 throw new UnsupportedOperationException ("Parsing not supported"); 337 } 338 } 339 340 } 341 | Popular Tags |