2 *******************************************************************************
3 * Copyright (C) 2008-2014, Google, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 *******************************************************************************
8 #include "unicode/tmunit.h"
11 #if !UCONFIG_NO_FORMATTING
15 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(TimeUnit
)
19 * There are only 7 time units.
20 * So, TimeUnit could be made as singleton
21 * (similar to uniset_props.cpp, or unorm.cpp,
22 * in which a static TimeUnit* array is created, and
23 * the creatInstance() returns a const TimeUnit*).
24 * But the constraint is TimeUnit is a data member of Measure.
25 * But Measure (which is an existing API) does not expect it's "unit" member
26 * as singleton. Meaure takes ownership of the "unit" member.
27 * In its constructor, it does not take a const "unit" pointer.
28 * Also, Measure can clone and destruct the "unit" pointer.
29 * In order to preserve the old behavior and let Measure handle singleton "unit",
30 * 1. a flag need to be added in Measure;
31 * 2. a new constructor which takes const "unit" as parameter need to be added,
32 * and this new constructor will set the flag on.
33 * 3. clone and destructor need to check upon this flag to distinguish on how
34 * to handle the "unit".
36 * Since TimeUnit is such a light weight object, comparing with the heavy weight
37 * format operation, we decided to avoid the above complication.
39 * So, both TimeUnit and CurrencyUnit (the 2 subclasses of MeasureUnit) are
40 * immutable and non-singleton.
42 * Currently, TimeUnitAmount and CurrencyAmount are immutable.
43 * If an application needs to create a long list of TimeUnitAmount on the same
44 * time unit but different number, for example,
45 * 1 hour, 2 hour, 3 hour, ................. 10,000 hour,
46 * there might be performance hit because 10,000 TimeUnit object,
47 * although all are the same time unit, will be created in heap and deleted.
49 * To address this performance issue, if there is any in the future,
50 * we should and need to change TimeUnitAmount and CurrencyAmount to be
51 * immutable by allowing a setter on the number.
52 * Or we need to add 2 parallel mutable classes in order to
53 * preserve the existing API.
54 * Or we can use freezable.
57 TimeUnit::createInstance(TimeUnit::UTimeUnitFields timeUnitField
,
59 if (U_FAILURE(status
)) {
62 if (timeUnitField
< 0 || timeUnitField
>= UTIMEUNIT_FIELD_COUNT
) {
63 status
= U_ILLEGAL_ARGUMENT_ERROR
;
66 return new TimeUnit(timeUnitField
);
70 TimeUnit::TimeUnit(TimeUnit::UTimeUnitFields timeUnitField
) {
71 fTimeUnitField
= timeUnitField
;
72 switch (fTimeUnitField
) {
88 case UTIMEUNIT_MINUTE
:
91 case UTIMEUNIT_SECOND
:
100 TimeUnit::TimeUnit(const TimeUnit
& other
)
101 : MeasureUnit(other
), fTimeUnitField(other
.fTimeUnitField
) {
105 TimeUnit::clone() const {
106 return new TimeUnit(*this);
110 TimeUnit::operator=(const TimeUnit
& other
) {
111 if (this == &other
) {
114 MeasureUnit::operator=(other
);
115 fTimeUnitField
= other
.fTimeUnitField
;
119 TimeUnit::UTimeUnitFields
120 TimeUnit::getTimeUnitField() const {
121 return fTimeUnitField
;
124 TimeUnit::~TimeUnit() {