Skip to content

Commit 5f59602

Browse files
authored
Merge pull request kubernetes-client#3495 from nulls/feature/quantity_hash_code
Override hashCode in Quantity
2 parents b84685e + 62d3dad commit 5f59602

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

kubernetes/src/main/java/io/kubernetes/client/custom/Quantity.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,14 @@ public boolean equals(Object o) {
8585
}
8686

8787
Quantity otherQuantity = (Quantity) o;
88-
8988
return ObjectUtils.compare(this.number, otherQuantity.number) == 0;
9089
}
9190

91+
@Override
92+
public int hashCode() {
93+
return Objects.hash(number);
94+
}
95+
9296
public static class QuantityAdapter extends TypeAdapter<Quantity> {
9397
@Override
9498
public void write(JsonWriter jsonWriter, Quantity quantity) throws IOException {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.custom;
14+
15+
import org.junit.jupiter.api.Test;
16+
17+
import java.util.List;
18+
import java.util.Set;
19+
import java.util.stream.Collectors;
20+
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
23+
class QuantityTest {
24+
25+
@Test
26+
void containsInSet() {
27+
final List<String> values = List.of("0.5Gi", "0.5G", "1Gi", "1G", "500Mi", "500M", "500m", "0.5", "1");
28+
final Set<Quantity> set1 = values.stream().map(Quantity::fromString).collect(Collectors.toSet());
29+
final Set<Quantity> set2 = values.stream().map(Quantity::fromString).collect(Collectors.toSet());
30+
31+
assertEquals(set1, set2, "Sets of equal elements are not equal");
32+
}
33+
}

0 commit comments

Comments
 (0)