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