Skip to content

Commit

Permalink
Remove date format methods not used by main code
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcono1234 committed Nov 26, 2023
1 parent 95fb4af commit 8a3fd5c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,6 @@

/** Provides DateFormats for US locale with patterns which were the default ones before Java 9. */
public class PreJava9DateFormatProvider {

/**
* Returns the same DateFormat as {@code DateFormat.getDateInstance(style, Locale.US)} in Java 8
* or below.
*/
public static DateFormat getUSDateFormat(int style) {
return new SimpleDateFormat(getDateFormatPattern(style), Locale.US);
}

/**
* Returns the same DateFormat as {@code DateFormat.getDateTimeInstance(dateStyle, timeStyle,
* Locale.US)} in Java 8 or below.
Expand All @@ -40,21 +31,6 @@ public static DateFormat getUSDateTimeFormat(int dateStyle, int timeStyle) {
return new SimpleDateFormat(pattern, Locale.US);
}

private static String getDateFormatPattern(int style) {
switch (style) {
case DateFormat.SHORT:
return "M/d/yy";
case DateFormat.MEDIUM:
return "MMM d, y";
case DateFormat.LONG:
return "MMMM d, y";
case DateFormat.FULL:
return "EEEE, MMMM d, y";
default:
throw new IllegalArgumentException("Unknown DateFormat style: " + style);
}
}

private static String getDatePartOfDateTimePattern(int dateStyle) {
switch (dateStyle) {
case DateFormat.SHORT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,9 @@ public final TypeAdapterFactory createAdapterFactory(String datePattern) {
return createFactory(new DefaultDateTypeAdapter<>(this, datePattern));
}

public final TypeAdapterFactory createAdapterFactory(int style) {
return createFactory(new DefaultDateTypeAdapter<>(this, style));
}

public final TypeAdapterFactory createAdapterFactory(int dateStyle, int timeStyle) {
return createFactory(new DefaultDateTypeAdapter<>(this, dateStyle, timeStyle));
}

public final TypeAdapterFactory createDefaultsAdapterFactory() {
return createFactory(
new DefaultDateTypeAdapter<>(this, DateFormat.DEFAULT, DateFormat.DEFAULT));
}
}

private final DateType<T> dateType;
Expand All @@ -134,17 +125,6 @@ private DefaultDateTypeAdapter(DateType<T> dateType, String datePattern) {
}
}

private DefaultDateTypeAdapter(DateType<T> dateType, int style) {
this.dateType = Objects.requireNonNull(dateType);
dateFormats.add(DateFormat.getDateInstance(style, Locale.US));
if (!Locale.getDefault().equals(Locale.US)) {
dateFormats.add(DateFormat.getDateInstance(style));
}
if (JavaVersion.isJava9OrLater()) {
dateFormats.add(PreJava9DateFormatProvider.getUSDateFormat(style));
}
}

private DefaultDateTypeAdapter(DateType<T> dateType, int dateStyle, int timeStyle) {
this.dateType = Objects.requireNonNull(dateType);
dateFormats.add(DateFormat.getDateTimeInstance(dateStyle, timeStyle, Locale.US));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertThrows;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
Expand Down Expand Up @@ -62,10 +62,7 @@ private void assertFormattingAlwaysEmitsUsLocale(Locale locale) {
// Note: \h means "horizontal space", because some JDK versions use Narrow No Break Space
// (U+202F) before the AM or PM indication.
String utcFull = "(Coordinated Universal Time|UTC)";
assertFormatted("Jan 1, 1970,? 12:00:00\\hAM", DateType.DATE.createDefaultsAdapterFactory());
assertFormatted("1/1/70", DateType.DATE.createAdapterFactory(DateFormat.SHORT));
assertFormatted("Jan 1, 1970", DateType.DATE.createAdapterFactory(DateFormat.MEDIUM));
assertFormatted("January 1, 1970", DateType.DATE.createAdapterFactory(DateFormat.LONG));
assertFormatted("Jan 1, 1970,? 12:00:00\\hAM", DefaultDateTypeAdapter.DEFAULT_STYLE_FACTORY);
assertFormatted(
"1/1/70,? 12:00\\hAM",
DateType.DATE.createAdapterFactory(DateFormat.SHORT, DateFormat.SHORT));
Expand Down Expand Up @@ -94,16 +91,7 @@ public void testParsingDatesFormattedWithSystemLocale() throws Exception {
Date date = new Date(0);
assertParsed(
DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM).format(date),
DateType.DATE.createDefaultsAdapterFactory());
assertParsed(
DateFormat.getDateInstance(DateFormat.SHORT).format(date),
DateType.DATE.createAdapterFactory(DateFormat.SHORT));
assertParsed(
DateFormat.getDateInstance(DateFormat.MEDIUM).format(date),
DateType.DATE.createAdapterFactory(DateFormat.MEDIUM));
assertParsed(
DateFormat.getDateInstance(DateFormat.LONG).format(date),
DateType.DATE.createAdapterFactory(DateFormat.LONG));
DefaultDateTypeAdapter.DEFAULT_STYLE_FACTORY);
assertParsed(
DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(date),
DateType.DATE.createAdapterFactory(DateFormat.SHORT, DateFormat.SHORT));
Expand All @@ -129,10 +117,7 @@ public void testParsingDatesFormattedWithUsLocale() throws Exception {
Locale defaultLocale = Locale.getDefault();
Locale.setDefault(Locale.US);
try {
assertParsed("Jan 1, 1970 0:00:00 AM", DateType.DATE.createDefaultsAdapterFactory());
assertParsed("1/1/70", DateType.DATE.createAdapterFactory(DateFormat.SHORT));
assertParsed("Jan 1, 1970", DateType.DATE.createAdapterFactory(DateFormat.MEDIUM));
assertParsed("January 1, 1970", DateType.DATE.createAdapterFactory(DateFormat.LONG));
assertParsed("Jan 1, 1970 0:00:00 AM", DefaultDateTypeAdapter.DEFAULT_STYLE_FACTORY);
assertParsed(
"1/1/70 0:00 AM", DateType.DATE.createAdapterFactory(DateFormat.SHORT, DateFormat.SHORT));
assertParsed(
Expand All @@ -157,8 +142,8 @@ public void testFormatUsesDefaultTimezone() throws Exception {
Locale defaultLocale = Locale.getDefault();
Locale.setDefault(Locale.US);
try {
assertFormatted("Dec 31, 1969,? 4:00:00\\hPM", DateType.DATE.createDefaultsAdapterFactory());
assertParsed("Dec 31, 1969 4:00:00 PM", DateType.DATE.createDefaultsAdapterFactory());
assertFormatted("Dec 31, 1969,? 4:00:00\\hPM", DefaultDateTypeAdapter.DEFAULT_STYLE_FACTORY);
assertParsed("Dec 31, 1969 4:00:00 PM", DefaultDateTypeAdapter.DEFAULT_STYLE_FACTORY);
} finally {
TimeZone.setDefault(defaultTimeZone);
Locale.setDefault(defaultLocale);
Expand All @@ -167,25 +152,14 @@ public void testFormatUsesDefaultTimezone() throws Exception {

@Test
public void testDateDeserializationISO8601() throws Exception {
TypeAdapterFactory adapterFactory = DateType.DATE.createDefaultsAdapterFactory();
TypeAdapterFactory adapterFactory = DefaultDateTypeAdapter.DEFAULT_STYLE_FACTORY;
assertParsed("1970-01-01T00:00:00.000Z", adapterFactory);
assertParsed("1970-01-01T00:00Z", adapterFactory);
assertParsed("1970-01-01T00:00:00+00:00", adapterFactory);
assertParsed("1970-01-01T01:00:00+01:00", adapterFactory);
assertParsed("1970-01-01T01:00:00+01", adapterFactory);
}

@Test
public void testDateSerialization() {
int dateStyle = DateFormat.LONG;
TypeAdapter<Date> dateTypeAdapter = dateAdapter(DateType.DATE.createAdapterFactory(dateStyle));
DateFormat formatter = DateFormat.getDateInstance(dateStyle, Locale.US);
Date currentDate = new Date();

String dateString = dateTypeAdapter.toJson(currentDate);
assertThat(dateString).isEqualTo(toLiteral(formatter.format(currentDate)));
}

@Test
public void testDatePattern() {
String pattern = "yyyy-MM-dd";
Expand All @@ -199,28 +173,24 @@ public void testDatePattern() {

@Test
public void testInvalidDatePattern() {
try {
DateType.DATE.createAdapterFactory("I am a bad Date pattern....");
fail("Invalid date pattern should fail.");
} catch (IllegalArgumentException expected) {
}
assertThrows(
IllegalArgumentException.class,
() -> DateType.DATE.createAdapterFactory("I am a bad Date pattern...."));
}

@Test
public void testNullValue() throws Exception {
TypeAdapter<Date> adapter = dateAdapter(DateType.DATE.createDefaultsAdapterFactory());
TypeAdapter<Date> adapter = dateAdapter(DefaultDateTypeAdapter.DEFAULT_STYLE_FACTORY);
assertThat(adapter.fromJson("null")).isNull();
assertThat(adapter.toJson(null)).isEqualTo("null");
}

@Test
public void testUnexpectedToken() throws Exception {
try {
TypeAdapter<Date> adapter = dateAdapter(DateType.DATE.createDefaultsAdapterFactory());
adapter.fromJson("{}");
fail("Unexpected token should fail.");
} catch (IllegalStateException expected) {
}
TypeAdapter<Date> adapter = dateAdapter(DefaultDateTypeAdapter.DEFAULT_STYLE_FACTORY);
IllegalStateException e =
assertThrows(IllegalStateException.class, () -> adapter.fromJson("{}"));
assertThat(e).hasMessageThat().startsWith("Expected a string but was BEGIN_OBJECT");
}

private static TypeAdapter<Date> dateAdapter(TypeAdapterFactory adapterFactory) {
Expand Down

0 comments on commit 8a3fd5c

Please sign in to comment.