blob: 0b6f8f2b335d52c4f52bdf43bebbb09282da22c9 [file] [log] [blame]
Yao Chend5aa01b32017-12-19 16:46:36 -08001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include "HashableDimensionKey.h"
17
18namespace android {
19namespace os {
20namespace statsd {
21
22using std::string;
23
24string HashableDimensionKey::toString() const {
25 string flattened;
26 for (const auto& pair : mKeyValuePairs) {
27 flattened += std::to_string(pair.key());
28 flattened += ":";
29 switch (pair.value_case()) {
30 case KeyValuePair::ValueCase::kValueStr:
31 flattened += pair.value_str();
32 break;
33 case KeyValuePair::ValueCase::kValueInt:
34 flattened += std::to_string(pair.value_int());
35 break;
36 case KeyValuePair::ValueCase::kValueLong:
37 flattened += std::to_string(pair.value_long());
38 break;
39 case KeyValuePair::ValueCase::kValueBool:
40 flattened += std::to_string(pair.value_bool());
41 break;
42 case KeyValuePair::ValueCase::kValueFloat:
43 flattened += std::to_string(pair.value_float());
44 break;
45 default:
46 break;
47 }
48 flattened += "|";
49 }
50 return flattened;
51}
52
53bool HashableDimensionKey::operator==(const HashableDimensionKey& that) const {
54 const auto& keyValue2 = that.getKeyValuePairs();
55 if (mKeyValuePairs.size() != keyValue2.size()) {
56 return false;
57 }
58
59 for (size_t i = 0; i < keyValue2.size(); i++) {
60 const auto& kv1 = mKeyValuePairs[i];
61 const auto& kv2 = keyValue2[i];
62 if (kv1.key() != kv2.key()) {
63 return false;
64 }
65
66 if (kv1.value_case() != kv2.value_case()) {
67 return false;
68 }
69
70 switch (kv1.value_case()) {
71 case KeyValuePair::ValueCase::kValueStr:
72 if (kv1.value_str() != kv2.value_str()) {
73 return false;
74 }
75 break;
76 case KeyValuePair::ValueCase::kValueInt:
77 if (kv1.value_int() != kv2.value_int()) {
78 return false;
79 }
80 break;
81 case KeyValuePair::ValueCase::kValueLong:
82 if (kv1.value_long() != kv2.value_long()) {
83 return false;
84 }
85 break;
86 case KeyValuePair::ValueCase::kValueBool:
87 if (kv1.value_bool() != kv2.value_bool()) {
88 return false;
89 }
90 break;
91 case KeyValuePair::ValueCase::kValueFloat: {
92 if (kv1.value_float() != kv2.value_float()) {
93 return false;
94 }
95 break;
96 }
97 case KeyValuePair::ValueCase::VALUE_NOT_SET:
98 break;
99 }
100 }
101 return true;
102};
103
104bool HashableDimensionKey::operator<(const HashableDimensionKey& that) const {
105 return toString().compare(that.toString()) < 0;
106};
107
108} // namespace statsd
109} // namespace os
110} // namespace android