1 /* 2 * Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos 3 * 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * * Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright notice, 13 * this list of conditions and the following disclaimer in the documentation 14 * and/or other materials provided with the distribution. 15 * 16 * * Neither the name of JSR-310 nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package org.threeten.bp; 33 34 import static org.threeten.bp.temporal.ChronoUnit.DAYS; 35 import static org.threeten.bp.temporal.ChronoUnit.FOREVER; 36 import static org.threeten.bp.temporal.ChronoUnit.SECONDS; 37 38 import java.util.Collections; 39 import java.util.List; 40 41 import org.threeten.bp.jdk8.Jdk8Methods; 42 import org.threeten.bp.temporal.Temporal; 43 import org.threeten.bp.temporal.TemporalAmount; 44 import org.threeten.bp.temporal.TemporalUnit; 45 46 /** 47 * Mock period of time measured using a single unit, such as {@code 3 Days}. 48 */ 49 public final class MockSimplePeriod 50 implements TemporalAmount, Comparable<MockSimplePeriod> { 51 52 /** 53 * A constant for a period of zero, measured in days. 54 */ 55 public static final MockSimplePeriod ZERO_DAYS = new MockSimplePeriod(0, DAYS); 56 /** 57 * A constant for a period of zero, measured in seconds. 58 */ 59 public static final MockSimplePeriod ZERO_SECONDS = new MockSimplePeriod(0, SECONDS); 60 61 /** 62 * The amount of the period. 63 */ 64 private final long amount; 65 /** 66 * The unit the period is measured in. 67 */ 68 private final TemporalUnit unit; 69 70 /** 71 * Obtains a {@code MockSimplePeriod} from an amount and unit. 72 * <p> 73 * The parameters represent the two parts of a phrase like '6 Days'. 74 * 75 * @param amount the amount of the period, measured in terms of the unit, positive or negative 76 * @param unit the unit that the period is measured in, must not be the 'Forever' unit, not null 77 * @return the {@code MockSimplePeriod} instance, not null 78 * @throws DateTimeException if the period unit is {@link org.threeten.bp.temporal.ChronoUnit#FOREVER}. 79 */ of(long amount, TemporalUnit unit)80 public static MockSimplePeriod of(long amount, TemporalUnit unit) { 81 return new MockSimplePeriod(amount, unit); 82 } 83 MockSimplePeriod(long amount, TemporalUnit unit)84 private MockSimplePeriod(long amount, TemporalUnit unit) { 85 Jdk8Methods.requireNonNull(unit, "unit"); 86 if (unit == FOREVER) { 87 throw new DateTimeException("Cannot create a period of the Forever unit"); 88 } 89 this.amount = amount; 90 this.unit = unit; 91 } 92 93 //----------------------------------------------------------------------- 94 @Override getUnits()95 public List<TemporalUnit> getUnits() { 96 return Collections.singletonList(unit); 97 } 98 99 @Override get(TemporalUnit unit)100 public long get(TemporalUnit unit) { 101 if (this.unit.equals(unit)) { 102 return amount; 103 } 104 throw new DateTimeException("Unsupported unit: " + unit); 105 } 106 107 //----------------------------------------------------------------------- getAmount()108 public long getAmount() { 109 return amount; 110 } 111 getUnit()112 public TemporalUnit getUnit() { 113 return unit; 114 } 115 116 //------------------------------------------------------------------------- 117 @Override addTo(Temporal dateTime)118 public Temporal addTo(Temporal dateTime) { 119 return dateTime.plus(amount, unit); 120 } 121 122 @Override subtractFrom(Temporal dateTime)123 public Temporal subtractFrom(Temporal dateTime) { 124 return dateTime.minus(amount, unit); 125 } 126 127 //----------------------------------------------------------------------- 128 @Override compareTo(MockSimplePeriod otherPeriod)129 public int compareTo(MockSimplePeriod otherPeriod) { 130 if (unit.equals(otherPeriod.getUnit()) == false) { 131 throw new IllegalArgumentException("Units cannot be compared: " + unit + " and " + otherPeriod.getUnit()); 132 } 133 return Jdk8Methods.compareLongs(amount, otherPeriod.amount); 134 } 135 136 @Override equals(Object obj)137 public boolean equals(Object obj) { 138 if (this == obj) { 139 return true; 140 } 141 if (obj instanceof MockSimplePeriod) { 142 MockSimplePeriod other = (MockSimplePeriod) obj; 143 return this.amount == other.amount && 144 this.unit.equals(other.unit); 145 } 146 return false; 147 } 148 149 @Override hashCode()150 public int hashCode() { 151 return unit.hashCode() ^ (int) (amount ^ (amount >>> 32)); 152 } 153 154 @Override toString()155 public String toString() { 156 return amount + " " + unit; 157 } 158 159 } 160