michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 2008-2012, Google, International Business Machines Corporation and michael@0: * others. All Rights Reserved. michael@0: ******************************************************************************* michael@0: */ michael@0: michael@0: #include "utypeinfo.h" // for 'typeid' to work michael@0: michael@0: #include "unicode/tmunit.h" michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: UOBJECT_DEFINE_RTTI_IMPLEMENTATION(TimeUnit) michael@0: michael@0: michael@0: /* michael@0: * There are only 7 time units. michael@0: * So, TimeUnit could be made as singleton michael@0: * (similar to uniset_props.cpp, or unorm.cpp, michael@0: * in which a static TimeUnit* array is created, and michael@0: * the creatInstance() returns a const TimeUnit*). michael@0: * But the constraint is TimeUnit is a data member of Measure. michael@0: * But Measure (which is an existing API) does not expect it's "unit" member michael@0: * as singleton. Meaure takes ownership of the "unit" member. michael@0: * In its constructor, it does not take a const "unit" pointer. michael@0: * Also, Measure can clone and destruct the "unit" pointer. michael@0: * In order to preserve the old behavior and let Measure handle singleton "unit", michael@0: * 1. a flag need to be added in Measure; michael@0: * 2. a new constructor which takes const "unit" as parameter need to be added, michael@0: * and this new constructor will set the flag on. michael@0: * 3. clone and destructor need to check upon this flag to distinguish on how michael@0: * to handle the "unit". michael@0: * michael@0: * Since TimeUnit is such a light weight object, comparing with the heavy weight michael@0: * format operation, we decided to avoid the above complication. michael@0: * michael@0: * So, both TimeUnit and CurrencyUnit (the 2 subclasses of MeasureUnit) are michael@0: * immutable and non-singleton. michael@0: * michael@0: * Currently, TimeUnitAmount and CurrencyAmount are immutable. michael@0: * If an application needs to create a long list of TimeUnitAmount on the same michael@0: * time unit but different number, for example, michael@0: * 1 hour, 2 hour, 3 hour, ................. 10,000 hour, michael@0: * there might be performance hit because 10,000 TimeUnit object, michael@0: * although all are the same time unit, will be created in heap and deleted. michael@0: * michael@0: * To address this performance issue, if there is any in the future, michael@0: * we should and need to change TimeUnitAmount and CurrencyAmount to be michael@0: * immutable by allowing a setter on the number. michael@0: * Or we need to add 2 parallel mutable classes in order to michael@0: * preserve the existing API. michael@0: * Or we can use freezable. michael@0: */ michael@0: TimeUnit* U_EXPORT2 michael@0: TimeUnit::createInstance(TimeUnit::UTimeUnitFields timeUnitField, michael@0: UErrorCode& status) { michael@0: if (U_FAILURE(status)) { michael@0: return NULL; michael@0: } michael@0: if (timeUnitField < 0 || timeUnitField >= UTIMEUNIT_FIELD_COUNT) { michael@0: status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return NULL; michael@0: } michael@0: return new TimeUnit(timeUnitField); michael@0: } michael@0: michael@0: michael@0: TimeUnit::TimeUnit(TimeUnit::UTimeUnitFields timeUnitField) { michael@0: fTimeUnitField = timeUnitField; michael@0: } michael@0: michael@0: michael@0: TimeUnit::TimeUnit(const TimeUnit& other) michael@0: : MeasureUnit(other) { michael@0: *this = other; michael@0: } michael@0: michael@0: michael@0: UObject* michael@0: TimeUnit::clone() const { michael@0: return new TimeUnit(*this); michael@0: } michael@0: michael@0: michael@0: TimeUnit& michael@0: TimeUnit::operator=(const TimeUnit& other) { michael@0: if (this == &other) { michael@0: return *this; michael@0: } michael@0: fTimeUnitField = other.fTimeUnitField; michael@0: return *this; michael@0: } michael@0: michael@0: michael@0: UBool michael@0: TimeUnit::operator==(const UObject& other) const { michael@0: return (typeid(*this) == typeid(other) michael@0: && fTimeUnitField == ((TimeUnit*)&other)->fTimeUnitField); michael@0: } michael@0: michael@0: michael@0: TimeUnit::UTimeUnitFields michael@0: TimeUnit::getTimeUnitField() const { michael@0: return fTimeUnitField; michael@0: } michael@0: michael@0: michael@0: TimeUnit::~TimeUnit() { michael@0: } michael@0: michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif