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

Commit

Permalink
DayOfWeek as a Calendrical
Browse files Browse the repository at this point in the history
git-svn-id: https://threeten.svn.sourceforge.net/svnroot/threeten/trunk/threeten@1496 291d795c-afe8-5c46-8ee5-bf9dd72e1864
  • Loading branch information
jodastephen committed Jun 9, 2011
1 parent 6eab4ad commit 0acbe4f
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 31 deletions.
71 changes: 49 additions & 22 deletions src/main/java/javax/time/calendar/DayOfWeek.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
* @author Michael Nascimento Santos
* @author Stephen Colebourne
*/
public enum DayOfWeek {
public enum DayOfWeek implements Calendrical {

/**
* The singleton instance for the day-of-week of Monday.
Expand Down Expand Up @@ -112,8 +112,7 @@ public enum DayOfWeek {
* This factory allows the enum to be obtained from the {@code int} value.
* The {@code int} value follows the ISO-8601 standard, from 1 (Monday) to 7 (Sunday).
* <p>
* An exception is thrown if the value is invalid. The exception uses the
* {@link ISOChronology} day-of-week rule to indicate the failed rule.
* An exception is thrown if the value is invalid.
*
* @param dayOfWeek the day-of-week to represent, from 1 (Monday) to 7 (Sunday)
* @return the DayOfWeek singleton, not null
Expand All @@ -126,6 +125,24 @@ public static DayOfWeek of(int dayOfWeek) {
return ENUMS[dayOfWeek - 1];
}

/**
* Obtains an instance of {@code DayOfWeek} from a {@code Calendrical}.
* <p>
* {@code DayOfWeek} is an enum representing the 7 days of the week.
* This factory allows the enum to be obtained from a {@code Calendrical},
* for example {@code LocalDate} or {@code DateTimeField}.
* <p>
* An exception is thrown if the day-of-week cannot be obtained.
*
* @param calendrical the calendrical to get the day-of-week from, not null
* @return the DayOfWeek singleton, not null
* @throws IllegalCalendarFieldValueException if the day-of-week is invalid
*/
public static DayOfWeek of(Calendrical calendrical) {
DateTimeField field = DAY_OF_WEEK.getValueChecked(calendrical);
return of(field.getValidIntValue());
}

//-----------------------------------------------------------------------
/**
* Gets the day-of-week {@code int} value.
Expand All @@ -139,30 +156,40 @@ public int getValue() {
return ordinal() + 1;
}

// /**
// * Gets the value of the specified calendrical rule.
// * <p>
// * This returns the one of the day-of-week values if the type of the rule
// * is {@code DayOfWeek}. Other rules will return {@code null}.
// *
// * @param rule the rule to use, not null
// * @return the value for the rule, null if the value cannot be returned
// */
// public <T> T get(CalendricalRule<T> rule) {
// if (rule.getReifiedType() != DayOfWeek.class) {
// return null;
// }
// return rule.reify(this);
// }
//-----------------------------------------------------------------------
/**
* Gets the value of the specified calendrical rule.
* <p>
* This will only return a value for the {@link ISODateTimeRule#DAY_OF_WEEK}
* rule, or something derivable from it.
*
* @param rule the rule to use, not null
* @return the value for the rule, null if the value cannot be returned
*/
public <T> T get(CalendricalRule<T> rule) {
if (rule instanceof DateTimeRule) {
return toField().get(rule);
}
return null;
}

/**
* Converts this day-of-week to an equivalent field.
* <p>
* The field is based on {@link ISODateTimeRule#DAY_OF_WEEK}.
*
* @return the equivalent day-of-week field, not null
*/
public DateTimeField toField() {
return DAY_OF_WEEK.field(getValue());
}

//-----------------------------------------------------------------------
/**
* Gets the textual representation, such as 'Mon' or 'Friday'.
* <p>
* This method is notionally specific to {@link ISOChronology} as it uses
* the day-of-week rule to obtain the text. However, it is expected that
* the text will be equivalent for all day-of-week rules, thus this aspect
* of the implementation should be irrelevant to applications.
* This enum uses the {@link ISODateTimeRule#DAY_OF_WEEK} rule to obtain the text.
* This allows the text to be localized by language, but not by chronology.
* <p>
* If no textual mapping is found then the {@link #getValue() numeric value} is returned.
*
Expand Down
39 changes: 30 additions & 9 deletions src/test/java/javax/time/calendar/TestDayOfWeek.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
*/
package javax.time.calendar;

import static javax.time.calendar.ISODateTimeRule.DAY_OF_WEEK;
import static javax.time.calendar.ISODateTimeRule.MONTH_OF_YEAR;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertSame;
import static org.testng.Assert.assertTrue;
Expand Down Expand Up @@ -82,15 +84,34 @@ public void test_factory_int_valueTooHigh() {
DayOfWeek.of(8);
}

// //-----------------------------------------------------------------------
// // get()
// //-----------------------------------------------------------------------
// public void test_get() {
// assertEquals(DayOfWeek.MONDAY.get(ISODateTimeRule.DAY_OF_WEEK), DayOfWeek.MONDAY);
// assertEquals(DayOfWeek.THURSDAY.get(ISODateTimeRule.DAY_OF_WEEK), DayOfWeek.THURSDAY);
//
// assertEquals(DayOfWeek.MONDAY.get(ISODateTimeRule.MONTH_OF_YEAR), null);
// }
//-----------------------------------------------------------------------
public void test_factory_Calendrical() {
assertEquals(DayOfWeek.of(LocalDate.of(2011, 6, 6)), DayOfWeek.MONDAY);
assertEquals(DayOfWeek.of(DAY_OF_WEEK.field(4)), DayOfWeek.THURSDAY);
}

@Test(expectedExceptions=CalendricalRuleException.class)
public void test_factory_Calendrical_invalid() {
DayOfWeek.of(LocalTime.of(12, 30));
}

//-----------------------------------------------------------------------
// get()
//-----------------------------------------------------------------------
public void test_get() {
assertEquals(DayOfWeek.MONDAY.get(DAY_OF_WEEK), DAY_OF_WEEK.field(1));
assertEquals(DayOfWeek.THURSDAY.get(DAY_OF_WEEK), DAY_OF_WEEK.field(4));

assertEquals(DayOfWeek.MONDAY.get(MONTH_OF_YEAR), null);
}

//-----------------------------------------------------------------------
// toField()
//-----------------------------------------------------------------------
public void test_toField() {
assertEquals(DayOfWeek.MONDAY.toField(), DAY_OF_WEEK.field(1));
assertEquals(DayOfWeek.THURSDAY.toField(), DAY_OF_WEEK.field(4));
}

//-----------------------------------------------------------------------
// getText()
Expand Down

0 comments on commit 0acbe4f

Please sign in to comment.