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;
035import org.threeten.bp.Duration;
036import org.threeten.bp.Period;
037
038/**
039 * Strategy for subtracting from a temporal object.
040 * <p>
041 * Subtractors are a key tool for modifying temporal objects.
042 * They exist to externalize the process of subtraction, permitting different
043 * approaches, as per the strategy design pattern.
044 * <p>
045 * There are two equivalent ways of using a {@code TemporalSubtractor}.
046 * The first is to invoke the method on this interface directly.
047 * The second is to use {@link Temporal#minus(TemporalSubtractor)}:
048 * <pre>
049 *   // these two lines are equivalent, but the second approach is recommended
050 *   dateTime = subtractor.subtractFrom(dateTime);
051 *   dateTime = dateTime.minus(subtractor);
052 * </pre>
053 * It is recommended to use the second approach, {@code minus(TemporalSubtractor)},
054 * as it is a lot clearer to read in code.
055 * <p>
056 * The {@link Period} and {@link Duration} classes implement this interface.
057 * Subtractors may also be defined by applications.
058 *
059 * <h3>Specification for implementors</h3>
060 * This interface places no restrictions on the mutability of implementations,
061 * however immutability is strongly recommended.
062 */
063public interface TemporalSubtractor {
064
065    /**
066     * Subtracts this object from the specified temporal object.
067     * <p>
068     * This adds to the specified temporal object using the logic
069     * encapsulated in the implementing class.
070     * <p>
071     * There are two equivalent ways of using this method.
072     * The first is to invoke this method directly.
073     * The second is to use {@link Temporal#minus(TemporalSubtractor)}:
074     * <pre>
075     *   // these two lines are equivalent, but the second approach is recommended
076     *   dateTime = subtractor.subtractFrom(dateTime);
077     *   dateTime = dateTime.minus(subtractor);
078     * </pre>
079     * It is recommended to use the second approach, {@code minus(TemporalSubtractor)},
080     * as it is a lot clearer to read in code.
081     *
082     * <h3>Specification for implementors</h3>
083     * The implementation must take the input object and subtract from it.
084     * The implementation defines the logic of the subtraction and is responsible for
085     * documenting that logic. It may use any method on {@code Temporal} to
086     * query the temporal object and perform the subtraction.
087     * The returned object must have the same observable type as the input object
088     * <p>
089     * The input object must not be altered.
090     * Instead, an adjusted copy of the original must be returned.
091     * This provides equivalent, safe behavior for immutable and mutable temporal objects.
092     * <p>
093     * The input temporal object may be in a calendar system other than ISO.
094     * Implementations may choose to document compatibility with other calendar systems,
095     * or reject non-ISO temporal objects by {@link TemporalQueries#chrono() querying the chronology}.
096     * <p>
097     * This method may be called from multiple threads in parallel.
098     * It must be thread-safe when invoked.
099     *
100     * @param temporal  the temporal object to adjust, not null
101     * @return an object of the same observable type with the subtraction made, not null
102     * @throws DateTimeException if unable to subtract
103     * @throws ArithmeticException if numeric overflow occurs
104     */
105    Temporal subtractFrom(Temporal temporal);
106
107}