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

Commit

Permalink
Alter storage used by LocalDate to int-short-short
Browse files Browse the repository at this point in the history
Fixes #13
  • Loading branch information
jodastephen committed May 1, 2012
1 parent 5382148 commit c03b983
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 42 deletions.
104 changes: 62 additions & 42 deletions src/main/java/javax/time/LocalDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ public final class LocalDate
/**
* The month-of-year, not null.
*/
private final MonthOfYear month;
private final short month;
/**
* The day-of-month.
*/
private final int day;
private final short day;

//-----------------------------------------------------------------------
/**
Expand Down Expand Up @@ -287,7 +287,7 @@ static LocalDate ofYearZeroDay(long zeroDay) {

// check year now we are certain it is correct
int year = YEAR.checkValidIntValue(yearEst);
return new LocalDate(year, MonthOfYear.of(month), dom);
return new LocalDate(year, month, dom);
}

//-----------------------------------------------------------------------
Expand Down Expand Up @@ -357,7 +357,30 @@ private static LocalDate create(int year, MonthOfYear monthOfYear, int dayOfMont
throw new CalendricalException("Invalid date '" + monthOfYear.name() + " " + dayOfMonth + "'");
}
}
return new LocalDate(year, monthOfYear, dayOfMonth);
return new LocalDate(year, monthOfYear.getValue(), dayOfMonth);
}

/**
* Resolves the date, resolving days past the end of month.
*
* @param year the year to represent, validated from MIN_YEAR to MAX_YEAR
* @param monthOfYear the month-of-year to represent, validated from 1 to 12
* @param dayOfMonth the day-of-month to represent, validated from 1 to 31
* @return the resolved date, not null
*/
private static LocalDate resolvePreviousValid(int year, int month, int day) {
switch (month) {
case 2:
day = Math.min(day, DateTimes.isLeapYear(year) ? 29 : 28);
break;
case 4:
case 6:
case 9:
case 11:
day = Math.min(day, 30);
break;
}
return LocalDate.of(year, month, day);
}

/**
Expand All @@ -367,10 +390,10 @@ private static LocalDate create(int year, MonthOfYear monthOfYear, int dayOfMont
* @param monthOfYear the month-of-year to represent, not null
* @param dayOfMonth the day-of-month to represent, valid for year-month, from 1 to 31
*/
private LocalDate(int year, MonthOfYear monthOfYear, int dayOfMonth) {
private LocalDate(int year, int monthOfYear, int dayOfMonth) {
this.year = year;
this.month = monthOfYear;
this.day = dayOfMonth;
this.month = (short) monthOfYear;
this.day = (short) dayOfMonth;
}

//-----------------------------------------------------------------------
Expand Down Expand Up @@ -406,6 +429,20 @@ public int getYear() {
return year;
}

/**
* Gets the month-of-year field from 1 to 12.
* <p>
* This method returns the month as an {@code int} from 1 to 12.
* Application code is frequently clearer if the enum {@link MonthOfYear}
* is used by calling {@link #getMonthOfYear()}.
*
* @return the month-of-year, from 1 to 12
* @see #getMonthOfYear()
*/
public int getMonth() {
return month;
}

/**
* Gets the month-of-year field, which is an enum {@code MonthOfYear}.
* <p>
Expand All @@ -415,9 +452,10 @@ public int getYear() {
* provides the {@link MonthOfYear#getValue() int value}.
*
* @return the month-of-year, not null
* @see #getMonth()
*/
public MonthOfYear getMonthOfYear() {
return month;
return MonthOfYear.of(month);
}

/**
Expand Down Expand Up @@ -484,26 +522,6 @@ public boolean isLeapYear() {
}

//-----------------------------------------------------------------------
/**
* Resolves the date, handling incorrectly implemented resolvers.
*
* @param dateResolver the resolver, not null
* @param year the year, from MIN_YEAR to MAX_YEAR
* @param month the month, not null
* @param day the day-of-month, from 1 to 31
* @return the resolved date, not null
* @throws NullPointerException if the resolver returned null
*/
private LocalDate resolvePreviousValid(int year, MonthOfYear month, int day) {
YEAR.checkValidValue(year);
DAY_OF_MONTH.checkValidValue(day);
int lastDay = month.lengthInDays(DateTimes.isLeapYear(year));
if (day > lastDay) {
return LocalDate.of(year, month, lastDay);
}
return LocalDate.of(year, month, day);
}

/**
* Returns a copy of this {@code LocalDate} with the date altered using the adjuster.
* <p>
Expand Down Expand Up @@ -560,6 +578,7 @@ public LocalDate withYear(int year) {
if (this.year == year) {
return this;
}
YEAR.checkValidValue(year);
return resolvePreviousValid(year, month, day);
}

Expand All @@ -574,7 +593,11 @@ public LocalDate withYear(int year) {
* @throws CalendricalException if the month-of-year value is invalid
*/
public LocalDate withMonthOfYear(int monthOfYear) {
return with(MonthOfYear.of(monthOfYear));
if (this.month == monthOfYear) {
return this;
}
MONTH_OF_YEAR.checkValidValue(monthOfYear);
return resolvePreviousValid(year, monthOfYear, day);
}

/**
Expand All @@ -588,10 +611,7 @@ public LocalDate withMonthOfYear(int monthOfYear) {
*/
public LocalDate with(MonthOfYear monthOfYear) {
DateTimes.checkNotNull(monthOfYear, "MonthOfYear must not be null");
if (this.month == monthOfYear) {
return this;
}
return resolvePreviousValid(year, monthOfYear, day);
return withMonthOfYear(monthOfYear.getValue());
}

/**
Expand Down Expand Up @@ -719,10 +739,10 @@ public LocalDate plusMonths(long months) {
if (months == 0) {
return this;
}
long monthCount = year * 12L + (month.getValue() - 1);
long monthCount = year * 12L + (month - 1);
long calcMonths = monthCount + months; // safe overflow
int newYear = YEAR.checkValidIntValue(DateTimes.floorDiv(calcMonths, 12));
MonthOfYear newMonth = MonthOfYear.of(DateTimes.floorMod(calcMonths, 12) + 1);
int newMonth = DateTimes.floorMod(calcMonths, 12) + 1;
return resolvePreviousValid(newYear, newMonth, day);
}

Expand Down Expand Up @@ -857,10 +877,10 @@ public LocalDate minusMonths(long months) {
if (months == 0) {
return this;
}
long monthCount = year * 12L + (month.getValue() - 1);
long monthCount = year * 12L + (month - 1);
long calcMonths = monthCount - months; // safe overflow
int newYear = YEAR.checkValidIntValue(DateTimes.floorDiv(calcMonths, 12));
MonthOfYear newMonth = MonthOfYear.of(DateTimes.floorMod(calcMonths, 12) + 1);
int newMonth = DateTimes.floorMod(calcMonths, 12) + 1;
return resolvePreviousValid(newYear, newMonth, day);
}

Expand Down Expand Up @@ -1131,7 +1151,7 @@ public long toModifiedJulianDay() {
*/
long toYearZeroDay() {
long y = year;
long m = month.getValue();
long m = month;
long total = 0;
total += 365 * y;
if (y >= 0) {
Expand Down Expand Up @@ -1162,9 +1182,9 @@ long toYearZeroDay() {
public int compareTo(LocalDate other) {
int cmp = DateTimes.safeCompare(year, other.year);
if (cmp == 0) {
cmp = month.compareTo(other.month);
cmp = (month - other.month);
if (cmp == 0) {
cmp = DateTimes.safeCompare(day, other.day);
cmp = (day - other.day);
}
}
return cmp;
Expand Down Expand Up @@ -1223,7 +1243,7 @@ public boolean equals(Object obj) {
@Override
public int hashCode() {
int yearValue = year;
int monthValue = month.getValue();
int monthValue = month;
int dayValue = day;
return (yearValue & 0xFFFFF800) ^ ((yearValue << 11) + (monthValue << 6) + (dayValue));
}
Expand All @@ -1239,7 +1259,7 @@ public int hashCode() {
@Override
public String toString() {
int yearValue = year;
int monthValue = month.getValue();
int monthValue = month;
int dayValue = day;
int absYear = Math.abs(yearValue);
StringBuilder buf = new StringBuilder(10);
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/javax/time/LocalDateTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,20 @@ public int getYear() {
return date.getYear();
}

/**
* Gets the month-of-year field from 1 to 12.
* <p>
* This method returns the month as an {@code int} from 1 to 12.
* Application code is frequently clearer if the enum {@link MonthOfYear}
* is used by calling {@link #getMonthOfYear()}.
*
* @return the month-of-year, from 1 to 12
* @see #getMonthOfYear()
*/
public int getMonth() {
return date.getMonth();
}

/**
* Gets the month-of-year field, which is an enum {@code MonthOfYear}.
* <p>
Expand All @@ -489,6 +503,7 @@ public int getYear() {
* provides the {@link MonthOfYear#getValue() int value}.
*
* @return the month-of-year, not null
* @see #getMonth()
*/
public MonthOfYear getMonthOfYear() {
return date.getMonthOfYear();
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/javax/time/OffsetDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,20 @@ public int getYear() {
return date.getYear();
}

/**
* Gets the month-of-year field from 1 to 12.
* <p>
* This method returns the month as an {@code int} from 1 to 12.
* Application code is frequently clearer if the enum {@link MonthOfYear}
* is used by calling {@link #getMonthOfYear()}.
*
* @return the month-of-year, from 1 to 12
* @see #getMonthOfYear()
*/
public int getMonth() {
return date.getMonth();
}

/**
* Gets the month-of-year field, which is an enum {@code MonthOfYear}.
* <p>
Expand All @@ -335,6 +349,7 @@ public int getYear() {
* provides the {@link MonthOfYear#getValue() int value}.
*
* @return the month-of-year, not null
* @see #getMonth()
*/
public MonthOfYear getMonthOfYear() {
return date.getMonthOfYear();
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/javax/time/OffsetDateTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,20 @@ public int getYear() {
return dateTime.getYear();
}

/**
* Gets the month-of-year field from 1 to 12.
* <p>
* This method returns the month as an {@code int} from 1 to 12.
* Application code is frequently clearer if the enum {@link MonthOfYear}
* is used by calling {@link #getMonthOfYear()}.
*
* @return the month-of-year, from 1 to 12
* @see #getMonthOfYear()
*/
public int getMonth() {
return dateTime.getMonth();
}

/**
* Gets the month-of-year field, which is an enum {@code MonthOfYear}.
* <p>
Expand All @@ -605,6 +619,7 @@ public int getYear() {
* provides the {@link MonthOfYear#getValue() int value}.
*
* @return the month-of-year, not null
* @see #getMonth()
*/
public MonthOfYear getMonthOfYear() {
return dateTime.getMonthOfYear();
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/javax/time/ZonedDateTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,20 @@ public int getYear() {
return dateTime.getYear();
}

/**
* Gets the month-of-year field from 1 to 12.
* <p>
* This method returns the month as an {@code int} from 1 to 12.
* Application code is frequently clearer if the enum {@link MonthOfYear}
* is used by calling {@link #getMonthOfYear()}.
*
* @return the month-of-year, from 1 to 12
* @see #getMonthOfYear()
*/
public int getMonth() {
return dateTime.getMonth();
}

/**
* Gets the month-of-year field, which is an enum {@code MonthOfYear}.
* <p>
Expand All @@ -824,6 +838,7 @@ public int getYear() {
* provides the {@link MonthOfYear#getValue() int value}.
*
* @return the month-of-year, not null
* @see #getMonth()
*/
public MonthOfYear getMonthOfYear() {
return dateTime.getMonthOfYear();
Expand Down

0 comments on commit c03b983

Please sign in to comment.