001/*
002 * Copyright (c) 2007-2013, Stephen Colebourne & Michael Nascimento Santos
003 *
004 * All rights reserved.
005 *
006 * Redistribution and use in source and binary forms, with or without
007 * modification, are permitted provided that the following conditions are met:
008 *
009 *  * Redistributions of source code must retain the above copyright notice,
010 *    this list of conditions and the following disclaimer.
011 *
012 *  * Redistributions in binary form must reproduce the above copyright notice,
013 *    this list of conditions and the following disclaimer in the documentation
014 *    and/or other materials provided with the distribution.
015 *
016 *  * Neither the name of JSR-310 nor the names of its contributors
017 *    may be used to endorse or promote products derived from this software
018 *    without specific prior written permission.
019 *
020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031 */
032package org.threeten.bp.temporal;
033
034import org.threeten.bp.DateTimeException;
035
036/**
037 * Strategy for adjusting a temporal object.
038 * <p>
039 * Adjusters are a key tool for modifying temporal objects.
040 * They exist to externalize the process of adjustment, permitting different
041 * approaches, as per the strategy design pattern.
042 * Examples might be an adjuster that sets the date avoiding weekends, or one that
043 * sets the date to the last day of the month.
044 * <p>
045 * There are two equivalent ways of using a {@code TemporalAdjuster}.
046 * The first is to invoke the method on this interface directly.
047 * The second is to use {@link Temporal#with(TemporalAdjuster)}:
048 * <pre>
049 *   // these two lines are equivalent, but the second approach is recommended
050 *   temporal = thisAdjuster.adjustInto(temporal);
051 *   temporal = temporal.with(thisAdjuster);
052 * </pre>
053 * It is recommended to use the second approach, {@code with(TemporalAdjuster)},
054 * as it is a lot clearer to read in code.
055 * <p>
056 * See {@link TemporalAdjusters} for a standard set of adjusters, including finding the
057 * last day of the month.
058 * Adjusters may also be defined by applications.
059 *
060 * <h3>Specification for implementors</h3>
061 * This interface places no restrictions on the mutability of implementations,
062 * however immutability is strongly recommended.
063 */
064public interface TemporalAdjuster {
065
066    /**
067     * Adjusts the specified temporal object.
068     * <p>
069     * This adjusts the specified temporal object using the logic
070     * encapsulated in the implementing class.
071     * Examples might be an adjuster that sets the date avoiding weekends, or one that
072     * sets the date to the last day of the month.
073     * <p>
074     * There are two equivalent ways of using this method.
075     * The first is to invoke this method directly.
076     * The second is to use {@link Temporal#with(TemporalAdjuster)}:
077     * <pre>
078     *   // these two lines are equivalent, but the second approach is recommended
079     *   temporal = thisAdjuster.adjustInto(temporal);
080     *   temporal = temporal.with(thisAdjuster);
081     * </pre>
082     * It is recommended to use the second approach, {@code with(TemporalAdjuster)},
083     * as it is a lot clearer to read in code.
084     *
085     * <h3>Specification for implementors</h3>
086     * The implementation must take the input object and adjust it.
087     * The implementation defines the logic of the adjustment and is responsible for
088     * documenting that logic. It may use any method on {@code Temporal} to
089     * query the temporal object and perform the adjustment.
090     * The returned object must have the same observable type as the input object
091     * <p>
092     * The input object must not be altered.
093     * Instead, an adjusted copy of the original must be returned.
094     * This provides equivalent, safe behavior for immutable and mutable temporal objects.
095     * <p>
096     * The input temporal object may be in a calendar system other than ISO.
097     * Implementations may choose to document compatibility with other calendar systems,
098     * or reject non-ISO temporal objects by {@link TemporalQueries#chrono() querying the chronology}.
099     * <p>
100     * This method may be called from multiple threads in parallel.
101     * It must be thread-safe when invoked.
102     *
103     * @param temporal  the temporal object to adjust, not null
104     * @return an object of the same observable type with the adjustment made, not null
105     * @throws DateTimeException if unable to make the adjustment
106     * @throws ArithmeticException if numeric overflow occurs
107     */
108    Temporal adjustInto(Temporal temporal);
109
110}