polishing

This commit is contained in:
Juergen Hoeller 2009-12-07 20:33:33 +00:00
parent 1d005e12af
commit 5fdc29f152
8 changed files with 58 additions and 39 deletions

View File

@ -73,8 +73,6 @@ public @interface DateTimeFormat {
/** /**
* Common ISO date time format patterns. * Common ISO date time format patterns.
* @author Keith Donald
* @since 3.0
*/ */
public enum ISO { public enum ISO {

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.format.datetime.joda; package org.springframework.format.datetime.joda;
import java.text.ParseException; import java.text.ParseException;
@ -20,10 +21,12 @@ import java.util.Locale;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.DateTimeFormatter;
import org.springframework.format.Parser; import org.springframework.format.Parser;
/** /**
* Parses Joda Time {@link DateTime} instances using a {@link DateTimeFormatter}. * Parses Joda Time {@link DateTime} instances using a {@link DateTimeFormatter}.
*
* @author Keith Donald * @author Keith Donald
* @since 3.0 * @since 3.0
*/ */
@ -32,7 +35,7 @@ public final class DateTimeParser implements Parser<DateTime> {
private final DateTimeFormatter formatter; private final DateTimeFormatter formatter;
/** /**
* Creates a new DateTimeParser. * Create a new DateTimeParser.
* @param formatter the Joda DateTimeFormatter instance * @param formatter the Joda DateTimeFormatter instance
*/ */
public DateTimeParser(DateTimeFormatter formatter) { public DateTimeParser(DateTimeFormatter formatter) {
@ -42,4 +45,5 @@ public final class DateTimeParser implements Parser<DateTime> {
public DateTime parse(String text, Locale locale) throws ParseException { public DateTime parse(String text, Locale locale) throws ParseException {
return JodaTimeContextHolder.getFormatter(this.formatter, locale).parseDateTime(text); return JodaTimeContextHolder.getFormatter(this.formatter, locale).parseDateTime(text);
} }
} }

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.format.datetime.joda; package org.springframework.format.datetime.joda;
import org.joda.time.Chronology; import org.joda.time.Chronology;
@ -22,6 +23,7 @@ import org.joda.time.format.DateTimeFormatter;
/** /**
* A context that holds user-specific Joda Time settings such as the user's Chronology (calendar system) and time zone. * A context that holds user-specific Joda Time settings such as the user's Chronology (calendar system) and time zone.
* A <code>null</code> property value indicate the user has not specified a setting. * A <code>null</code> property value indicate the user has not specified a setting.
*
* @author Keith Donald * @author Keith Donald
* @since 3.0 * @since 3.0
* @see JodaTimeContextHolder * @see JodaTimeContextHolder
@ -32,19 +34,27 @@ public class JodaTimeContext {
private DateTimeZone timeZone; private DateTimeZone timeZone;
/**
* Set the user's chronology.
*/
public void setChronology(Chronology chronology) {
this.chronology = chronology;
}
/** /**
* The user's chronology (calendar system). * The user's chronology (calendar system).
* Null if not specified. * Null if not specified.
*/ */
public Chronology getChronology() { public Chronology getChronology() {
return chronology; return this.chronology;
} }
/** /**
* Sets the user's chronology. * Set the user's timezone.
*/ */
public void setChronology(Chronology chronology) { public void setTimeZone(DateTimeZone timeZone) {
this.chronology = chronology; this.timeZone = timeZone;
} }
/** /**
@ -55,12 +65,6 @@ public class JodaTimeContext {
return timeZone; return timeZone;
} }
/**
* Sets the user's timezone.
*/
public void setTimeZone(DateTimeZone timeZone) {
this.timeZone = timeZone;
}
/** /**
* Gets the Formatter with the this context's settings applied to the base <code>formatter</code>. * Gets the Formatter with the this context's settings applied to the base <code>formatter</code>.

View File

@ -13,30 +13,26 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.format.datetime.joda; package org.springframework.format.datetime.joda;
import java.util.Locale; import java.util.Locale;
import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.DateTimeFormatter;
import org.springframework.core.NamedInheritableThreadLocal; import org.springframework.core.NamedInheritableThreadLocal;
/** /**
* A holder for a thread-local user {@link JodaTimeContext}. * A holder for a thread-local user {@link JodaTimeContext}.
*
* @since 3.0 * @since 3.0
* @author Keith Donald * @author Keith Donald
*/ */
public final class JodaTimeContextHolder { public final class JodaTimeContextHolder {
private static final ThreadLocal<JodaTimeContext> jodaTimeContextHolder = new NamedInheritableThreadLocal<JodaTimeContext>( private static final ThreadLocal<JodaTimeContext> jodaTimeContextHolder =
"Joda Time Context"); new NamedInheritableThreadLocal<JodaTimeContext>("JodaTime Context");
/**
* Return the JodaTimeContext associated with the current thread, if any.
* @return the current JodaTimeContext, or <code>null</code> if none
*/
public static JodaTimeContext getJodaTimeContext() {
return jodaTimeContextHolder.get();
}
/** /**
* Associate the given JodaTimeContext with the current thread. * Associate the given JodaTimeContext with the current thread.
@ -47,6 +43,14 @@ public final class JodaTimeContextHolder {
jodaTimeContextHolder.set(context); jodaTimeContextHolder.set(context);
} }
/**
* Return the JodaTimeContext associated with the current thread, if any.
* @return the current JodaTimeContext, or <code>null</code> if none
*/
public static JodaTimeContext getJodaTimeContext() {
return jodaTimeContextHolder.get();
}
/** /**
* Gets the Formatter with the user-specific settings applied to thefrom the base <code>formatter</code>. * Gets the Formatter with the user-specific settings applied to thefrom the base <code>formatter</code>.
* @param formatter the base formatter that establishes default formatting rules, generally user independent * @param formatter the base formatter that establishes default formatting rules, generally user independent
@ -58,7 +62,7 @@ public final class JodaTimeContextHolder {
formatter = formatter.withLocale(locale); formatter = formatter.withLocale(locale);
} }
JodaTimeContext context = getJodaTimeContext(); JodaTimeContext context = getJodaTimeContext();
return context != null ? context.getFormatter(formatter) : formatter; return (context != null ? context.getFormatter(formatter) : formatter);
} }
} }

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.format.datetime.joda; package org.springframework.format.datetime.joda;
import java.util.Calendar; import java.util.Calendar;
@ -24,22 +25,20 @@ import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime; import org.joda.time.LocalDateTime;
import org.joda.time.LocalTime; import org.joda.time.LocalTime;
import org.joda.time.ReadableInstant; import org.joda.time.ReadableInstant;
import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterRegistry; import org.springframework.core.convert.converter.ConverterRegistry;
/** /**
* Installs lower-level type converters required to integrate Joda Time support into Spring's field formatting system. * Installs lower-level type converters required to integrate Joda Time support into Spring's field formatting system.
*
* @author Keith Donald * @author Keith Donald
* @since 3.0 * @since 3.0
*/ */
final class JodaTimeConverters { final class JodaTimeConverters {
private JodaTimeConverters() {
}
/** /**
* Installs the converters into the converter registry. * Install the converters into the converter registry.
* @param registry the converter registry * @param registry the converter registry
*/ */
public static void registerConverters(ConverterRegistry registry) { public static void registerConverters(ConverterRegistry registry) {
@ -54,7 +53,6 @@ final class JodaTimeConverters {
registry.addConverter(new CalendarToReadableInstantConverter()); registry.addConverter(new CalendarToReadableInstantConverter());
} }
// internal helpers
/** /**
* Used when binding a parsed DateTime to a LocalDate field. * Used when binding a parsed DateTime to a LocalDate field.
@ -129,7 +127,7 @@ final class JodaTimeConverters {
/** /**
* Used when printing a java.util.Date field with a MillisecondInstantPrinter. * Used when printing a java.util.Date field with a MillisecondInstantPrinter.
* @see MillisecondInstantPrinter * @see MillisecondInstantPrinter
* @see DateTimeFormatAnnotationFormatterFactory * @see JodaDateTimeFormatAnnotationFormatterFactory
*/ */
private static class DateToLongConverter implements Converter<Date, Long> { private static class DateToLongConverter implements Converter<Date, Long> {
public Long convert(Date source) { public Long convert(Date source) {
@ -140,7 +138,7 @@ final class JodaTimeConverters {
/** /**
* Used when printing a java.util.Calendar field with a ReadableInstantPrinter. * Used when printing a java.util.Calendar field with a ReadableInstantPrinter.
* @see MillisecondInstantPrinter * @see MillisecondInstantPrinter
* @see DateTimeFormatAnnotationFormatterFactory * @see JodaDateTimeFormatAnnotationFormatterFactory
*/ */
private static class CalendarToReadableInstantConverter implements Converter<Calendar, ReadableInstant> { private static class CalendarToReadableInstantConverter implements Converter<Calendar, ReadableInstant> {
public ReadableInstant convert(Calendar source) { public ReadableInstant convert(Calendar source) {

View File

@ -13,15 +13,18 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.format.datetime.joda; package org.springframework.format.datetime.joda;
import java.util.Locale; import java.util.Locale;
import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.DateTimeFormatter;
import org.springframework.format.Printer; import org.springframework.format.Printer;
/** /**
* Prints Long instances using a {@link DateTimeFormatter}. * Prints Long instances using a {@link DateTimeFormatter}.
*
* @author Keith Donald * @author Keith Donald
* @since 3.0 * @since 3.0
*/ */
@ -30,7 +33,7 @@ public final class MillisecondInstantPrinter implements Printer<Long> {
private final DateTimeFormatter formatter; private final DateTimeFormatter formatter;
/** /**
* Creates a new ReadableInstantPrinter. * Create a new ReadableInstantPrinter.
* @param formatter the Joda DateTimeFormatter instance * @param formatter the Joda DateTimeFormatter instance
*/ */
public MillisecondInstantPrinter(DateTimeFormatter formatter) { public MillisecondInstantPrinter(DateTimeFormatter formatter) {
@ -40,4 +43,5 @@ public final class MillisecondInstantPrinter implements Printer<Long> {
public String print(Long instant, Locale locale) { public String print(Long instant, Locale locale) {
return JodaTimeContextHolder.getFormatter(this.formatter, locale).print(instant); return JodaTimeContextHolder.getFormatter(this.formatter, locale).print(instant);
} }
} }

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.format.datetime.joda; package org.springframework.format.datetime.joda;
import java.util.Locale; import java.util.Locale;
@ -22,7 +23,8 @@ import org.joda.time.format.DateTimeFormatter;
import org.springframework.format.Printer; import org.springframework.format.Printer;
/** /**
* Prints Joda Time {@link ReadableInstant} instances using a {@link DateTimeFormatter}. * Prints JodaTime {@link ReadableInstant} instances using a {@link DateTimeFormatter}.
*
* @author Keith Donald * @author Keith Donald
* @since 3.0 * @since 3.0
*/ */
@ -31,7 +33,7 @@ public final class ReadableInstantPrinter implements Printer<ReadableInstant> {
private final DateTimeFormatter formatter; private final DateTimeFormatter formatter;
/** /**
* Creates a new ReadableInstantPrinter. * Create a new ReadableInstantPrinter.
* @param formatter the Joda DateTimeFormatter instance * @param formatter the Joda DateTimeFormatter instance
*/ */
public ReadableInstantPrinter(DateTimeFormatter formatter) { public ReadableInstantPrinter(DateTimeFormatter formatter) {
@ -41,4 +43,5 @@ public final class ReadableInstantPrinter implements Printer<ReadableInstant> {
public String print(ReadableInstant instant, Locale locale) { public String print(ReadableInstant instant, Locale locale) {
return JodaTimeContextHolder.getFormatter(this.formatter, locale).print(instant); return JodaTimeContextHolder.getFormatter(this.formatter, locale).print(instant);
} }
} }

View File

@ -13,16 +13,19 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.format.datetime.joda; package org.springframework.format.datetime.joda;
import java.util.Locale; import java.util.Locale;
import org.joda.time.ReadablePartial; import org.joda.time.ReadablePartial;
import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.DateTimeFormatter;
import org.springframework.format.Printer; import org.springframework.format.Printer;
/** /**
* Prints Joda Time {@link ReadablePartial} instances using a {@link DateTimeFormatter}. * Prints JodaTime {@link ReadablePartial} instances using a {@link DateTimeFormatter}.
*
* @author Keith Donald * @author Keith Donald
* @since 3.0 * @since 3.0
*/ */
@ -31,7 +34,7 @@ public final class ReadablePartialPrinter implements Printer<ReadablePartial> {
private final DateTimeFormatter formatter; private final DateTimeFormatter formatter;
/** /**
* Creates a new ReadableInstantPrinter. * Create a new ReadableInstantPrinter.
* @param formatter the Joda DateTimeFormatter instance * @param formatter the Joda DateTimeFormatter instance
*/ */
public ReadablePartialPrinter(DateTimeFormatter formatter) { public ReadablePartialPrinter(DateTimeFormatter formatter) {
@ -41,4 +44,5 @@ public final class ReadablePartialPrinter implements Printer<ReadablePartial> {
public String print(ReadablePartial partial, Locale locale) { public String print(ReadablePartial partial, Locale locale) {
return JodaTimeContextHolder.getFormatter(this.formatter, locale).print(partial); return JodaTimeContextHolder.getFormatter(this.formatter, locale).print(partial);
} }
} }