View Javadoc
1 /* 2 * Copyright (C) The Apache Software Foundation. All rights reserved. 3 * 4 * This software is published under the terms of the Apache Software License 5 * version 1.1, a copy of which has been included with this distribution in 6 * the LICENSE file. 7 */ 8 package org.apache.james.util; 9 10 import java.text.ParseException; 11 import java.text.DateFormat; 12 import java.text.SimpleDateFormat; 13 import java.util.Date; 14 import java.util.Locale; 15 import java.util.TimeZone; 16 17 /*** 18 * This class is designed to be a synchronized wrapper for a 19 * <code>java.text.DateFormat</code> subclass. In general, 20 * these subclasses (most notably the <code>java.text.SimpleDateFormat</code> 21 * classes are not thread safe, so we need to synchronize on the 22 * internal DateFormat for all delegated calls. 23 * 24 * @author Peter M. Goldstein <farsight@alum.mit.edu> 25 */ 26 public class SynchronizedDateFormat implements SimplifiedDateFormat { 27 private final DateFormat internalDateFormat; 28 29 /*** 30 * Public constructor that mimics that of SimpleDateFormat. See 31 * java.text.SimpleDateFormat for more details. 32 * 33 * @param pattern the pattern that defines this DateFormat 34 * @param locale the locale 35 */ 36 public SynchronizedDateFormat(String pattern, Locale locale) { 37 internalDateFormat = new SimpleDateFormat(pattern, locale); 38 } 39 40 /*** 41 * <p>Wrapper method to allow child classes to synchronize a preexisting 42 * DateFormat.</p> 43 * 44 * <p>TODO: Investigate replacing this with a factory method.</p> 45 * 46 * @param the DateFormat to synchronize 47 */ 48 protected SynchronizedDateFormat(DateFormat theDateFormat) { 49 internalDateFormat = theDateFormat; 50 } 51 52 /*** 53 * SimpleDateFormat will handle most of this for us, but we 54 * want to ensure thread safety, so we wrap the call in a 55 * synchronized block. 56 * 57 * @return java.lang.String 58 * @param d Date 59 */ 60 public String format(Date d) { 61 synchronized (internalDateFormat) { 62 return internalDateFormat.format(d); 63 } 64 } 65 66 /*** 67 * Parses text from the beginning of the given string to produce a date. 68 * The method may not use the entire text of the given string. 69 * <p> 70 * This method is designed to be thread safe, so we wrap our delegated 71 * parse method in an appropriate synchronized block. 72 * 73 * @param source A <code>String</code> whose beginning should be parsed. 74 * @return A <code>Date</code> parsed from the string. 75 * @throws ParseException if the beginning of the specified string 76 * cannot be parsed. 77 */ 78 public Date parse(String source) throws ParseException { 79 synchronized (internalDateFormat) { 80 return internalDateFormat.parse(source); 81 } 82 } 83 84 /*** 85 * Sets the time zone of this SynchronizedDateFormat object. 86 * @param zone the given new time zone. 87 */ 88 public void setTimeZone(TimeZone zone) { 89 synchronized(internalDateFormat) { 90 internalDateFormat.setTimeZone(zone); 91 } 92 } 93 94 /*** 95 * Gets the time zone. 96 * @return the time zone associated with this SynchronizedDateFormat. 97 */ 98 public TimeZone getTimeZone() { 99 synchronized(internalDateFormat) { 100 return internalDateFormat.getTimeZone(); 101 } 102 } 103 104 /*** 105 * Specify whether or not date/time parsing is to be lenient. With 106 * lenient parsing, the parser may use heuristics to interpret inputs that 107 * do not precisely match this object's format. With strict parsing, 108 * inputs must match this object's format. 109 * @param lenient when true, parsing is lenient 110 * @see java.util.Calendar#setLenient 111 */ 112 public void setLenient(boolean lenient) 113 { 114 synchronized(internalDateFormat) { 115 internalDateFormat.setLenient(lenient); 116 } 117 } 118 119 /*** 120 * Tell whether date/time parsing is to be lenient. 121 * @return whether this SynchronizedDateFormat is lenient. 122 */ 123 public boolean isLenient() 124 { 125 synchronized(internalDateFormat) { 126 return internalDateFormat.isLenient(); 127 } 128 } 129 130 /*** 131 * Overrides hashCode 132 */ 133 public int hashCode() { 134 synchronized(internalDateFormat) { 135 return internalDateFormat.hashCode(); 136 } 137 } 138 139 /*** 140 * Overrides equals 141 */ 142 public boolean equals(Object obj) { 143 if (this == obj) { 144 return true; 145 } 146 if (obj == null || getClass() != obj.getClass()) { 147 return false; 148 } 149 synchronized(internalDateFormat) { 150 return internalDateFormat.equals(obj); 151 } 152 } 153 154 }

This page was automatically generated by Maven