1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/icu/source/i18n/tmunit.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,117 @@ 1.4 +/* 1.5 + ******************************************************************************* 1.6 + * Copyright (C) 2008-2012, Google, International Business Machines Corporation and 1.7 + * others. All Rights Reserved. 1.8 + ******************************************************************************* 1.9 + */ 1.10 + 1.11 +#include "utypeinfo.h" // for 'typeid' to work 1.12 + 1.13 +#include "unicode/tmunit.h" 1.14 + 1.15 +#if !UCONFIG_NO_FORMATTING 1.16 + 1.17 +U_NAMESPACE_BEGIN 1.18 + 1.19 +UOBJECT_DEFINE_RTTI_IMPLEMENTATION(TimeUnit) 1.20 + 1.21 + 1.22 +/* 1.23 + * There are only 7 time units. 1.24 + * So, TimeUnit could be made as singleton 1.25 + * (similar to uniset_props.cpp, or unorm.cpp, 1.26 + * in which a static TimeUnit* array is created, and 1.27 + * the creatInstance() returns a const TimeUnit*). 1.28 + * But the constraint is TimeUnit is a data member of Measure. 1.29 + * But Measure (which is an existing API) does not expect it's "unit" member 1.30 + * as singleton. Meaure takes ownership of the "unit" member. 1.31 + * In its constructor, it does not take a const "unit" pointer. 1.32 + * Also, Measure can clone and destruct the "unit" pointer. 1.33 + * In order to preserve the old behavior and let Measure handle singleton "unit", 1.34 + * 1. a flag need to be added in Measure; 1.35 + * 2. a new constructor which takes const "unit" as parameter need to be added, 1.36 + * and this new constructor will set the flag on. 1.37 + * 3. clone and destructor need to check upon this flag to distinguish on how 1.38 + * to handle the "unit". 1.39 + * 1.40 + * Since TimeUnit is such a light weight object, comparing with the heavy weight 1.41 + * format operation, we decided to avoid the above complication. 1.42 + * 1.43 + * So, both TimeUnit and CurrencyUnit (the 2 subclasses of MeasureUnit) are 1.44 + * immutable and non-singleton. 1.45 + * 1.46 + * Currently, TimeUnitAmount and CurrencyAmount are immutable. 1.47 + * If an application needs to create a long list of TimeUnitAmount on the same 1.48 + * time unit but different number, for example, 1.49 + * 1 hour, 2 hour, 3 hour, ................. 10,000 hour, 1.50 + * there might be performance hit because 10,000 TimeUnit object, 1.51 + * although all are the same time unit, will be created in heap and deleted. 1.52 + * 1.53 + * To address this performance issue, if there is any in the future, 1.54 + * we should and need to change TimeUnitAmount and CurrencyAmount to be 1.55 + * immutable by allowing a setter on the number. 1.56 + * Or we need to add 2 parallel mutable classes in order to 1.57 + * preserve the existing API. 1.58 + * Or we can use freezable. 1.59 + */ 1.60 +TimeUnit* U_EXPORT2 1.61 +TimeUnit::createInstance(TimeUnit::UTimeUnitFields timeUnitField, 1.62 + UErrorCode& status) { 1.63 + if (U_FAILURE(status)) { 1.64 + return NULL; 1.65 + } 1.66 + if (timeUnitField < 0 || timeUnitField >= UTIMEUNIT_FIELD_COUNT) { 1.67 + status = U_ILLEGAL_ARGUMENT_ERROR; 1.68 + return NULL; 1.69 + } 1.70 + return new TimeUnit(timeUnitField); 1.71 +} 1.72 + 1.73 + 1.74 +TimeUnit::TimeUnit(TimeUnit::UTimeUnitFields timeUnitField) { 1.75 + fTimeUnitField = timeUnitField; 1.76 +} 1.77 + 1.78 + 1.79 +TimeUnit::TimeUnit(const TimeUnit& other) 1.80 +: MeasureUnit(other) { 1.81 + *this = other; 1.82 +} 1.83 + 1.84 + 1.85 +UObject* 1.86 +TimeUnit::clone() const { 1.87 + return new TimeUnit(*this); 1.88 +} 1.89 + 1.90 + 1.91 +TimeUnit& 1.92 +TimeUnit::operator=(const TimeUnit& other) { 1.93 + if (this == &other) { 1.94 + return *this; 1.95 + } 1.96 + fTimeUnitField = other.fTimeUnitField; 1.97 + return *this; 1.98 +} 1.99 + 1.100 + 1.101 +UBool 1.102 +TimeUnit::operator==(const UObject& other) const { 1.103 + return (typeid(*this) == typeid(other) 1.104 + && fTimeUnitField == ((TimeUnit*)&other)->fTimeUnitField); 1.105 +} 1.106 + 1.107 + 1.108 +TimeUnit::UTimeUnitFields 1.109 +TimeUnit::getTimeUnitField() const { 1.110 + return fTimeUnitField; 1.111 +} 1.112 + 1.113 + 1.114 +TimeUnit::~TimeUnit() { 1.115 +} 1.116 + 1.117 + 1.118 +U_NAMESPACE_END 1.119 + 1.120 +#endif