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

Commit

Permalink
Prototype extract method in rule
Browse files Browse the repository at this point in the history
  • Loading branch information
jodastephen committed Jan 30, 2012
1 parent 56cab54 commit 5dd3eae
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/main/java/javax/time/ExtendedCalendricalRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@
*/
package javax.time;

import static javax.time.calendrical.ISODateTimeRule.ZERO_EPOCH_MONTH;

import java.io.Serializable;

import javax.time.calendrical.CalendricalEngine;
import javax.time.calendrical.CalendricalRule;
import javax.time.calendrical.DateTimeField;
import javax.time.calendrical.ISOChronology;
import javax.time.calendrical.ISODateTimeRule;

/**
Expand Down Expand Up @@ -88,6 +91,39 @@ private Object readResolve() {
return RULE_CACHE[ordinal];
}

//-----------------------------------------------------------------------
@SuppressWarnings("unchecked")
@Override
public <R> R extract(CalendricalRule<R> ruleToExtract, T calendrical) {
if (ruleToExtract == this) {
return (R) calendrical;
}
switch (ordinal) {
case YEAR_ORDINAL:
Year y = (Year) calendrical;
return CalendricalEngine.derive(ruleToExtract, this, ISOChronology.INSTANCE, ISODateTimeRule.YEAR.field(y.getValue()));
case YEAR_MONTH_ORDINAL:
YearMonth ym = (YearMonth) calendrical;
return CalendricalEngine.derive(ruleToExtract, this, ISOChronology.INSTANCE, ZERO_EPOCH_MONTH.field(ym.toZeroEpochMonth()));
case MONTH_DAY_ORDINAL:
MonthDay md = (MonthDay) calendrical;
return CalendricalEngine.derive(ruleToExtract, this, null, null, null, null, ISOChronology.INSTANCE, md.toFields());
case MONTH_OF_YEAR_ORDINAL:
MonthOfYear moy = (MonthOfYear) calendrical;
return CalendricalEngine.derive(ruleToExtract, this, ISOChronology.INSTANCE, ISODateTimeRule.MONTH_OF_YEAR.field(moy.getValue()));
case QUARTER_OF_YEAR_ORDINAL:
QuarterOfYear qoy = (QuarterOfYear) calendrical;
return CalendricalEngine.derive(ruleToExtract, this, ISOChronology.INSTANCE, ISODateTimeRule.QUARTER_OF_YEAR.field(qoy.getValue()));
case DAY_OF_WEEK_ORDINAL:
DayOfWeek dow = (DayOfWeek) calendrical;
return CalendricalEngine.derive(ruleToExtract, this, ISOChronology.INSTANCE, ISODateTimeRule.DAY_OF_WEEK.field(dow.getValue()));
case AM_PM_OF_DAY_ORDINAL:
AmPmOfDay amPm = (AmPmOfDay) calendrical;
return CalendricalEngine.derive(ruleToExtract, this, ISOChronology.INSTANCE, ISODateTimeRule.AMPM_OF_DAY.field(amPm.getValue()));
}
return null;
}

//-----------------------------------------------------------------------
@SuppressWarnings("unchecked")
@Override
Expand Down
37 changes: 36 additions & 1 deletion src/main/java/javax/time/ISOCalendricalRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import javax.time.calendrical.CalendricalEngine;
import javax.time.calendrical.CalendricalRule;
import javax.time.calendrical.DateTimeField;
import javax.time.calendrical.ISOChronology;

/**
* Internal class supplying the rules for the principal date and time objects.
Expand Down Expand Up @@ -94,8 +95,42 @@ private Object readResolve() {
//-----------------------------------------------------------------------
@SuppressWarnings("unchecked")
@Override
protected T deriveFrom(CalendricalEngine engine) {
public <R> R extract(CalendricalRule<R> ruleToExtract, T calendrical) {
if (ruleToExtract == this) {
return (R) calendrical;
}
switch (ordinal) {
case LOCAL_DATE_ORDINAL:
return CalendricalEngine.derive(ruleToExtract, this, (LocalDate) calendrical, null, null, null, ISOChronology.INSTANCE, null);
case LOCAL_TIME_ORDINAL:
return CalendricalEngine.derive(ruleToExtract, this, null, (LocalTime) calendrical, null, null, ISOChronology.INSTANCE, null);
case LOCAL_DATE_TIME_ORDINAL:
LocalDateTime ldt = (LocalDateTime) calendrical;
return CalendricalEngine.derive(ruleToExtract, this, ldt.toLocalDate(), ldt.toLocalTime(), null, null, ISOChronology.INSTANCE, null);
case OFFSET_DATE_ORDINAL:
OffsetDate od = (OffsetDate) calendrical;
return CalendricalEngine.derive(ruleToExtract, this, od.toLocalDate(), null, od.getOffset(), null, ISOChronology.INSTANCE, null);
case OFFSET_TIME_ORDINAL:
OffsetTime ot = (OffsetTime) calendrical;
return CalendricalEngine.derive(ruleToExtract, this, null, ot.toLocalTime(), ot.getOffset(), null, ISOChronology.INSTANCE, null);
case OFFSET_DATE_TIME_ORDINAL:
OffsetDateTime odt = (OffsetDateTime) calendrical;
return CalendricalEngine.derive(ruleToExtract, this, odt.toLocalDate(), odt.toLocalTime(), odt.getOffset(), null, ISOChronology.INSTANCE, null);
case ZONED_DATE_TIME_ORDINAL:
ZonedDateTime zdt = (ZonedDateTime) calendrical;
return CalendricalEngine.derive(ruleToExtract, this, zdt.toLocalDate(), zdt.toLocalTime(), zdt.getOffset(), zdt.getZone(), ISOChronology.INSTANCE, null);
case ZONE_OFFSET_ORDINAL:
return CalendricalEngine.derive(ruleToExtract, this, null, null, (ZoneOffset) calendrical, null, ISOChronology.INSTANCE, null);
case ZONE_ID_ORDINAL:
return CalendricalEngine.derive(ruleToExtract, this, null, null, null, (ZoneId) calendrical, ISOChronology.INSTANCE, null);
}
return null;
}

@SuppressWarnings("unchecked")
@Override
protected T deriveFrom(CalendricalEngine engine) {
switch (ordinal) {
case LOCAL_DATE_ORDINAL: return (T) engine.getDate(true);
case LOCAL_TIME_ORDINAL: {
LocalTime time = engine.getTime(false);
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/javax/time/YearMonth.java
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,18 @@ public LocalDate atDay(int dayOfMonth, DateResolver dateResolver) {
}

//-----------------------------------------------------------------------
/**
* Converts this {@code YearMonth} to a count of months.
* <p>
* The Zero Epoch Month count is a simple incrementing count of months
* where month 0 is 0000-01-01 using ISO proleptic years.
*
* @return the count of months
*/
public long toZeroEpochMonth() {
return year * 12L + month.ordinal();
}

/**
* Converts this year-month to an equivalent fields object.
* <p>
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/javax/time/calendrical/CalendricalRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.List;

import javax.time.CalendricalException;
import javax.time.LocalDate;

/**
* A rule defining how a single well-defined calendrical element operates.
Expand Down Expand Up @@ -127,6 +128,21 @@ public final Class<T> getType() {
return type;
}

//-----------------------------------------------------------------------
/**
* Extracts the value of the specified rule from an instance of the class
* this rule manages.
*
* @param ruleToExtract the rule to extract, not null
* @param calendrical the calendrical to get the field value from, not null
* @return the value of the specified rule, null if unable to extract the field
*/
public <R> R extract(CalendricalRule<R> ruleToExtract, T calendrical) {
ISOChronology.checkNotNull(ruleToExtract, "CalendricalRule must not be null");
ISOChronology.checkNotNull(calendrical, "Calendrical must not be null");
return (R) ((Calendrical) calendrical).get(ruleToExtract);
}

//-----------------------------------------------------------------------
/**
* Gets the value of this rule from the specified calendrical returning
Expand Down Expand Up @@ -255,4 +271,38 @@ public String toString() {
return name;
}

//-------------------------------------------------------------------------
/**
* Rule class.
*
*/
static class LocalDateRule extends CalendricalRule<LocalDate> implements Serializable {
static final CalendricalRule<LocalDate> INSTANCE = new LocalDateRule();
private static final long serialVersionUID = 1L;

private LocalDateRule() {
super(LocalDate.class, "LocalDate");
}
private Object readResolve() {
return INSTANCE;
}

@SuppressWarnings("unchecked")
@Override
public <R> R extract(CalendricalRule<R> ruleToExtract, LocalDate calendrical) {
if (ruleToExtract == this) {
return (R) calendrical;
}
if (ruleToExtract instanceof ISODateTimeRule) {
return (R) ((ISODateTimeRule) ruleToExtract).deriveFrom(calendrical, null, null);
}
return CalendricalEngine.derive(ruleToExtract, INSTANCE, calendrical, null, null, null, ISOChronology.INSTANCE, null);
}

@Override
protected LocalDate deriveFrom(CalendricalEngine engine) {
return engine.getDate(true);
}
}

}
12 changes: 12 additions & 0 deletions src/test/java/javax/time/TestYearMonth.java
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,18 @@ public void test_atDay_intDateResolver_resolved() {
assertEquals(test.atDay(31, DateResolvers.previousValid()), LocalDate.of(2008, 6, 30));
}

//-----------------------------------------------------------------------
// toZeroEpochMonth()
//-----------------------------------------------------------------------
public void test_toZeroEpochMonth() {
assertEquals(YearMonth.of(0, 1).toZeroEpochMonth(), 0);
assertEquals(YearMonth.of(0, 2).toZeroEpochMonth(), 1);
assertEquals(YearMonth.of(0, 12).toZeroEpochMonth(), 11);
assertEquals(YearMonth.of(1, 1).toZeroEpochMonth(), 12);
assertEquals(YearMonth.of(-1, 12).toZeroEpochMonth(), -1);
assertEquals(YearMonth.of(2010, 6).toZeroEpochMonth(), 2010 * 12L + 5);
}

//-----------------------------------------------------------------------
// toFields()
//-----------------------------------------------------------------------
Expand Down

0 comments on commit 5dd3eae

Please sign in to comment.