changeset 7473:3dbe3cd949dd

Add TemporalAdjuster.ofDateAdjuster() Simplifies user-written adjusters
author scolebourne
date Mon, 01 Apr 2013 23:03:41 +0100
parents a98a9eb86c54
children ca671c2e333f
files src/share/classes/java/time/temporal/TemporalAdjuster.java src/share/classes/java/time/temporal/TemporalAdjusters.java test/java/time/tck/java/time/temporal/TCKTemporalAdjusters.java
diffstat 3 files changed, 68 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/time/temporal/TemporalAdjuster.java	Mon Apr 01 22:48:29 2013 +0100
+++ b/src/share/classes/java/time/temporal/TemporalAdjuster.java	Mon Apr 01 23:03:41 2013 +0100
@@ -63,6 +63,8 @@
 
 import java.time.DateTimeException;
 import java.time.DayOfWeek;
+import java.time.LocalDate;
+import java.util.function.UnaryOperator;
 
 /**
  * Strategy for adjusting a temporal object.
@@ -152,6 +154,28 @@
 
     //-----------------------------------------------------------------------
     /**
+     * Obtains a {@code TemporalAdjuster} that wraps a date adjuster.
+     * <p>
+     * The {@code TemporalAdjuster} is based on the low level {@code Temporal} interface.
+     * This method allows an adjustment from {@code LocalDate} to {@code LocalDate}
+     * to be wrapped to match the temporal-based interface.
+     * This is provided for convenience to make user-written adjusters simpler.
+     * <p>
+     * In general, user-written adjusters should be static constants:
+     * <pre>
+     *  public static TemporalAdjuster TWO_DAYS_LATER = TemporalAdjuster.ofDateAdjuster(
+     *    date -> date.plusDays(2));
+     * </pre>
+     *
+     * @param dateBasedAdjuster  the date-based adjuster, not null
+     * @return the temporal adjuster wrapping on the date adjuster, not null
+     */
+    public static TemporalAdjuster ofDateAdjuster(UnaryOperator<LocalDate> dateBasedAdjuster) {
+        return TemporalAdjusters.ofDateAdjuster(dateBasedAdjuster);
+    }
+
+    //-----------------------------------------------------------------------
+    /**
      * Returns the "first day of month" adjuster, which returns a new date set to
      * the first day of the current month.
      * <p>
--- a/src/share/classes/java/time/temporal/TemporalAdjusters.java	Mon Apr 01 22:48:29 2013 +0100
+++ b/src/share/classes/java/time/temporal/TemporalAdjusters.java	Mon Apr 01 23:03:41 2013 +0100
@@ -69,7 +69,9 @@
 import static java.time.temporal.ChronoUnit.YEARS;
 
 import java.time.DayOfWeek;
+import java.time.LocalDate;
 import java.util.Objects;
+import java.util.function.UnaryOperator;
 
 /**
  * Implementations of the static methods in {@code TemporalAdjuster}
@@ -84,6 +86,33 @@
 
     //-----------------------------------------------------------------------
     /**
+     * Obtains a {@code TemporalAdjuster} that wraps a date adjuster.
+     * <p>
+     * The {@code TemporalAdjuster} is based on the low level {@code Temporal} interface.
+     * This method allows an adjustment from {@code LocalDate} to {@code LocalDate}
+     * to be wrapped to match the temporal-based interface.
+     * This is provided for convenience to make user-written adjusters simpler.
+     * <p>
+     * In general, user-written adjusters should be static constants:
+     * <pre>
+     *  public static TemporalAdjuster TWO_DAYS_LATER = TemporalAdjuster.ofDateAdjuster(
+     *    date -> date.plusDays(2));
+     * </pre>
+     *
+     * @param dateBasedAdjuster  the date-based adjuster, not null
+     * @return the temporal adjuster wrapping on the date adjuster, not null
+     */
+    static TemporalAdjuster ofDateAdjuster(UnaryOperator<LocalDate> dateBasedAdjuster) {
+        Objects.requireNonNull(dateBasedAdjuster, "dateBasedAdjuster");
+        return (temporal) -> {
+            LocalDate input = LocalDate.from(temporal);
+            LocalDate output = dateBasedAdjuster.apply(input);
+            return temporal.with(output);
+        };
+    }
+
+    //-----------------------------------------------------------------------
+    /**
      * Returns the "first day of month" adjuster, which returns a new date set to
      * the first day of the current month.
      * <p>
--- a/test/java/time/tck/java/time/temporal/TCKTemporalAdjusters.java	Mon Apr 01 22:48:29 2013 +0100
+++ b/test/java/time/tck/java/time/temporal/TCKTemporalAdjusters.java	Mon Apr 01 23:03:41 2013 +0100
@@ -84,6 +84,21 @@
 public class TCKTemporalAdjusters {
 
     //-----------------------------------------------------------------------
+    // ofDateAdjuster()
+    //-----------------------------------------------------------------------
+    @Test
+    public void factory_ofDateAdjuster() {
+        TemporalAdjuster test = TemporalAdjuster.ofDateAdjuster(date -> date.plusDays(2));
+        assertEquals(LocalDate.of(2012, 6, 30).with(test), LocalDate.of(2012, 7, 2));
+    }
+
+    @Test(expectedExceptions = NullPointerException.class)
+    public void factory_ofDateAdjuster_null() {
+        TemporalAdjuster.ofDateAdjuster(null);
+    }
+
+
+    //-----------------------------------------------------------------------
     // firstDayOfMonth()
     //-----------------------------------------------------------------------
     @Test