Refactor duplicate code.

This commit is contained in:
Gary Gregory 2019-09-03 16:20:19 -04:00
parent 4f0f75cdb6
commit 5e7d64d6b2
1 changed files with 18 additions and 14 deletions

View File

@ -165,7 +165,7 @@ public class DateUtils {
*/ */
public static boolean isSameDay(final Date date1, final Date date2) { public static boolean isSameDay(final Date date1, final Date date2) {
if (date1 == null || date2 == null) { if (date1 == null || date2 == null) {
throw new IllegalArgumentException("The date must not be null"); throw nullDateIllegalArgumentException();
} }
final Calendar cal1 = Calendar.getInstance(); final Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1); cal1.setTime(date1);
@ -189,7 +189,7 @@ public class DateUtils {
*/ */
public static boolean isSameDay(final Calendar cal1, final Calendar cal2) { public static boolean isSameDay(final Calendar cal1, final Calendar cal2) {
if (cal1 == null || cal2 == null) { if (cal1 == null || cal2 == null) {
throw new IllegalArgumentException("The date must not be null"); throw nullDateIllegalArgumentException();
} }
return cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) && return cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) &&
cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
@ -210,7 +210,7 @@ public class DateUtils {
*/ */
public static boolean isSameInstant(final Date date1, final Date date2) { public static boolean isSameInstant(final Date date1, final Date date2) {
if (date1 == null || date2 == null) { if (date1 == null || date2 == null) {
throw new IllegalArgumentException("The date must not be null"); throw nullDateIllegalArgumentException();
} }
return date1.getTime() == date2.getTime(); return date1.getTime() == date2.getTime();
} }
@ -228,7 +228,7 @@ public class DateUtils {
*/ */
public static boolean isSameInstant(final Calendar cal1, final Calendar cal2) { public static boolean isSameInstant(final Calendar cal1, final Calendar cal2) {
if (cal1 == null || cal2 == null) { if (cal1 == null || cal2 == null) {
throw new IllegalArgumentException("The date must not be null"); throw nullDateIllegalArgumentException();
} }
return cal1.getTime().getTime() == cal2.getTime().getTime(); return cal1.getTime().getTime() == cal2.getTime().getTime();
} }
@ -248,7 +248,7 @@ public class DateUtils {
*/ */
public static boolean isSameLocalTime(final Calendar cal1, final Calendar cal2) { public static boolean isSameLocalTime(final Calendar cal1, final Calendar cal2) {
if (cal1 == null || cal2 == null) { if (cal1 == null || cal2 == null) {
throw new IllegalArgumentException("The date must not be null"); throw nullDateIllegalArgumentException();
} }
return cal1.get(Calendar.MILLISECOND) == cal2.get(Calendar.MILLISECOND) && return cal1.get(Calendar.MILLISECOND) == cal2.get(Calendar.MILLISECOND) &&
cal1.get(Calendar.SECOND) == cal2.get(Calendar.SECOND) && cal1.get(Calendar.SECOND) == cal2.get(Calendar.SECOND) &&
@ -741,13 +741,17 @@ public class DateUtils {
*/ */
public static Calendar round(final Calendar date, final int field) { public static Calendar round(final Calendar date, final int field) {
if (date == null) { if (date == null) {
throw new IllegalArgumentException("The date must not be null"); throw nullDateIllegalArgumentException();
} }
final Calendar rounded = (Calendar) date.clone(); final Calendar rounded = (Calendar) date.clone();
modify(rounded, field, ModifyType.ROUND); modify(rounded, field, ModifyType.ROUND);
return rounded; return rounded;
} }
private static IllegalArgumentException nullDateIllegalArgumentException() {
return new IllegalArgumentException("The date must not be null");
}
/** /**
* <p>Rounds a date, leaving the field specified as the most * <p>Rounds a date, leaving the field specified as the most
* significant field.</p> * significant field.</p>
@ -778,7 +782,7 @@ public class DateUtils {
*/ */
public static Date round(final Object date, final int field) { public static Date round(final Object date, final int field) {
if (date == null) { if (date == null) {
throw new IllegalArgumentException("The date must not be null"); throw nullDateIllegalArgumentException();
} }
if (date instanceof Date) { if (date instanceof Date) {
return round((Date) date, field); return round((Date) date, field);
@ -830,7 +834,7 @@ public class DateUtils {
*/ */
public static Calendar truncate(final Calendar date, final int field) { public static Calendar truncate(final Calendar date, final int field) {
if (date == null) { if (date == null) {
throw new IllegalArgumentException("The date must not be null"); throw nullDateIllegalArgumentException();
} }
final Calendar truncated = (Calendar) date.clone(); final Calendar truncated = (Calendar) date.clone();
modify(truncated, field, ModifyType.TRUNCATE); modify(truncated, field, ModifyType.TRUNCATE);
@ -855,7 +859,7 @@ public class DateUtils {
*/ */
public static Date truncate(final Object date, final int field) { public static Date truncate(final Object date, final int field) {
if (date == null) { if (date == null) {
throw new IllegalArgumentException("The date must not be null"); throw nullDateIllegalArgumentException();
} }
if (date instanceof Date) { if (date instanceof Date) {
return truncate((Date) date, field); return truncate((Date) date, field);
@ -909,7 +913,7 @@ public class DateUtils {
*/ */
public static Calendar ceiling(final Calendar date, final int field) { public static Calendar ceiling(final Calendar date, final int field) {
if (date == null) { if (date == null) {
throw new IllegalArgumentException("The date must not be null"); throw nullDateIllegalArgumentException();
} }
final Calendar ceiled = (Calendar) date.clone(); final Calendar ceiled = (Calendar) date.clone();
modify(ceiled, field, ModifyType.CEILING); modify(ceiled, field, ModifyType.CEILING);
@ -935,7 +939,7 @@ public class DateUtils {
*/ */
public static Date ceiling(final Object date, final int field) { public static Date ceiling(final Object date, final int field) {
if (date == null) { if (date == null) {
throw new IllegalArgumentException("The date must not be null"); throw nullDateIllegalArgumentException();
} }
if (date instanceof Date) { if (date instanceof Date) {
return ceiling((Date) date, field); return ceiling((Date) date, field);
@ -1153,7 +1157,7 @@ public class DateUtils {
*/ */
public static Iterator<Calendar> iterator(final Calendar focus, final int rangeStyle) { public static Iterator<Calendar> iterator(final Calendar focus, final int rangeStyle) {
if (focus == null) { if (focus == null) {
throw new IllegalArgumentException("The date must not be null"); throw nullDateIllegalArgumentException();
} }
Calendar start = null; Calendar start = null;
Calendar end = null; Calendar end = null;
@ -1243,7 +1247,7 @@ public class DateUtils {
*/ */
public static Iterator<?> iterator(final Object focus, final int rangeStyle) { public static Iterator<?> iterator(final Object focus, final int rangeStyle) {
if (focus == null) { if (focus == null) {
throw new IllegalArgumentException("The date must not be null"); throw nullDateIllegalArgumentException();
} }
if (focus instanceof Date) { if (focus instanceof Date) {
return iterator((Date) focus, rangeStyle); return iterator((Date) focus, rangeStyle);
@ -1663,7 +1667,7 @@ public class DateUtils {
*/ */
private static long getFragment(final Calendar calendar, final int fragment, final TimeUnit unit) { private static long getFragment(final Calendar calendar, final int fragment, final TimeUnit unit) {
if (calendar == null) { if (calendar == null) {
throw new IllegalArgumentException("The date must not be null"); throw nullDateIllegalArgumentException();
} }
long result = 0; long result = 0;