Skip to content
This repository has been archived by the owner on Mar 20, 2018. It is now read-only.

Commit

Permalink
Use Objects.requireNonNull()
Browse files Browse the repository at this point in the history
See #71
  • Loading branch information
jodastephen committed Oct 24, 2012
1 parent 3f99c25 commit 2d29ed9
Show file tree
Hide file tree
Showing 41 changed files with 245 additions and 267 deletions.
7 changes: 3 additions & 4 deletions src-extra/main/java/javax/time/extra/DayOfYear.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import static javax.time.calendrical.LocalDateTimeField.YEAR;

import java.io.Serializable;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReferenceArray;

import javax.time.DateTimeException;
Expand Down Expand Up @@ -180,9 +181,7 @@ public DateTime doAdjustment(DateTime calendrical) {
* @return true if this day-of-year is valid for the year
*/
public boolean isValid(Year year) {
if (year == null) {
throw new NullPointerException("Year must not be null");
}
Objects.requireNonNull(year, "Year");
return (dayOfYear < 366 || year.isLeap());
}

Expand Down Expand Up @@ -213,7 +212,7 @@ public boolean isValid(int year) {
*/
public LocalDate atYear(Year year) {
if (year == null) {
throw new NullPointerException("Year must not be null");
throw new NullPointerException("Year");
}
return year.atDay(dayOfYear);
}
Expand Down
4 changes: 2 additions & 2 deletions src-extra/main/java/javax/time/extra/QuarterOfYear.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
import static javax.time.extra.QuarterYearField.QUARTER_OF_YEAR;

import java.util.Locale;
import java.util.Objects;

import javax.time.DateTimeException;
import javax.time.DateTimes;
import javax.time.Month;
import javax.time.calendrical.DateTime;
import javax.time.calendrical.DateTime.WithAdjuster;
Expand Down Expand Up @@ -129,7 +129,7 @@ public static QuarterOfYear of(int quarterOfYear) {
* @return the QuarterOfYear singleton, not null
*/
public static QuarterOfYear ofMonth(Month month) {
DateTimes.checkNotNull(month, "Month must not be null");
Objects.requireNonNull(month, "Month");
return of(month.ordinal() / 3 + 1);
}

Expand Down
5 changes: 3 additions & 2 deletions src-extra/main/java/javax/time/extra/TAIInstant.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@
package javax.time.extra;

import java.io.Serializable;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.time.DateTimeException;
import javax.time.DateTimes;
import javax.time.Duration;
import javax.time.Instant;
import javax.time.DateTimes;
import javax.time.format.DateTimeParseException;

/**
Expand Down Expand Up @@ -190,7 +191,7 @@ public static TAIInstant of(UTCInstant instant) {
* @throws DateTimeException if the text cannot be parsed
*/
public static TAIInstant parse(CharSequence text) {
DateTimes.checkNotNull(text, "Text to parse must not be null");
Objects.requireNonNull(text, "Text ");
Matcher matcher = PARSER.matcher(text);
if (matcher.matches()) {
try {
Expand Down
5 changes: 2 additions & 3 deletions src-standard/main/java/javax/time/chrono/Chronology.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@

import java.util.HashSet;
import java.util.Locale;
import java.util.Objects;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import javax.time.Clock;
import javax.time.DateTimeException;
import javax.time.DateTimes;
import javax.time.LocalDate;
import javax.time.ZoneId;
import javax.time.calendrical.DateTimeAccessor;
Expand Down Expand Up @@ -140,7 +140,7 @@ public static Chronology from(DateTimeAccessor dateTime) {
* @throws DateTimeException if the locale-specified calendar cannot be found
*/
public static Chronology ofLocale(Locale locale) {
DateTimes.checkNotNull(locale, "Locale must not be null");
Objects.requireNonNull(locale, "Locale");
String type = locale.getUnicodeLocaleType("ca");
if (type == null) {
return ISOChronology.INSTANCE;
Expand Down Expand Up @@ -345,7 +345,6 @@ public ChronoDate now(ZoneId zone) {
* @return the current date, not null
*/
public ChronoDate now(Clock clock) {
DateTimes.checkNotNull(clock, "Clock must not be null");
return dateFromEpochDay(LocalDate.now(clock).toEpochDay());
}

Expand Down
3 changes: 2 additions & 1 deletion src-standard/main/java/javax/time/chrono/HijrahDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.io.Serializable;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Objects;
import java.util.StringTokenizer;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
Expand Down Expand Up @@ -433,7 +434,7 @@ public static HijrahDate of(int prolepticYear, int monthOfYear, int dayOfMonth)
* @throws InvalidCalendarFieldException if the day-of-month is invalid for the month-year
*/
public static HijrahDate of(HijrahEra era, int yearOfEra, int monthOfYear, int dayOfMonth) {
DateTimes.checkNotNull(era, "HijrahEra must not be null");
Objects.requireNonNull(era, "HijrahEra");
checkValidYearOfEra(yearOfEra);
checkValidMonth(monthOfYear);
checkValidDayOfMonth(dayOfMonth);
Expand Down
4 changes: 2 additions & 2 deletions src-standard/main/java/javax/time/chrono/ISODate.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
package javax.time.chrono;

import java.io.Serializable;
import java.util.Objects;

import javax.time.DateTimes;
import javax.time.LocalDate;
import javax.time.calendrical.DateTimeField;
import javax.time.calendrical.DateTimeValueRange;
Expand Down Expand Up @@ -67,7 +67,7 @@ final class ISODate extends ChronoDate implements Comparable<ChronoDate>, Serial
* @param date the time-line date, not null
*/
ISODate(LocalDate date) {
DateTimes.checkNotNull(date, "LocalDate must not be null");
Objects.requireNonNull(date, "LocalDate");
this.isoDate = date;
}

Expand Down
4 changes: 2 additions & 2 deletions src-standard/main/java/javax/time/chrono/JapaneseDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Objects;

import javax.time.DateTimeException;
import javax.time.DateTimes;
import javax.time.LocalDate;
import javax.time.calendrical.DateTimeField;
import javax.time.calendrical.DateTimeValueRange;
Expand Down Expand Up @@ -106,7 +106,7 @@ public static JapaneseDate of(int prolepticYear, int month, int dayOfMonth) {
* if the day-of-month is invalid for the month-year
*/
static JapaneseDate of(JapaneseEra era, int yearOfEra, int month, int dayOfMonth) {
DateTimes.checkNotNull(era, "era must not be null");
Objects.requireNonNull(era, "Era");
LocalGregorianCalendar.Date jdate = JapaneseChronology.JCAL.newCalendarDate(null);
jdate.setEra(era.getPrivateEra()).setDate(yearOfEra, month, dayOfMonth);
if (!JapaneseChronology.JCAL.validate(jdate)) {
Expand Down
4 changes: 2 additions & 2 deletions src-standard/main/java/javax/time/chrono/MinguoDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
import static javax.time.chrono.MinguoChronology.YEARS_DIFFERENCE;

import java.io.Serializable;
import java.util.Objects;

import javax.time.DateTimes;
import javax.time.LocalDate;
import javax.time.calendrical.DateTimeField;
import javax.time.calendrical.DateTimeValueRange;
Expand Down Expand Up @@ -71,7 +71,7 @@ final class MinguoDate extends ChronoDate implements Comparable<ChronoDate>, Ser
* @param date the time-line date, not null
*/
MinguoDate(LocalDate date) {
DateTimes.checkNotNull(date, "LocalDate must not be null");
Objects.requireNonNull(date, "LocalDate");
this.isoDate = date;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
import static javax.time.chrono.ThaiBuddhistChronology.YEARS_DIFFERENCE;

import java.io.Serializable;
import java.util.Objects;

import javax.time.DateTimeException;
import javax.time.DateTimes;
import javax.time.LocalDate;
import javax.time.calendrical.DateTimeAccessor;
import javax.time.calendrical.DateTimeField;
Expand Down Expand Up @@ -105,7 +105,7 @@ public static ChronoDate ofEpochDay(long epochDay) {
* @param date the time-line date, not null
*/
ThaiBuddhistDate(LocalDate date) {
DateTimes.checkNotNull(date, "LocalDate must not be null");
Objects.requireNonNull(date, "LocalDate");
this.isoDate = date;
}

Expand Down
17 changes: 9 additions & 8 deletions src/main/java/javax/time/Clock.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import static javax.time.DateTimes.NANOS_PER_SECOND;

import java.io.Serializable;
import java.util.Objects;
import java.util.TimeZone;

/**
Expand Down Expand Up @@ -123,7 +124,7 @@ public static Clock systemUTC() {
* @return a clock that uses the best available system clock in the specified zone, not null
*/
public static Clock system(ZoneId zone) {
DateTimes.checkNotNull(zone, "ZoneId must not be null");
Objects.requireNonNull(zone, "ZoneId");
return new SystemClock(zone);
}

Expand Down Expand Up @@ -203,8 +204,8 @@ public static Clock tickMinutes(ZoneId zone) {
* @throws ArithmeticException if the duration is too large
*/
public static Clock tick(Clock baseClock, Duration tickDuration) {
DateTimes.checkNotNull(baseClock, "Clock must not be null");
DateTimes.checkNotNull(tickDuration, "Duration must not be null");
Objects.requireNonNull(baseClock, "Clock");
Objects.requireNonNull(tickDuration, "Duration");
if (tickDuration.isNegative()) {
throw new IllegalArgumentException("Duration must not be negative");
}
Expand All @@ -230,7 +231,7 @@ public static Clock tick(Clock baseClock, Duration tickDuration) {
* @return a clock that always returns the same instant, not null
*/
public static Clock fixedUTC(Instant fixedInstant) {
DateTimes.checkNotNull(fixedInstant, "Instant must not be null");
Objects.requireNonNull(fixedInstant, "Instant");
return new FixedClock(fixedInstant, ZoneId.UTC);
}

Expand All @@ -249,8 +250,8 @@ public static Clock fixedUTC(Instant fixedInstant) {
* @return a clock that always returns the same instant, not null
*/
public static Clock fixed(Instant fixedInstant, ZoneId zone) {
DateTimes.checkNotNull(fixedInstant, "Instant must not be null");
DateTimes.checkNotNull(zone, "ZoneId must not be null");
Objects.requireNonNull(fixedInstant, "Instant");
Objects.requireNonNull(zone, "ZoneId");
return new FixedClock(fixedInstant, zone);
}

Expand All @@ -269,8 +270,8 @@ public static Clock fixed(Instant fixedInstant, ZoneId zone) {
* @return a {@code TimeSource} that is offset from the system millisecond clock, not null
*/
public static Clock offset(Clock baseClock, Duration offset) {
DateTimes.checkNotNull(baseClock, "Clock must not be null");
DateTimes.checkNotNull(offset, "Duration must not be null");
Objects.requireNonNull(baseClock, "Clock");
Objects.requireNonNull(offset, "Duration");
if (offset.equals(Duration.ZERO)) {
return baseClock;
}
Expand Down
13 changes: 0 additions & 13 deletions src/main/java/javax/time/DateTimes.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,6 @@ private DateTimes() {
}

//-----------------------------------------------------------------------
/**
* Validates that the specified object is not null.
*
* @param object the object to check
* @param errorMessage the error message to use if null
* @throws NullPointerException if the object is null
*/
public static void checkNotNull(Object object, String errorMessage) {
if (object == null) {
throw new NullPointerException(errorMessage);
}
}

/**
* Validates that the specified object is not null.
*
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/javax/time/Duration.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.Objects;

import javax.time.calendrical.DateTime;
import javax.time.calendrical.DateTime.MinusAdjuster;
Expand Down Expand Up @@ -288,7 +289,7 @@ public static Duration between(DateTimeAccessor startInclusive, DateTimeAccessor
* @throws DateTimeParseException if the text cannot be parsed to a {@code Duration}
*/
public static Duration parse(final CharSequence text) {
DateTimes.checkNotNull(text, "Text to parse must not be null");
Objects.requireNonNull(text, "Text to parse");
int len = text.length();
if (len < 4 ||
(text.charAt(0) != 'P' && text.charAt(0) != 'p') ||
Expand Down Expand Up @@ -497,6 +498,7 @@ public Duration plus(Duration duration) {
* @throws ArithmeticException if numeric overflow occurs
*/
public Duration plus(long amountToAdd, PeriodUnit unit) {
Objects.requireNonNull(unit, "PeriodUnit");
if (unit == DAYS) {
return plus(DateTimes.safeMultiply(amountToAdd, DateTimes.SECONDS_PER_DAY), 0);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/javax/time/Instant.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import static javax.time.calendrical.LocalDateTimeField.NANO_OF_SECOND;

import java.io.Serializable;
import java.util.Objects;

import javax.time.calendrical.DateTime;
import javax.time.calendrical.DateTime.WithAdjuster;
Expand Down Expand Up @@ -193,7 +194,7 @@ public static Instant now() {
* @return the current instant, not null
*/
public static Instant now(Clock clock) {
DateTimes.checkNotNull(clock, "Clock must not be null");
Objects.requireNonNull(clock, "Clock");
return clock.instant();
}

Expand Down
9 changes: 5 additions & 4 deletions src/main/java/javax/time/LocalDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import static javax.time.calendrical.LocalDateTimeField.YEAR;

import java.io.Serializable;
import java.util.Objects;

import javax.time.calendrical.DateTime;
import javax.time.calendrical.DateTime.WithAdjuster;
Expand Down Expand Up @@ -167,7 +168,7 @@ public static LocalDate now(ZoneId zone) {
* @return the current date, not null
*/
public static LocalDate now(Clock clock) {
DateTimes.checkNotNull(clock, "Clock must not be null");
Objects.requireNonNull(clock, "Clock");
// inline OffsetDate factory to avoid creating object and InstantProvider checks
final Instant now = clock.instant(); // called once
ZoneOffset offset = clock.getZone().getRules().getOffset(now);
Expand All @@ -191,7 +192,7 @@ public static LocalDate now(Clock clock) {
*/
public static LocalDate of(int year, Month month, int dayOfMonth) {
YEAR.checkValidValue(year);
DateTimes.checkNotNull(month, "Month must not be null");
Objects.requireNonNull(month, "Month");
DAY_OF_MONTH.checkValidValue(dayOfMonth);
return create(year, month, dayOfMonth);
}
Expand Down Expand Up @@ -328,7 +329,7 @@ public static LocalDate parse(CharSequence text) {
* @throws DateTimeParseException if the text cannot be parsed
*/
public static LocalDate parse(CharSequence text, CalendricalFormatter formatter) {
DateTimes.checkNotNull(formatter, "CalendricalFormatter must not be null");
Objects.requireNonNull(formatter, "CalendricalFormatter");
return formatter.parse(text, LocalDate.class);
}

Expand Down Expand Up @@ -1380,7 +1381,7 @@ public String toString() {
* @throws DateTimeException if an error occurs during printing
*/
public String toString(CalendricalFormatter formatter) {
DateTimes.checkNotNull(formatter, "CalendricalFormatter must not be null");
Objects.requireNonNull(formatter, "CalendricalFormatter");
return formatter.print(this);
}

Expand Down
Loading

4 comments on commit 2d29ed9

@sjmisterm
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JDK encouraged pattern is to use the argument name in the message. Most occurrences in our code base use the type instead. Any good reason not to use arguments instead? I can change it if you think it's better.

@jodastephen
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you any evidence that the JDK is consistent about this? I couldn't google a coding standards doc.

@sjmisterm
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The javadoc for the method uses the argument name. If two arguments are of the same time, this is the only way to distinguish them if you don't have source code available.

@jodastephen
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its probably worth changing as you say. There are a few cases where the parameter name should be changed so this works, such as where it is currently "id" but should be "timeZoneId" or "offsetId" etc. Feel free to make the change ;-)

Please sign in to comment.