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

Commit

Permalink
Add safe adjuster
Browse files Browse the repository at this point in the history
This seems too specialized for the JDK, although the day-of-month case could still be useful
  • Loading branch information
jodastephen committed Oct 31, 2012
1 parent 2bc1177 commit 04b9ebd
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/main/java/javax/time/calendrical/DateTimeAdjusters.java
Expand Up @@ -41,6 +41,7 @@
import java.io.Serializable;
import java.util.Objects;

import javax.time.DateTimeException;
import javax.time.DayOfWeek;
import javax.time.calendrical.DateTime.WithAdjuster;

Expand Down Expand Up @@ -433,4 +434,60 @@ public int hashCode() {
}
}

/**
* Returns the safe day-of-month adjuster, which adjusts the day-of-month.
* <p>
* The ISO calendar system behaves as follows:<br />
* The input 2011-01-15 (a Saturday) for parameter (MONDAY) will return 2011-01-10 (five days earlier).<br />
* The input 2011-01-15 (a Saturday) for parameter (SATURDAY) will return 2011-01-15 (same as input).
*
* @param field the field to set in the returned date-time, not null
* @param newValue the new value of the field in the returned date-time, not null
* @return the safe adjuster, not null
*/
public static WithAdjuster safe(DateTimeField field, long newValue) {
return new Safe(field, newValue);
}

/**
* Implementation of safe setting.
*/
private static final class Safe implements WithAdjuster, Serializable {
/** Serialization version. */
private static final long serialVersionUID = 1L;
/** The day-of-month. */
private final DateTimeField field;
/** The day-of-month. */
private final long newValue;

private Safe(DateTimeField field, long newValue) {
this.field = field;
this.newValue = newValue;
}

@Override
public DateTime doWithAdjustment(DateTime dateTime) {
DateTimeValueRange range = dateTime.range(field);
if (range.isFixed() == false) {
throw new DateTimeException("Range of valid values is not fully defined");
}
long value = Math.min(Math.max(newValue, range.getMinimum()), range.getMaximum());
return dateTime.with(field, value);
}

@Override
public boolean equals(Object obj) {
if (obj instanceof Safe) {
Safe other = (Safe) obj;
return newValue == other.newValue;
}
return false;
}

@Override
public int hashCode() {
return field.hashCode() ^ ((int) (newValue ^ (newValue >>> 32)));
}
}

}

0 comments on commit 04b9ebd

Please sign in to comment.