diff options
Diffstat (limited to 'cmds/statsd/src/FieldValue.cpp')
-rw-r--r-- | cmds/statsd/src/FieldValue.cpp | 194 |
1 files changed, 194 insertions, 0 deletions
diff --git a/cmds/statsd/src/FieldValue.cpp b/cmds/statsd/src/FieldValue.cpp index f150f074c52b..80ed80776829 100644 --- a/cmds/statsd/src/FieldValue.cpp +++ b/cmds/statsd/src/FieldValue.cpp @@ -18,6 +18,7 @@ #include "Log.h" #include "FieldValue.h" #include "HashableDimensionKey.h" +#include "math.h" namespace android { namespace os { @@ -141,9 +142,15 @@ Value::Value(const Value& from) { case FLOAT: float_value = from.float_value; break; + case DOUBLE: + double_value = from.double_value; + break; case STRING: str_value = from.str_value; break; + case STORAGE: + storage_value = from.storage_value; + break; default: break; } @@ -157,13 +164,36 @@ std::string Value::toString() const { return std::to_string(long_value) + "[L]"; case FLOAT: return std::to_string(float_value) + "[F]"; + case DOUBLE: + return std::to_string(double_value) + "[D]"; case STRING: return str_value + "[S]"; + case STORAGE: + return "bytes of size " + std::to_string(storage_value.size()) + "[ST]"; default: return "[UNKNOWN]"; } } +bool Value::isZero() const { + switch (type) { + case INT: + return int_value == 0; + case LONG: + return long_value == 0; + case FLOAT: + return fabs(float_value) <= std::numeric_limits<float>::epsilon(); + case DOUBLE: + return fabs(double_value) <= std::numeric_limits<double>::epsilon(); + case STRING: + return str_value.size() == 0; + case STORAGE: + return storage_value.size() == 0; + default: + return false; + } +} + bool Value::operator==(const Value& that) const { if (type != that.getType()) return false; @@ -174,8 +204,12 @@ bool Value::operator==(const Value& that) const { return long_value == that.long_value; case FLOAT: return float_value == that.float_value; + case DOUBLE: + return double_value == that.double_value; case STRING: return str_value == that.str_value; + case STORAGE: + return storage_value == that.storage_value; default: return false; } @@ -190,8 +224,12 @@ bool Value::operator!=(const Value& that) const { return long_value != that.long_value; case FLOAT: return float_value != that.float_value; + case DOUBLE: + return double_value != that.double_value; case STRING: return str_value != that.str_value; + case STORAGE: + return storage_value != that.storage_value; default: return false; } @@ -207,13 +245,169 @@ bool Value::operator<(const Value& that) const { return long_value < that.long_value; case FLOAT: return float_value < that.float_value; + case DOUBLE: + return double_value < that.double_value; case STRING: return str_value < that.str_value; + case STORAGE: + return storage_value < that.storage_value; default: return false; } } +bool Value::operator>(const Value& that) const { + if (type != that.getType()) return type > that.getType(); + + switch (type) { + case INT: + return int_value > that.int_value; + case LONG: + return long_value > that.long_value; + case FLOAT: + return float_value > that.float_value; + case DOUBLE: + return double_value > that.double_value; + case STRING: + return str_value > that.str_value; + case STORAGE: + return storage_value > that.storage_value; + default: + return false; + } +} + +bool Value::operator>=(const Value& that) const { + if (type != that.getType()) return type >= that.getType(); + + switch (type) { + case INT: + return int_value >= that.int_value; + case LONG: + return long_value >= that.long_value; + case FLOAT: + return float_value >= that.float_value; + case DOUBLE: + return double_value >= that.double_value; + case STRING: + return str_value >= that.str_value; + case STORAGE: + return storage_value >= that.storage_value; + default: + return false; + } +} + +Value Value::operator-(const Value& that) const { + Value v; + if (type != that.type) { + ALOGE("Can't operate on different value types, %d, %d", type, that.type); + return v; + } + if (type == STRING) { + ALOGE("Can't operate on string value type"); + return v; + } + + if (type == STORAGE) { + ALOGE("Can't operate on storage value type"); + return v; + } + + switch (type) { + case INT: + v.setInt(int_value - that.int_value); + break; + case LONG: + v.setLong(long_value - that.long_value); + break; + case FLOAT: + v.setFloat(float_value - that.float_value); + break; + case DOUBLE: + v.setDouble(double_value - that.double_value); + break; + default: + break; + } + return v; +} + +Value& Value::operator=(const Value& that) { + type = that.type; + switch (type) { + case INT: + int_value = that.int_value; + break; + case LONG: + long_value = that.long_value; + break; + case FLOAT: + float_value = that.float_value; + break; + case DOUBLE: + double_value = that.double_value; + break; + case STRING: + str_value = that.str_value; + break; + case STORAGE: + storage_value = that.storage_value; + break; + default: + break; + } + return *this; +} + +Value& Value::operator+=(const Value& that) { + if (type != that.type) { + ALOGE("Can't operate on different value types, %d, %d", type, that.type); + return *this; + } + if (type == STRING) { + ALOGE("Can't operate on string value type"); + return *this; + } + if (type == STORAGE) { + ALOGE("Can't operate on storage value type"); + return *this; + } + + switch (type) { + case INT: + int_value += that.int_value; + break; + case LONG: + long_value += that.long_value; + break; + case FLOAT: + float_value += that.float_value; + break; + case DOUBLE: + double_value += that.double_value; + break; + default: + break; + } + return *this; +} + +double Value::getDouble() const { + switch (type) { + case INT: + return int_value; + case LONG: + return long_value; + case FLOAT: + return float_value; + case DOUBLE: + return double_value; + default: + return 0; + } +} + bool equalDimensions(const std::vector<Matcher>& dimension_a, const std::vector<Matcher>& dimension_b) { bool eq = dimension_a.size() == dimension_b.size(); |