2 ******************************************************************************
\r
3 * Copyright (C) 2007, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 ******************************************************************************
\r
8 package com.ibm.icu.impl.duration;
\r
11 * 'Enum' for individual time units. Not an actual enum so that it can be
\r
14 public final class TimeUnit {
\r
15 /** The name of this unit, a key, not for localization. */
\r
18 /** The ordinal of the unit, in order from largest to smallest. */
\r
21 /** Private constructor */
\r
22 private TimeUnit(String name, int ordinal) {
\r
24 this.ordinal = (byte) ordinal;
\r
27 public String toString() {
\r
31 /** Represents a year. */
\r
32 public static final TimeUnit YEAR = new TimeUnit("year", 0);
\r
34 /** Represents a month. */
\r
35 public static final TimeUnit MONTH = new TimeUnit("month", 1);
\r
37 /** Represents a week. */
\r
38 public static final TimeUnit WEEK = new TimeUnit("week", 2);
\r
40 /** Represents a day. */
\r
41 public static final TimeUnit DAY = new TimeUnit("day", 3);
\r
43 /** Represents an hour. */
\r
44 public static final TimeUnit HOUR = new TimeUnit("hour", 4);
\r
46 /** Represents a minute. */
\r
47 public static final TimeUnit MINUTE = new TimeUnit("minute", 5);
\r
49 /** Represents a second. */
\r
50 public static final TimeUnit SECOND = new TimeUnit("second", 6);
\r
52 /** Represents a millisecond. */
\r
53 public static final TimeUnit MILLISECOND = new TimeUnit("millisecond", 7);
\r
55 /** Returns the next larger time unit, or null if this is the largest. */
\r
56 public TimeUnit larger() {
\r
57 return ordinal == 0 ? null : units[ordinal - 1];
\r
60 /** Returns the next smaller time unit, or null if this is the smallest. */
\r
61 public TimeUnit smaller() {
\r
62 return ordinal == units.length - 1 ? null : units[ordinal + 1];
\r
65 /** The list of units, in order from largest to smallest. */
\r
66 static final TimeUnit[] units = {
\r
67 YEAR, MONTH, WEEK, DAY, HOUR, MINUTE, SECOND, MILLISECOND
\r
70 /** Returns the ordinal value of this time unit, largest is 0. **/
\r
71 public int ordinal() {
\r
75 /** Approximate, durations for the units independent of the time at which
\r
76 they are measured */
\r
78 // hack, initialization long array using expressions with 'L' at end doesn't
\r
79 // compute entire expression using 'long'. differs from initializtion of
\r
80 // a single constant
\r
81 static final long[] approxDurations = {
\r
82 36525L*24*60*60*10, 3045*24*60*60*10L, 7*24*60*60*1000L, 24*60*60*1000L,
\r
83 60*60*1000L, 60*1000L, 1000L, 1L
\r