Skip to content

Commit f83049d

Browse files
committed
impl for every data type hashCode and equals
1 parent 82e2f43 commit f83049d

File tree

10 files changed

+255
-1
lines changed

10 files changed

+255
-1
lines changed

lib/src/collectors/implementations/trafic_consumption/http_client/http_client.extension.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import 'dart:typed_data';
99

1010
import 'package:http_parser/http_parser.dart';
1111

12+
// TODO: move into separate package
13+
1214
extension HttpClientRequestExtension on HttpClientRequest {
1315
HttpClientRequestExtended toExtended() => HttpClientRequestExtended(this);
1416
}

lib/src/models/core/extremum.dart

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import 'dart:convert';
66

7-
import 'encodable.dart';
7+
import 'package:app_analysis/app_analysis.dart';
88

99
class Extremum<T> implements Encodable {
1010
const Extremum(this.min, this.max);
@@ -32,4 +32,23 @@ class Extremum<T> implements Encodable {
3232

3333
@override
3434
String toString() => json.encode(toMap());
35+
36+
@override
37+
int get hashCode {
38+
if (T is List) {
39+
return MdHash.list(min as List) ^ MdHash.list(max as List);
40+
}
41+
42+
return min.hashCode ^ max.hashCode;
43+
}
44+
45+
@override
46+
bool operator ==(covariant Extremum<T> other) {
47+
if (T is List) {
48+
return MdEquals.list(min as List, other.min as List) &&
49+
MdEquals.list(max as List, other.max as List);
50+
}
51+
52+
return min == other.min && max == other.max;
53+
}
3554
}

lib/src/models/core/mem_unit.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,10 @@ class MemUnit implements Comparable<MemUnit> {
2727

2828
@override
2929
int compareTo(MemUnit other) => bytes.compareTo(other.bytes);
30+
31+
@override
32+
int get hashCode => bytes.hashCode;
33+
34+
@override
35+
bool operator ==(covariant MemUnit other) => bytes == other.bytes;
3036
}

lib/src/models/core/ram_info.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,13 @@ class RamInfo implements Encodable {
4141
'available': available.bytes,
4242
};
4343
}
44+
45+
@override
46+
int get hashCode => total.hashCode ^ used.hashCode ^ available.hashCode;
47+
48+
@override
49+
bool operator ==(covariant RamInfo other) =>
50+
total == other.total &&
51+
used == other.used &&
52+
available == other.available;
4453
}

lib/src/models/data.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,22 @@ class AnalysisData implements AnalysisDataInterface {
9494

9595
@override
9696
String toString() => json.encode(toMap());
97+
98+
@override
99+
int get hashCode =>
100+
MdHash.map(batteryLevel) ^
101+
MdHash.map(batteryTemperature) ^
102+
MdHash.map(cpuFrequency) ^
103+
MdHash.map(cpuTemperature) ^
104+
MdHash.map(ramConsumption) ^
105+
MdHash.map(trafficConsumption);
106+
107+
@override
108+
bool operator ==(covariant AnalysisData other) =>
109+
MdEquals.map(batteryLevel, other.batteryLevel) &&
110+
MdEquals.map(batteryTemperature, other.batteryTemperature) &&
111+
MdEquals.map(cpuFrequency, other.cpuFrequency) &&
112+
MdEquals.map(cpuTemperature, other.cpuTemperature) &&
113+
MdEquals.map(ramConsumption, other.ramConsumption) &&
114+
MdEquals.map(trafficConsumption, other.trafficConsumption);
97115
}

lib/src/models/extremums.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,20 @@ class AnalysisExtremums implements AnalysisExtremumsInterface {
6969

7070
@override
7171
String toString() => json.encode(toMap());
72+
73+
@override
74+
int get hashCode =>
75+
batteryLevel.hashCode ^
76+
batteryTemperature.hashCode ^
77+
cpuFrequency.hashCode ^
78+
cpuTemperature.hashCode ^
79+
ramConsumption.hashCode;
80+
81+
@override
82+
bool operator ==(covariant AnalysisExtremums other) =>
83+
batteryLevel == other.batteryLevel &&
84+
batteryTemperature == other.batteryTemperature &&
85+
cpuFrequency == other.cpuFrequency &&
86+
cpuTemperature == other.cpuTemperature &&
87+
ramConsumption == other.ramConsumption;
7288
}

lib/src/models/info.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,22 @@ class AnalysisInfo implements AnalysisInfoInterface {
106106

107107
@override
108108
String toString() => json.encode(toMap());
109+
110+
@override
111+
int get hashCode =>
112+
id.hashCode ^
113+
startedAt.hashCode ^
114+
endedAt.hashCode ^
115+
data.hashCode ^
116+
extremums.hashCode ^
117+
units.hashCode;
118+
119+
@override
120+
bool operator ==(covariant AnalysisInfo other) =>
121+
id == other.id &&
122+
startedAt == other.startedAt &&
123+
endedAt == other.endedAt &&
124+
data == other.data &&
125+
extremums == other.extremums &&
126+
units == other.units;
109127
}

lib/src/models/units.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,18 @@ class AnalysisUnits implements AnalysisUnitsInterface {
7171

7272
@override
7373
String toString() => json.encode(toMap());
74+
75+
@override
76+
int get hashCode =>
77+
batteryLevel.hashCode ^
78+
batteryTemperature.hashCode ^
79+
cpuFrequency.hashCode ^
80+
cpuTemperature.hashCode;
81+
82+
@override
83+
bool operator ==(covariant AnalysisUnits other) =>
84+
batteryLevel == other.batteryLevel &&
85+
batteryTemperature == other.batteryTemperature &&
86+
cpuFrequency == other.cpuFrequency &&
87+
cpuTemperature == other.cpuTemperature;
7488
}

lib/src/utils.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
export 'utils/equals.dart';
56
export 'utils/delegates.dart';
67
export 'utils/exceptions.dart';
78
export 'utils/id.generator.dart';

lib/src/utils/equals.dart

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
// Copyright (c) 2022, the MarchDev Toolkit project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// TODO: move into separate package
6+
7+
class MdEquals {
8+
const MdEquals._();
9+
10+
static bool map<T, U>(Map<T, U>? a, Map<T, U>? b) {
11+
if (a == null) {
12+
return b == null;
13+
}
14+
if (b == null || a.length != b.length) {
15+
return false;
16+
}
17+
if (identical(a, b)) {
18+
return true;
19+
}
20+
for (final T key in a.keys) {
21+
final av = a[key];
22+
final bv = b[key];
23+
if (!b.containsKey(key)) {
24+
return false;
25+
}
26+
if (bv.runtimeType != av.runtimeType) {
27+
return false;
28+
}
29+
if (av is Map && bv is Map) {
30+
return map(av, bv);
31+
}
32+
if (av is List && bv is List) {
33+
return list(av, bv);
34+
}
35+
if (av is Set && bv is Set) {
36+
return set(av, bv);
37+
}
38+
}
39+
return true;
40+
}
41+
42+
static bool list<T>(List<T>? a, List<T>? b) {
43+
if (a == null) {
44+
return b == null;
45+
}
46+
if (b == null || a.length != b.length) {
47+
return false;
48+
}
49+
if (identical(a, b)) {
50+
return true;
51+
}
52+
for (int index = 0; index < a.length; index += 1) {
53+
final av = a[index];
54+
final bv = b[index];
55+
if (bv.runtimeType != av.runtimeType) {
56+
return false;
57+
}
58+
if (av is Map && bv is Map) {
59+
return map(av, bv);
60+
}
61+
if (av is List && bv is List) {
62+
return list(av, bv);
63+
}
64+
if (av is Set && bv is Set) {
65+
return set(av, bv);
66+
}
67+
if (av != bv) {
68+
return false;
69+
}
70+
}
71+
return true;
72+
}
73+
74+
static bool set<T>(Set<T>? a, Set<T>? b) {
75+
if (a == null) {
76+
return b == null;
77+
}
78+
if (b == null || a.length != b.length) {
79+
return false;
80+
}
81+
if (identical(a, b)) {
82+
return true;
83+
}
84+
for (final T av in a) {
85+
if (!b.contains(av)) {
86+
return false;
87+
}
88+
}
89+
return true;
90+
}
91+
}
92+
93+
class MdHash {
94+
const MdHash._();
95+
96+
static int _hash<T>(T item) {
97+
int hash = item.hashCode;
98+
99+
if (item is Map) {
100+
hash = map(item);
101+
}
102+
if (item is List) {
103+
hash = list(item);
104+
}
105+
if (item is Set) {
106+
hash = set(item);
107+
}
108+
109+
return hash;
110+
}
111+
112+
static int map<T, U>(Map<T, U>? m) {
113+
if (m == null || m.isEmpty) {
114+
return 0;
115+
}
116+
117+
int hash = 0;
118+
for (final key in m.keys) {
119+
final value = m[key];
120+
hash ^= _hash(key) ^ _hash(value);
121+
}
122+
123+
return hash;
124+
}
125+
126+
static int list<T>(List<T>? l) {
127+
if (l == null || l.isEmpty) {
128+
return 0;
129+
}
130+
131+
int hash = 0;
132+
for (final value in l) {
133+
hash ^= _hash(value);
134+
}
135+
136+
return hash;
137+
}
138+
139+
static int set<T>(Set<T>? s) {
140+
if (s == null || s.isEmpty) {
141+
return 0;
142+
}
143+
144+
int hash = 0;
145+
for (final value in s) {
146+
hash ^= _hash(value);
147+
}
148+
149+
return hash;
150+
}
151+
}

0 commit comments

Comments
 (0)