12ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller/* GENERATED SOURCE. DO NOT MODIFY. */
22ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller/*
32ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller******************************************************************************
42ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller* Copyright (C) 2007, International Business Machines Corporation and   *
52ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller* others. All Rights Reserved.                                               *
62ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller******************************************************************************
72ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller*/
82ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
92ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerpackage android.icu.impl.duration;
102ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
112ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerimport android.icu.impl.duration.impl.DataRecord.ETimeLimit;
122ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
132ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller/**
142ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Represents an approximate duration in multiple TimeUnits.  Each unit,
152ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * if set, has a count (which can be fractional and must be non-negative).
162ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * In addition Period can either represent the duration as being into the past
172ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * or future, and as being more or less than the defined value.
182ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * <p>
192ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Use a PeriodFormatter to convert a Period to a String.
202ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * <p>
212ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Periods are immutable.  Mutating operations return the new
222ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * result leaving the original unchanged.
232ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * <p>
242ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Example:<pre>
252ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Period p1 = Period.at(3, WEEK).and(2, DAY).inFuture();
262ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Period p2 = p1.and(12, HOUR);</pre>
27836e6b40a94ec3fb7545a76cb072960442b7eee9Neil Fuller * @hide Only a subset of ICU is exposed in Android
282ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */
292ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerpublic final class Period {
302ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  final byte timeLimit;
312ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  final boolean inFuture;
322ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  final int[] counts;
332ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
342ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  /**
352ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * Constructs a Period representing a duration of
362ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * count units extending into the past.
372ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @param count the number of units, must be non-negative
382ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @param unit the unit
392ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @return the new Period
402ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   */
412ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  public static Period at(float count, TimeUnit unit) {
422ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    checkCount(count);
432ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    return new Period(ETimeLimit.NOLIMIT, false, count, unit);
442ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  }
452ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
462ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  /**
472ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * Constructs a Period representing a duration more than
482ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * count units extending into the past.
492ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @param count the number of units. must be non-negative
502ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @param unit the unit
512ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @return the new Period
522ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   */
532ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  public static Period moreThan(float count, TimeUnit unit) {
542ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    checkCount(count);
552ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    return new Period(ETimeLimit.MT, false, count, unit);
562ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  }
572ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
582ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  /**
592ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * Constructs a Period representing a duration
602ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * less than count units extending into the past.
612ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @param count the number of units. must be non-negative
622ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @param unit the unit
632ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @return the new Period
642ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   */
652ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  public static Period lessThan(float count, TimeUnit unit) {
662ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    checkCount(count);
672ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    return new Period(ETimeLimit.LT, false, count, unit);
682ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  }
692ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
702ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  /**
712ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * Set the given unit to have the given count.  Marks the
722ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * unit as having been set.  This can be used to set
732ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * multiple units, or to reset a unit to have a new count.
742ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * This does <b>not</b> add the count to an existing count
752ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * for this unit.
762ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   *
772ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @param count the number of units.  must be non-negative
782ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @param unit the unit
792ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @return the new Period
802ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   */
812ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  public Period and(float count, TimeUnit unit) {
822ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    checkCount(count);
832ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    return setTimeUnitValue(unit, count);
842ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  }
852ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
862ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  /**
872ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * Mark the given unit as not being set.
882ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   *
892ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @param unit the unit to unset
902ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @return the new Period
912ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   */
922ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  public Period omit(TimeUnit unit) {
932ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    return setTimeUnitInternalValue(unit, 0);
942ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  }
952ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
962ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  /**
972ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * Mark the duration as being at the defined duration.
982ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   *
992ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @return the new Period
1002ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   */
1012ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  public Period at() {
1022ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    return setTimeLimit(ETimeLimit.NOLIMIT);
1032ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  }
1042ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1052ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  /**
1062ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * Mark the duration as being more than the defined duration.
1072ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   *
1082ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @return the new Period
1092ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   */
1102ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  public Period moreThan() {
1112ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    return setTimeLimit(ETimeLimit.MT);
1122ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  }
1132ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1142ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  /**
1152ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * Mark the duration as being less than the defined duration.
1162ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   *
1172ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @return the new Period
1182ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   */
1192ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  public Period lessThan() {
1202ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    return setTimeLimit(ETimeLimit.LT);
1212ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  }
1222ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1232ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  /**
1242ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * Mark the time as being in the future.
1252ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   *
1262ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @return the new Period
1272ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   */
1282ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  public Period inFuture() {
1292ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    return setFuture(true);
1302ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  }
1312ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1322ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  /**
1332ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * Mark the duration as extending into the past.
1342ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   *
1352ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @return the new Period
1362ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   */
1372ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  public Period inPast() {
1382ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    return setFuture(false);
1392ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  }
1402ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1412ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  /**
1422ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * Mark the duration as extending into the future if
1432ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * future is true, and into the past otherwise.
1442ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   *
1452ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @param future true if the time is in the future
1462ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @return the new Period
1472ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   */
1482ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  public Period inFuture(boolean future) {
1492ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    return setFuture(future);
1502ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  }
1512ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1522ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  /**
1532ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * Mark the duration as extending into the past if
1542ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * past is true, and into the future otherwise.
1552ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   *
1562ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @param past true if the time is in the past
1572ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @return the new Period
1582ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   */
1592ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  public Period inPast(boolean past) {
1602ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    return setFuture(!past);
1612ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  }
1622ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1632ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  /**
1642ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * Returns true if any unit is set.
1652ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @return true if any unit is set
1662ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   */
1672ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  public boolean isSet() {
1682ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    for (int i = 0; i < counts.length; ++i) {
1692ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller      if (counts[i] != 0) {
1702ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return true;
1712ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller      }
1722ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
1732ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    return false;
1742ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  }
1752ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1762ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  /**
1772ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * Returns true if the given unit is set.
1782ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @param unit the unit to test
1792ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @return true if the given unit is set.
1802ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   */
1812ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  public boolean isSet(TimeUnit unit) {
1822ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    return counts[unit.ordinal] > 0;
1832ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  }
1842ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1852ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  /**
1862ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * Returns the count for the specified unit.  If the
1872ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * unit is not set, returns 0.
1882ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @param unit the unit to test
1892ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @return the count
1902ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   */
1912ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  public float getCount(TimeUnit unit) {
1922ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    int ord = unit.ordinal;
1932ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    if (counts[ord] == 0) {
1942ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller      return 0;
1952ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
1962ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    return (counts[ord] - 1)/1000f;
1972ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  }
1982ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1992ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  /**
2002ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * Returns true if this represents a
2012ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * duration into the future.
2022ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @return true if this represents a
2032ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * duration into the future.
2042ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   */
2052ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  public boolean isInFuture() {
2062ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    return inFuture;
2072ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  }
2082ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
2092ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  /**
2102ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * Returns true if this represents a
2112ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * duration into the past
2122ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @return true if this represents a
2132ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * duration into the past
2142ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   */
2152ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  public boolean isInPast  () {
2162ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    return !inFuture;
2172ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  }
2182ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
2192ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  /**
2202ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * Returns true if this represents a duration in
2212ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * excess of the defined duration.
2222ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @return true if this represents a duration in
2232ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * excess of the defined duration.
2242ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   */
2252ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  public boolean isMoreThan() {
2262ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    return timeLimit == ETimeLimit.MT;
2272ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  }
2282ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
2292ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  /**
2302ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * Returns true if this represents a duration
2312ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * less than the defined duration.
2322ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @return true if this represents a duration
2332ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * less than the defined duration.
2342ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   */
2352ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  public boolean isLessThan() {
2362ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    return timeLimit == ETimeLimit.LT;
2372ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  }
2382ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
2392ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  /**
2402ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * Returns true if rhs extends Period and
2412ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * the two Periods are equal.
2422ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @param rhs the object to compare to
2432ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @return true if rhs is a Period and is equal to this
2442ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   */
2452ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  public boolean equals(Object rhs) {
2462ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    try {
2472ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller      return equals((Period)rhs);
2482ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
2492ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    catch (ClassCastException e) {
2502ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller      return false;
2512ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
2522ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  }
2532ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
2542ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  /**
2552ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * Returns true if the same units are defined with
2562ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * the same counts, both extend into the future or both into the
2572ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * past, and if the limits (at, more than, less than) are the same.
2582ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * Note that this means that a period of 1000ms and a period of 1sec
2592ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * will not compare equal.
2602ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   *
2612ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @param rhs the period to compare to
2622ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @return true if the two periods are equal
2632ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   */
2642ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  public boolean equals(Period rhs) {
2652ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    if (rhs != null &&
2662ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        this.timeLimit == rhs.timeLimit &&
2672ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        this.inFuture == rhs.inFuture) {
2682ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller      for (int i = 0; i < counts.length; ++i) {
2692ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if (counts[i] != rhs.counts[i]) {
2702ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller          return false;
2712ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
2722ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller      }
2732ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller      return true;
2742ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
2752ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    return false;
2762ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  }
2772ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
2782ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  /**
2792ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * Returns the hashCode.
2802ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @return the hashCode
2812ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   */
2822ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  public int hashCode() {
2832ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    int hc = (timeLimit << 1) | (inFuture ? 1 : 0);
2842ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    for (int i = 0; i < counts.length; ++i) {
2852ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller      hc = (hc << 2) ^ counts[i];
2862ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
2872ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    return hc;
2882ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  }
2892ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
2902ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  /**
2912ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * Private constructor used by static factory methods.
2922ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   */
2932ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  private Period(int limit, boolean future, float count, TimeUnit unit) {
2942ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    this.timeLimit = (byte) limit;
2952ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    this.inFuture = future;
2962ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    this.counts = new int[TimeUnit.units.length];
2972ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    this.counts[unit.ordinal] = (int)(count * 1000) + 1;
2982ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  }
2992ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
3002ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  /**
3012ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * Package private constructor used by setters and factory.
3022ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   */
3032ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  Period(int timeLimit, boolean inFuture, int[] counts) {
3042ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    this.timeLimit = (byte) timeLimit;
3052ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    this.inFuture = inFuture;
3062ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    this.counts = counts;
3072ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  }
3082ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
3092ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  /**
3102ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * Set the unit's internal value, converting from float to int.
3112ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   */
3122ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  private Period setTimeUnitValue(TimeUnit unit, float value) {
3132ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    if (value < 0) {
3142ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller      throw new IllegalArgumentException("value: " + value);
3152ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
3162ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    return setTimeUnitInternalValue(unit, (int)(value * 1000) + 1);
3172ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  }
3182ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
3192ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  /**
3202ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * Sets the period to have the provided value, 1/1000 of the
3212ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * unit plus 1.  Thus unset values are '0', 1' is the set value '0',
3222ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * 2 is the set value '1/1000', 3 is the set value '2/1000' etc.
3232ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @param p the period to change
3242ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @param value the int value as described above.
3252ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @eturn the new Period object.
3262ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   */
3272ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  private Period setTimeUnitInternalValue(TimeUnit unit, int value) {
3282ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    int ord = unit.ordinal;
3292ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    if (counts[ord] != value) {
3302ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller      int[] newCounts = new int[counts.length];
3312ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller      for (int i = 0; i < counts.length; ++i) {
3322ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        newCounts[i] = counts[i];
3332ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller      }
3342ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller      newCounts[ord] = value;
3352ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller      return new Period(timeLimit, inFuture, newCounts);
3362ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
3372ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    return this;
3382ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  }
3392ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
3402ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  /**
3412ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * Sets whether this defines a future time.
3422ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @param future true if the time is in the future
3432ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @return  the new Period
3442ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   */
3452ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  private Period setFuture(boolean future) {
3462ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    if (this.inFuture != future) {
3472ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller      return new Period(timeLimit, future, counts);
3482ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
3492ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    return this;
3502ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  }
3512ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
3522ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  /**
3532ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * Sets whether this is more than, less than, or
3542ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * 'about' the specified time.
3552ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @param limit the kind of limit
3562ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * @return the new Period
3572ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   */
3582ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  private Period setTimeLimit(byte limit) {
3592ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    if (this.timeLimit != limit) {
3602ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller      return new Period(limit, inFuture, counts);
3612ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
3622ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
3632ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    return this;
3642ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  }
3652ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
3662ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  /**
3672ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   * Validate count.
3682ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller   */
3692ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  private static void checkCount(float count) {
3702ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    if (count < 0) {
3712ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller      throw new IllegalArgumentException("count (" + count +
3722ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                                         ") cannot be negative");
3732ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
3742ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller  }
3752ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller}
376