]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/classes/core/src/com/ibm/icu/impl/duration/TimeUnit.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / classes / core / src / com / ibm / icu / impl / duration / TimeUnit.java
1 /*\r
2 ******************************************************************************\r
3 * Copyright (C) 2007, International Business Machines Corporation and   *\r
4 * others. All Rights Reserved.                                               *\r
5 ******************************************************************************\r
6 */\r
7 \r
8 package com.ibm.icu.impl.duration;\r
9 \r
10 /**\r
11  * 'Enum' for individual time units.  Not an actual enum so that it can be \r
12  * used by Java 1.4.\r
13  */\r
14 public final class TimeUnit {\r
15   /** The name of this unit, a key, not for localization. */\r
16   final String name;\r
17 \r
18   /** The ordinal of the unit, in order from largest to smallest. */\r
19   final byte ordinal;\r
20 \r
21   /** Private constructor */\r
22   private TimeUnit(String name, int ordinal) {\r
23     this.name = name;\r
24     this.ordinal = (byte) ordinal;\r
25   }\r
26 \r
27   public String toString() {\r
28     return name;\r
29   }\r
30   \r
31   /** Represents a year. */ \r
32   public static final TimeUnit YEAR = new TimeUnit("year", 0);\r
33 \r
34   /** Represents a month. */  \r
35   public static final TimeUnit MONTH = new TimeUnit("month", 1);\r
36 \r
37   /** Represents a week. */ \r
38   public static final TimeUnit WEEK = new TimeUnit("week", 2);\r
39 \r
40   /** Represents a day. */ \r
41   public static final TimeUnit DAY = new TimeUnit("day", 3);\r
42 \r
43   /** Represents an hour. */ \r
44   public static final TimeUnit HOUR = new TimeUnit("hour", 4);\r
45 \r
46   /** Represents a minute. */ \r
47   public static final TimeUnit MINUTE = new TimeUnit("minute", 5);\r
48 \r
49   /** Represents a second. */ \r
50   public static final TimeUnit SECOND = new TimeUnit("second", 6);\r
51 \r
52   /** Represents a millisecond. */ \r
53   public static final TimeUnit MILLISECOND = new TimeUnit("millisecond", 7);\r
54 \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
58   }\r
59 \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
63   }\r
64 \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
68   };\r
69 \r
70     /** Returns the ordinal value of this time unit, largest is 0. **/\r
71   public int ordinal() {\r
72     return ordinal;\r
73   }\r
74 \r
75   /** Approximate, durations for the units independent of the time at which\r
76       they are measured */\r
77 \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
84   };\r
85 }\r