1 // © 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html 3 /* 4 ****************************************************************************** 5 * Copyright (C) 2007, International Business Machines Corporation and * 6 * others. All Rights Reserved. * 7 ****************************************************************************** 8 */ 9 10 package com.ibm.icu.impl.duration; 11 12 /** 13 * 'Enum' for individual time units. Not an actual enum so that it can be 14 * used by Java 1.4. 15 */ 16 public final class TimeUnit { 17 /** The name of this unit, a key, not for localization. */ 18 final String name; 19 20 /** The ordinal of the unit, in order from largest to smallest. */ 21 final byte ordinal; 22 23 /** Private constructor */ TimeUnit(String name, int ordinal)24 private TimeUnit(String name, int ordinal) { 25 this.name = name; 26 this.ordinal = (byte) ordinal; 27 } 28 29 @Override toString()30 public String toString() { 31 return name; 32 } 33 34 /** Represents a year. */ 35 public static final TimeUnit YEAR = new TimeUnit("year", 0); 36 37 /** Represents a month. */ 38 public static final TimeUnit MONTH = new TimeUnit("month", 1); 39 40 /** Represents a week. */ 41 public static final TimeUnit WEEK = new TimeUnit("week", 2); 42 43 /** Represents a day. */ 44 public static final TimeUnit DAY = new TimeUnit("day", 3); 45 46 /** Represents an hour. */ 47 public static final TimeUnit HOUR = new TimeUnit("hour", 4); 48 49 /** Represents a minute. */ 50 public static final TimeUnit MINUTE = new TimeUnit("minute", 5); 51 52 /** Represents a second. */ 53 public static final TimeUnit SECOND = new TimeUnit("second", 6); 54 55 /** Represents a millisecond. */ 56 public static final TimeUnit MILLISECOND = new TimeUnit("millisecond", 7); 57 58 /** Returns the next larger time unit, or null if this is the largest. */ larger()59 public TimeUnit larger() { 60 return ordinal == 0 ? null : units[ordinal - 1]; 61 } 62 63 /** Returns the next smaller time unit, or null if this is the smallest. */ smaller()64 public TimeUnit smaller() { 65 return ordinal == units.length - 1 ? null : units[ordinal + 1]; 66 } 67 68 /** The list of units, in order from largest to smallest. */ 69 static final TimeUnit[] units = { 70 YEAR, MONTH, WEEK, DAY, HOUR, MINUTE, SECOND, MILLISECOND 71 }; 72 73 /** Returns the ordinal value of this time unit, largest is 0. **/ ordinal()74 public int ordinal() { 75 return ordinal; 76 } 77 78 /** Approximate, durations for the units independent of the time at which 79 they are measured */ 80 81 // hack, initialization long array using expressions with 'L' at end doesn't 82 // compute entire expression using 'long'. differs from initialization of 83 // a single constant 84 static final long[] approxDurations = { 85 36525L*24*60*60*10, 3045*24*60*60*10L, 7*24*60*60*1000L, 24*60*60*1000L, 86 60*60*1000L, 60*1000L, 1000L, 1L 87 }; 88 } 89