1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html#License
3/*
4 **************************************************************************
5 * Copyright (C) 2008-2014, Google, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **************************************************************************
8 */
9package com.ibm.icu.util;
10
11import java.io.InvalidObjectException;
12import java.io.ObjectStreamException;
13
14
15/**
16 * Measurement unit for time units.
17 * @see TimeUnitAmount
18 * @see TimeUnit
19 * @author markdavis
20 * @stable ICU 4.0
21 */
22public class TimeUnit extends MeasureUnit {
23    private static final long serialVersionUID = -2839973855554750484L;
24
25    /**
26     * Here for serialization backward compatibility only.
27     */
28    private final int index;
29
30    TimeUnit(String type, String code) {
31        super(type, code);
32        index = 0;
33    }
34
35    /**
36     * @return the available values
37     * @stable ICU 4.0
38     */
39    public static TimeUnit[] values() {
40        return new TimeUnit[] { SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, YEAR };
41    }
42
43    private Object writeReplace() throws ObjectStreamException {
44        return new MeasureUnitProxy(type, subType);
45    }
46
47    // For backward compatibility only
48    private Object readResolve() throws ObjectStreamException {
49        // The old index field used to uniquely identify the time unit.
50        switch (index) {
51        case 6:
52            return SECOND;
53        case 5:
54            return MINUTE;
55        case 4:
56            return HOUR;
57        case 3:
58            return DAY;
59        case 2:
60            return WEEK;
61        case 1:
62            return MONTH;
63        case 0:
64            return YEAR;
65        default:
66            throw new InvalidObjectException("Bad index: " + index);
67        }
68    }
69}
70