intl/icu/source/i18n/tmunit.cpp

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /*
michael@0 2 *******************************************************************************
michael@0 3 * Copyright (C) 2008-2012, Google, International Business Machines Corporation and
michael@0 4 * others. All Rights Reserved.
michael@0 5 *******************************************************************************
michael@0 6 */
michael@0 7
michael@0 8 #include "utypeinfo.h" // for 'typeid' to work
michael@0 9
michael@0 10 #include "unicode/tmunit.h"
michael@0 11
michael@0 12 #if !UCONFIG_NO_FORMATTING
michael@0 13
michael@0 14 U_NAMESPACE_BEGIN
michael@0 15
michael@0 16 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(TimeUnit)
michael@0 17
michael@0 18
michael@0 19 /*
michael@0 20 * There are only 7 time units.
michael@0 21 * So, TimeUnit could be made as singleton
michael@0 22 * (similar to uniset_props.cpp, or unorm.cpp,
michael@0 23 * in which a static TimeUnit* array is created, and
michael@0 24 * the creatInstance() returns a const TimeUnit*).
michael@0 25 * But the constraint is TimeUnit is a data member of Measure.
michael@0 26 * But Measure (which is an existing API) does not expect it's "unit" member
michael@0 27 * as singleton. Meaure takes ownership of the "unit" member.
michael@0 28 * In its constructor, it does not take a const "unit" pointer.
michael@0 29 * Also, Measure can clone and destruct the "unit" pointer.
michael@0 30 * In order to preserve the old behavior and let Measure handle singleton "unit",
michael@0 31 * 1. a flag need to be added in Measure;
michael@0 32 * 2. a new constructor which takes const "unit" as parameter need to be added,
michael@0 33 * and this new constructor will set the flag on.
michael@0 34 * 3. clone and destructor need to check upon this flag to distinguish on how
michael@0 35 * to handle the "unit".
michael@0 36 *
michael@0 37 * Since TimeUnit is such a light weight object, comparing with the heavy weight
michael@0 38 * format operation, we decided to avoid the above complication.
michael@0 39 *
michael@0 40 * So, both TimeUnit and CurrencyUnit (the 2 subclasses of MeasureUnit) are
michael@0 41 * immutable and non-singleton.
michael@0 42 *
michael@0 43 * Currently, TimeUnitAmount and CurrencyAmount are immutable.
michael@0 44 * If an application needs to create a long list of TimeUnitAmount on the same
michael@0 45 * time unit but different number, for example,
michael@0 46 * 1 hour, 2 hour, 3 hour, ................. 10,000 hour,
michael@0 47 * there might be performance hit because 10,000 TimeUnit object,
michael@0 48 * although all are the same time unit, will be created in heap and deleted.
michael@0 49 *
michael@0 50 * To address this performance issue, if there is any in the future,
michael@0 51 * we should and need to change TimeUnitAmount and CurrencyAmount to be
michael@0 52 * immutable by allowing a setter on the number.
michael@0 53 * Or we need to add 2 parallel mutable classes in order to
michael@0 54 * preserve the existing API.
michael@0 55 * Or we can use freezable.
michael@0 56 */
michael@0 57 TimeUnit* U_EXPORT2
michael@0 58 TimeUnit::createInstance(TimeUnit::UTimeUnitFields timeUnitField,
michael@0 59 UErrorCode& status) {
michael@0 60 if (U_FAILURE(status)) {
michael@0 61 return NULL;
michael@0 62 }
michael@0 63 if (timeUnitField < 0 || timeUnitField >= UTIMEUNIT_FIELD_COUNT) {
michael@0 64 status = U_ILLEGAL_ARGUMENT_ERROR;
michael@0 65 return NULL;
michael@0 66 }
michael@0 67 return new TimeUnit(timeUnitField);
michael@0 68 }
michael@0 69
michael@0 70
michael@0 71 TimeUnit::TimeUnit(TimeUnit::UTimeUnitFields timeUnitField) {
michael@0 72 fTimeUnitField = timeUnitField;
michael@0 73 }
michael@0 74
michael@0 75
michael@0 76 TimeUnit::TimeUnit(const TimeUnit& other)
michael@0 77 : MeasureUnit(other) {
michael@0 78 *this = other;
michael@0 79 }
michael@0 80
michael@0 81
michael@0 82 UObject*
michael@0 83 TimeUnit::clone() const {
michael@0 84 return new TimeUnit(*this);
michael@0 85 }
michael@0 86
michael@0 87
michael@0 88 TimeUnit&
michael@0 89 TimeUnit::operator=(const TimeUnit& other) {
michael@0 90 if (this == &other) {
michael@0 91 return *this;
michael@0 92 }
michael@0 93 fTimeUnitField = other.fTimeUnitField;
michael@0 94 return *this;
michael@0 95 }
michael@0 96
michael@0 97
michael@0 98 UBool
michael@0 99 TimeUnit::operator==(const UObject& other) const {
michael@0 100 return (typeid(*this) == typeid(other)
michael@0 101 && fTimeUnitField == ((TimeUnit*)&other)->fTimeUnitField);
michael@0 102 }
michael@0 103
michael@0 104
michael@0 105 TimeUnit::UTimeUnitFields
michael@0 106 TimeUnit::getTimeUnitField() const {
michael@0 107 return fTimeUnitField;
michael@0 108 }
michael@0 109
michael@0 110
michael@0 111 TimeUnit::~TimeUnit() {
michael@0 112 }
michael@0 113
michael@0 114
michael@0 115 U_NAMESPACE_END
michael@0 116
michael@0 117 #endif

mercurial