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

Commit

Permalink
Adjust last commit, making chrono date classes package-scoped
Browse files Browse the repository at this point in the history
Making ChronoDate classes public requires all with/plus/minus mutators to be overridden
  • Loading branch information
jodastephen committed Jun 22, 2012
1 parent e9e9470 commit ad38bab
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 135 deletions.
128 changes: 112 additions & 16 deletions src-standard/main/java/javax/time/chrono/ChronoDate.java
Expand Up @@ -31,6 +31,11 @@
*/
package javax.time.chrono;

import static javax.time.calendrical.LocalDateTimeUnit.DAYS;
import static javax.time.calendrical.LocalDateTimeUnit.MONTHS;
import static javax.time.calendrical.LocalDateTimeUnit.WEEKS;
import static javax.time.calendrical.LocalDateTimeUnit.YEARS;

import javax.time.CalendricalException;
import javax.time.DateTimes;
import javax.time.DayOfWeek;
Expand All @@ -40,7 +45,10 @@
import javax.time.calendrical.DateAdjuster;
import javax.time.calendrical.DateTimeBuilder;
import javax.time.calendrical.DateTimeField;
import javax.time.calendrical.DateTimeObject;
import javax.time.calendrical.LocalDateTimeField;
import javax.time.calendrical.LocalDateTimeUnit;
import javax.time.calendrical.PeriodUnit;

/**
* A date expressed in terms of a calendar system.
Expand Down Expand Up @@ -109,14 +117,18 @@ protected ChronoDate() {

//-----------------------------------------------------------------------
/**
* Gets the value of the specified date field.
* Gets the value of the specified date-time field for the calendar system represented by this date.
* <p>
* This returns the value of the specified field.
* The result of this method will depend on the {@code Chrono}.
* <p>
* This method queries the value of the specified field.
* The field specified is chronology-neutral.
* The same set of fields are used to describe all chronologies.
* Implementations must check and return any fields defined in {@code LocalDateTimeField}
* before delegating on to the method on the specified field.
* Invoking this method must not change the observed state of the target.
*
* @param field the field to query, not null
* @return the value of the field
* @param field the field to get, not null
* @return the value for the field
* @throws CalendricalException if a value for the field cannot be obtained
*/
public abstract long get(DateTimeField field);

Expand Down Expand Up @@ -251,22 +263,29 @@ public boolean isLeapYear() {

//-----------------------------------------------------------------------
/**
* Returns a copy of this date with the specified field altered.
* Returns an object of the same type as this object with the specified field altered.
* <p>
* This method returns a new date based on this date with a new value for the specified field.
* This can be used to change any field, for example to set the year, month of day-of-month.
* The field specified is chronology-neutral.
* The same set of fields are used to describe all chronologies.
* This method returns a new object based on this one with the value for the specified field changed.
* The result of this method will depend on the {@code Chrono}.
* For example, on a {@code GregorianDate}, this can be used to set the year, month of day-of-month.
* The returned object will have the same observable type as this object.
* <p>
* In some cases, changing the specified field can cause the resulting date to become invalid.
* If this occurs, then other fields, typically the day-of-month, will be adjusted to ensure
* that the result is valid. Typically this will select the last valid day of the month.
* In some cases, changing a field is not fully defined. For example, if the target object is
* a date representing the 31st January, then changing the month to February would be unclear.
* In cases like this, the field is responsible for resolving the result. Typically it will choose
* the previous valid date, which would be the last valid day of February in this example.
* <p>
* This instance is immutable and unaffected by this method call.
* Implementations must check and return any fields defined in {@code LocalDateTimeField} before
* delegating on to the method on the specified field.
* If the implementing class is immutable, then this method must return an updated copy of the original.
* If the class is mutable, then this method must update the original.
*
* @param field the field to set in the returned date, not null
* @param newValue the new value of the field in the returned date, not null
* @return a date based on this one with the specified field set, not null
* @return an object of the same type with the specified field set, not null
* @throws CalendricalException if the specified value is invalid
* @throws CalendricalException if the field cannot be set on this type
* @throws RuntimeException if the result exceeds the supported range
*/
public abstract ChronoDate with(DateTimeField field, long newValue);

Expand Down Expand Up @@ -356,6 +375,52 @@ public ChronoDate withDayOfWeek(DayOfWeek dayOfWeek) {
}

//-----------------------------------------------------------------------
/**
* Returns an object of the same type as this object with the specified period added.
* <p>
* This method returns a new object based on this one with the specified period added.
* The result of this method will depend on the {@code Chrono}.
* For example, on a {@code GregorianDate}, this can be used to add a number of years, months or days.
* The returned object will have the same observable type as this object.
* <p>
* In some cases, changing a field is not fully defined. For example, if the target object is
* a date representing the 31st January, then adding one month would be unclear.
* In cases like this, the field is responsible for resolving the result. Typically it will choose
* the previous valid date, which would be the last valid day of February in this example.
* <p>
* Implementations must check and return any fields defined in {@code LocalDateTimeUnit} before
* delegating on to the method on the specified unit.
* If the implementing class is immutable, then this method must return an updated copy of the original.
* If the class is mutable, then this method must update the original.
*
* @param period the amount of the specified unit to add, not null
* @param unit the unit of the period to add, not null
* @return an object of the same type with the specified period added, not null
* @throws CalendricalException if the unit cannot be added to this type
* @throws RuntimeException if the result exceeds the supported range
*/
@Override
public ChronoDate plus(long period, PeriodUnit unit) {
if (unit instanceof LocalDateTimeUnit) {
LocalDateTimeUnit f = (LocalDateTimeUnit) unit;
switch (f) {
case DAYS: return plusDays(period);
case WEEKS: return plusDays(DateTimes.safeMultiply(period, 7));
case MONTHS: return plusMonths(period);
case QUARTER_YEARS: return plusYears(period / 256).plusMonths((period % 256) * 3); // no overflow (256 is multiple of 4)
case HALF_YEARS: return plusYears(period / 256).plusMonths((period % 256) * 6); // no overflow (256 is multiple of 2)
case YEARS: return plusYears(period);
case DECADES: return plusYears(DateTimes.safeMultiply(period, 10));
case CENTURIES: return plusYears(DateTimes.safeMultiply(period, 100));
case MILLENIA: return plusYears(DateTimes.safeMultiply(period, 1000));
// case ERAS: throw new CalendricalException("Unable to add era, standard calendar system only has one era");
// case FOREVER: return (period == 0 ? this : (period > 0 ? LocalDate.MAX_DATE : LocalDate.MIN_DATE));
}
throw new CalendricalException(unit.getName() + " not valid for CopticDate");
}
return unit.add(this, period);
}

/**
* Returns a copy of this date with the specified period in years added.
* <p>
Expand Down Expand Up @@ -421,6 +486,37 @@ public ChronoDate plusWeeks(long weeks) {
public abstract ChronoDate plusDays(long days);

//-----------------------------------------------------------------------
/**
* Returns an object of the same type as this object with the specified period subtracted.
* <p>
* This method returns a new object based on this one with the specified period subtracted.
* The result of this method will depend on the {@code Chrono}.
* For example, on a {@code GregorianDate}, this can be used to subtract a number of years, months or days.
* The returned object will have the same observable type as this object.
* <p>
* In some cases, changing a field is not fully defined. For example, if the target object is
* a date representing the 31st March, then subtracting one month would be unclear.
* In cases like this, the field is responsible for resolving the result. Typically it will choose
* the previous valid date, which would be the last valid day of February in this example.
* <p>
* Implementations must check and return any fields defined in {@code LocalDateTimeUnit} before
* delegating on to the method on the specified unit.
* If the implementing class is immutable, then this method must return an updated copy of the original.
* If the class is mutable, then this method must update the original.
* <p>
* The default implementation uses {@link #plus(long, PeriodUnit)}.
*
* @param period the amount of the specified unit to subtract, not null
* @param unit the unit of the period to subtract, not null
* @return an object of the same type with the specified period subtracted, not null
* @throws CalendricalException if the unit cannot be subtracted to this type
* @throws RuntimeException if the result exceeds the supported range
*/
@Override
public ChronoDate minus(long period, PeriodUnit unit) {
return plus(DateTimes.safeNegate(period), unit);
}

/**
* Returns a copy of this date with the specified period in years subtracted.
* <p>
Expand Down
14 changes: 5 additions & 9 deletions src-standard/main/java/javax/time/chrono/CopticChrono.java
Expand Up @@ -94,36 +94,32 @@ public String getName() {

//-----------------------------------------------------------------------
@Override
public CopticDate date(Era era, int yearOfEra, int monthOfYear, int dayOfMonth) {
public ChronoDate date(Era era, int yearOfEra, int monthOfYear, int dayOfMonth) {
if (era instanceof CopticEra) {
throw new CalendricalException("Era must be a CopticEra");
}
return date(prolepticYear((CopticEra) era, yearOfEra), monthOfYear, dayOfMonth);
}

@Override
public CopticDate date(int prolepticYear, int monthOfYear, int dayOfMonth) {
public ChronoDate date(int prolepticYear, int monthOfYear, int dayOfMonth) {
return new CopticDate(prolepticYear, monthOfYear, dayOfMonth);
}

@Override
public CopticDate date(CalendricalObject calendrical) {
public ChronoDate date(CalendricalObject calendrical) {
if (calendrical instanceof CopticDate) {
return (CopticDate) calendrical;
}
return dateFromEpochDay(LocalDate.from(calendrical).toEpochDay());
}

@Override
public CopticDate dateFromEpochDay(long epochDay) {
public ChronoDate dateFromEpochDay(long epochDay) {
return CopticDate.ofEpochDay(epochDay);
}

@Override
public CopticDate now() {
return (CopticDate) super.now();
}

//-----------------------------------------------------------------------
/**
* Checks if the specified year is a leap year.
* <p>
Expand Down
31 changes: 1 addition & 30 deletions src-standard/main/java/javax/time/chrono/CopticDate.java
Expand Up @@ -37,8 +37,6 @@
import javax.time.DateTimes;
import javax.time.calendrical.DateTimeField;
import javax.time.calendrical.LocalDateTimeField;
import javax.time.calendrical.LocalDateTimeUnit;
import javax.time.calendrical.PeriodUnit;

/**
* A date in the Coptic calendar system.
Expand All @@ -48,7 +46,7 @@
* <h4>Implementation notes</h4>
* This class is immutable and thread-safe.
*/
public final class CopticDate extends ChronoDate implements Comparable<ChronoDate>, Serializable {
final class CopticDate extends ChronoDate implements Comparable<ChronoDate>, Serializable {
// this class is package-scoped so that future conversion to public
// would not change serialization

Expand Down Expand Up @@ -171,33 +169,6 @@ public CopticDate with(DateTimeField field, long newValue) {
return field.set(this, newValue);
}

@Override
public CopticDate minus(long period, PeriodUnit unit) {
return plus(DateTimes.safeNegate(period), unit);
}

@Override
public CopticDate plus(long period, PeriodUnit unit) {
if (unit instanceof LocalDateTimeUnit) {
LocalDateTimeUnit f = (LocalDateTimeUnit) unit;
switch (f) {
case DAYS: return plusDays(period);
case WEEKS: return plusDays(DateTimes.safeMultiply(period, 7));
case MONTHS: return plusMonths(period);
case QUARTER_YEARS: return plusYears(period / 256).plusMonths((period % 256) * 3); // no overflow (256 is multiple of 4)
case HALF_YEARS: return plusYears(period / 256).plusMonths((period % 256) * 6); // no overflow (256 is multiple of 2)
case YEARS: return plusYears(period);
case DECADES: return plusYears(DateTimes.safeMultiply(period, 10));
case CENTURIES: return plusYears(DateTimes.safeMultiply(period, 100));
case MILLENIA: return plusYears(DateTimes.safeMultiply(period, 1000));
// case ERAS: throw new CalendricalException("Unable to add era, standard calendar system only has one era");
// case FOREVER: return (period == 0 ? this : (period > 0 ? LocalDate.MAX_DATE : LocalDate.MIN_DATE));
}
throw new CalendricalException(unit.getName() + " not valid for CopticDate");
}
return unit.add(this, period);
}

//-----------------------------------------------------------------------
@Override
public CopticDate plusYears(long years) {
Expand Down
14 changes: 5 additions & 9 deletions src-standard/main/java/javax/time/chrono/ISOChrono.java
Expand Up @@ -98,36 +98,32 @@ public String getName() {

//-----------------------------------------------------------------------
@Override
public ISODate date(Era era, int yearOfEra, int monthOfYear, int dayOfMonth) {
public ChronoDate date(Era era, int yearOfEra, int monthOfYear, int dayOfMonth) {
if (era instanceof ISOEra) {
throw new CalendricalException("Era must be a ISOEra");
}
return date(prolepticYear((ISOEra) era, yearOfEra), monthOfYear, dayOfMonth);
}

@Override
public ISODate date(int prolepticYear, int monthOfYear, int dayOfMonth) {
public ChronoDate date(int prolepticYear, int monthOfYear, int dayOfMonth) {
return new ISODate(LocalDate.of(prolepticYear, monthOfYear, dayOfMonth));
}

@Override
public ISODate date(CalendricalObject calendrical) {
public ChronoDate date(CalendricalObject calendrical) {
if (calendrical instanceof ISODate) {
return (ISODate) calendrical;
}
return new ISODate(LocalDate.from(calendrical));
}

@Override
public ISODate dateFromEpochDay(long epochDay) {
public ChronoDate dateFromEpochDay(long epochDay) {
return new ISODate(LocalDate.ofEpochDay(epochDay));
}

@Override
public ISODate now() {
return (ISODate) super.now();
}

//-----------------------------------------------------------------------
/**
* Checks if the specified year is a leap year.
* <p>
Expand Down
32 changes: 1 addition & 31 deletions src-standard/main/java/javax/time/chrono/ISODate.java
Expand Up @@ -34,12 +34,9 @@
import java.io.Serializable;

import javax.time.CalendricalException;
import javax.time.DateTimes;
import javax.time.LocalDate;
import javax.time.calendrical.DateTimeField;
import javax.time.calendrical.LocalDateTimeField;
import javax.time.calendrical.LocalDateTimeUnit;
import javax.time.calendrical.PeriodUnit;

/**
* A date in the ISO calendar system.
Expand All @@ -49,7 +46,7 @@
* <h4>Implementation notes</h4>
* This class is immutable and thread-safe.
*/
public final class ISODate extends ChronoDate implements Comparable<ChronoDate>, Serializable {
final class ISODate extends ChronoDate implements Comparable<ChronoDate>, Serializable {
// this class is package-scoped so that future conversion to public
// would not change serialization

Expand Down Expand Up @@ -118,33 +115,6 @@ public ISODate with(DateTimeField field, long newValue) {
return field.set(this, newValue);
}

@Override
public ISODate minus(long period, PeriodUnit unit) {
return plus(DateTimes.safeNegate(period), unit);
}

@Override
public ISODate plus(long period, PeriodUnit unit) {
if (unit instanceof LocalDateTimeUnit) {
LocalDateTimeUnit f = (LocalDateTimeUnit) unit;
switch (f) {
case DAYS: return plusDays(period);
case WEEKS: return plusDays(DateTimes.safeMultiply(period, 7));
case MONTHS: return plusMonths(period);
case QUARTER_YEARS: return plusYears(period / 256).plusMonths((period % 256) * 3); // no overflow (256 is multiple of 4)
case HALF_YEARS: return plusYears(period / 256).plusMonths((period % 256) * 6); // no overflow (256 is multiple of 2)
case YEARS: return plusYears(period);
case DECADES: return plusYears(DateTimes.safeMultiply(period, 10));
case CENTURIES: return plusYears(DateTimes.safeMultiply(period, 100));
case MILLENIA: return plusYears(DateTimes.safeMultiply(period, 1000));
// case ERAS: throw new CalendricalException("Unable to add era, standard calendar system only has one era");
// case FOREVER: return (period == 0 ? this : (period > 0 ? LocalDate.MAX_DATE : LocalDate.MIN_DATE));
}
throw new CalendricalException(unit.getName() + " not valid for CopticDate");
}
return unit.add(this, period);
}

//-----------------------------------------------------------------------
@Override
public ISODate plusYears(long years) {
Expand Down

0 comments on commit ad38bab

Please sign in to comment.