1 16 package org.joda.time.convert; 17 18 import org.joda.time.JodaTimePermission; 19 20 81 public final class ConverterManager { 82 83 86 private static ConverterManager INSTANCE; 87 88 public static ConverterManager getInstance() { 89 if (INSTANCE == null) { 90 INSTANCE = new ConverterManager(); 91 } 92 return INSTANCE; 93 } 94 95 private ConverterSet iInstantConverters; 96 private ConverterSet iPartialConverters; 97 private ConverterSet iDurationConverters; 98 private ConverterSet iPeriodConverters; 99 private ConverterSet iIntervalConverters; 100 101 104 protected ConverterManager() { 105 super(); 106 107 iInstantConverters = new ConverterSet(new Converter[] { 108 ReadableInstantConverter.INSTANCE, 109 StringConverter.INSTANCE, 110 CalendarConverter.INSTANCE, 111 DateConverter.INSTANCE, 112 LongConverter.INSTANCE, 113 NullConverter.INSTANCE, 114 }); 115 116 iPartialConverters = new ConverterSet(new Converter[] { 117 ReadablePartialConverter.INSTANCE, 118 ReadableInstantConverter.INSTANCE, 119 StringConverter.INSTANCE, 120 CalendarConverter.INSTANCE, 121 DateConverter.INSTANCE, 122 LongConverter.INSTANCE, 123 NullConverter.INSTANCE, 124 }); 125 126 iDurationConverters = new ConverterSet(new Converter[] { 127 ReadableDurationConverter.INSTANCE, 128 ReadableIntervalConverter.INSTANCE, 129 StringConverter.INSTANCE, 130 LongConverter.INSTANCE, 131 NullConverter.INSTANCE, 132 }); 133 134 iPeriodConverters = new ConverterSet(new Converter[] { 135 ReadableDurationConverter.INSTANCE, 136 ReadablePeriodConverter.INSTANCE, 137 ReadableIntervalConverter.INSTANCE, 138 StringConverter.INSTANCE, 139 NullConverter.INSTANCE, 140 }); 141 142 iIntervalConverters = new ConverterSet(new Converter[] { 143 ReadableIntervalConverter.INSTANCE, 144 StringConverter.INSTANCE, 145 NullConverter.INSTANCE, 146 }); 147 } 148 149 159 public InstantConverter getInstantConverter(Object object) { 160 InstantConverter converter = 161 (InstantConverter)iInstantConverters.select(object == null ? null : object.getClass()); 162 if (converter != null) { 163 return converter; 164 } 165 throw new IllegalArgumentException ("No instant converter found for type: " + 166 (object == null ? "null" : object.getClass().getName())); 167 } 168 169 175 public InstantConverter[] getInstantConverters() { 176 ConverterSet set = iInstantConverters; 177 InstantConverter[] converters = new InstantConverter[set.size()]; 178 set.copyInto(converters); 179 return converters; 180 } 181 182 193 public InstantConverter addInstantConverter(InstantConverter converter) 194 throws SecurityException { 195 196 checkAlterInstantConverters(); 197 if (converter == null) { 198 return null; 199 } 200 InstantConverter[] removed = new InstantConverter[1]; 201 iInstantConverters = iInstantConverters.add(converter, removed); 202 return removed[0]; 203 } 204 205 212 public InstantConverter removeInstantConverter(InstantConverter converter) 213 throws SecurityException { 214 215 checkAlterInstantConverters(); 216 if (converter == null) { 217 return null; 218 } 219 InstantConverter[] removed = new InstantConverter[1]; 220 iInstantConverters = iInstantConverters.remove(converter, removed); 221 return removed[0]; 222 } 223 224 229 private void checkAlterInstantConverters() throws SecurityException { 230 SecurityManager sm = System.getSecurityManager(); 231 if (sm != null) { 232 sm.checkPermission(new JodaTimePermission("ConverterManager.alterInstantConverters")); 233 } 234 } 235 236 246 public PartialConverter getPartialConverter(Object object) { 247 PartialConverter converter = 248 (PartialConverter)iPartialConverters.select(object == null ? null : object.getClass()); 249 if (converter != null) { 250 return converter; 251 } 252 throw new IllegalArgumentException ("No partial converter found for type: " + 253 (object == null ? "null" : object.getClass().getName())); 254 } 255 256 262 public PartialConverter[] getPartialConverters() { 263 ConverterSet set = iPartialConverters; 264 PartialConverter[] converters = new PartialConverter[set.size()]; 265 set.copyInto(converters); 266 return converters; 267 } 268 269 280 public PartialConverter addPartialConverter(PartialConverter converter) 281 throws SecurityException { 282 283 checkAlterPartialConverters(); 284 if (converter == null) { 285 return null; 286 } 287 PartialConverter[] removed = new PartialConverter[1]; 288 iPartialConverters = iPartialConverters.add(converter, removed); 289 return removed[0]; 290 } 291 292 299 public PartialConverter removePartialConverter(PartialConverter converter) 300 throws SecurityException { 301 302 checkAlterPartialConverters(); 303 if (converter == null) { 304 return null; 305 } 306 PartialConverter[] removed = new PartialConverter[1]; 307 iPartialConverters = iPartialConverters.remove(converter, removed); 308 return removed[0]; 309 } 310 311 316 private void checkAlterPartialConverters() throws SecurityException { 317 SecurityManager sm = System.getSecurityManager(); 318 if (sm != null) { 319 sm.checkPermission(new JodaTimePermission("ConverterManager.alterPartialConverters")); 320 } 321 } 322 323 333 public DurationConverter getDurationConverter(Object object) { 334 DurationConverter converter = 335 (DurationConverter)iDurationConverters.select(object == null ? null : object.getClass()); 336 if (converter != null) { 337 return converter; 338 } 339 throw new IllegalArgumentException ("No duration converter found for type: " + 340 (object == null ? "null" : object.getClass().getName())); 341 } 342 343 349 public DurationConverter[] getDurationConverters() { 350 ConverterSet set = iDurationConverters; 351 DurationConverter[] converters = new DurationConverter[set.size()]; 352 set.copyInto(converters); 353 return converters; 354 } 355 356 367 public DurationConverter addDurationConverter(DurationConverter converter) 368 throws SecurityException { 369 370 checkAlterDurationConverters(); 371 if (converter == null) { 372 return null; 373 } 374 DurationConverter[] removed = new DurationConverter[1]; 375 iDurationConverters = iDurationConverters.add(converter, removed); 376 return removed[0]; 377 } 378 379 386 public DurationConverter removeDurationConverter(DurationConverter converter) 387 throws SecurityException { 388 389 checkAlterDurationConverters(); 390 if (converter == null) { 391 return null; 392 } 393 DurationConverter[] removed = new DurationConverter[1]; 394 iDurationConverters = iDurationConverters.remove(converter, removed); 395 return removed[0]; 396 } 397 398 403 private void checkAlterDurationConverters() throws SecurityException { 404 SecurityManager sm = System.getSecurityManager(); 405 if (sm != null) { 406 sm.checkPermission(new JodaTimePermission("ConverterManager.alterDurationConverters")); 407 } 408 } 409 410 420 public PeriodConverter getPeriodConverter(Object object) { 421 PeriodConverter converter = 422 (PeriodConverter)iPeriodConverters.select(object == null ? null : object.getClass()); 423 if (converter != null) { 424 return converter; 425 } 426 throw new IllegalArgumentException ("No period converter found for type: " + 427 (object == null ? "null" : object.getClass().getName())); 428 } 429 430 436 public PeriodConverter[] getPeriodConverters() { 437 ConverterSet set = iPeriodConverters; 438 PeriodConverter[] converters = new PeriodConverter[set.size()]; 439 set.copyInto(converters); 440 return converters; 441 } 442 443 454 public PeriodConverter addPeriodConverter(PeriodConverter converter) 455 throws SecurityException { 456 457 checkAlterPeriodConverters(); 458 if (converter == null) { 459 return null; 460 } 461 PeriodConverter[] removed = new PeriodConverter[1]; 462 iPeriodConverters = iPeriodConverters.add(converter, removed); 463 return removed[0]; 464 } 465 466 473 public PeriodConverter removePeriodConverter(PeriodConverter converter) 474 throws SecurityException { 475 476 checkAlterPeriodConverters(); 477 if (converter == null) { 478 return null; 479 } 480 PeriodConverter[] removed = new PeriodConverter[1]; 481 iPeriodConverters = iPeriodConverters.remove(converter, removed); 482 return removed[0]; 483 } 484 485 490 private void checkAlterPeriodConverters() throws SecurityException { 491 SecurityManager sm = System.getSecurityManager(); 492 if (sm != null) { 493 sm.checkPermission(new JodaTimePermission("ConverterManager.alterPeriodConverters")); 494 } 495 } 496 497 507 public IntervalConverter getIntervalConverter(Object object) { 508 IntervalConverter converter = 509 (IntervalConverter)iIntervalConverters.select(object == null ? null : object.getClass()); 510 if (converter != null) { 511 return converter; 512 } 513 throw new IllegalArgumentException ("No interval converter found for type: " + 514 (object == null ? "null" : object.getClass().getName())); 515 } 516 517 523 public IntervalConverter[] getIntervalConverters() { 524 ConverterSet set = iIntervalConverters; 525 IntervalConverter[] converters = new IntervalConverter[set.size()]; 526 set.copyInto(converters); 527 return converters; 528 } 529 530 541 public IntervalConverter addIntervalConverter(IntervalConverter converter) 542 throws SecurityException { 543 544 checkAlterIntervalConverters(); 545 if (converter == null) { 546 return null; 547 } 548 IntervalConverter[] removed = new IntervalConverter[1]; 549 iIntervalConverters = iIntervalConverters.add(converter, removed); 550 return removed[0]; 551 } 552 553 560 public IntervalConverter removeIntervalConverter(IntervalConverter converter) 561 throws SecurityException { 562 563 checkAlterIntervalConverters(); 564 if (converter == null) { 565 return null; 566 } 567 IntervalConverter[] removed = new IntervalConverter[1]; 568 iIntervalConverters = iIntervalConverters.remove(converter, removed); 569 return removed[0]; 570 } 571 572 577 private void checkAlterIntervalConverters() throws SecurityException { 578 SecurityManager sm = System.getSecurityManager(); 579 if (sm != null) { 580 sm.checkPermission(new JodaTimePermission("ConverterManager.alterIntervalConverters")); 581 } 582 } 583 584 588 public String toString() { 589 return "ConverterManager[" + 590 iInstantConverters.size() + " instant," + 591 iPartialConverters.size() + " partial," + 592 iDurationConverters.size() + " duration," + 593 iPeriodConverters.size() + " period," + 594 iIntervalConverters.size() + " interval]"; 595 } 596 597 } 598 | Popular Tags |