Committed by
Gerrit Code Review
Avoid extracting resoure set from backing range set
This is one of fixes for ONOS-4684 Change-Id: I93cd95317b985f98fd6e72a4f96c7f7d6857f279
Showing
3 changed files
with
34 additions
and
1 deletions
... | @@ -30,6 +30,7 @@ import java.util.Objects; | ... | @@ -30,6 +30,7 @@ import java.util.Objects; |
30 | import java.util.Optional; | 30 | import java.util.Optional; |
31 | import java.util.Set; | 31 | import java.util.Set; |
32 | import java.util.stream.Collectors; | 32 | import java.util.stream.Collectors; |
33 | +import java.util.stream.Stream; | ||
33 | 34 | ||
34 | /** | 35 | /** |
35 | * A set of discrete resources that can be encoded as integers. | 36 | * A set of discrete resources that can be encoded as integers. |
... | @@ -88,7 +89,16 @@ final class EncodableDiscreteResources implements DiscreteResources { | ... | @@ -88,7 +89,16 @@ final class EncodableDiscreteResources implements DiscreteResources { |
88 | @Override | 89 | @Override |
89 | public DiscreteResources difference(DiscreteResources other) { | 90 | public DiscreteResources difference(DiscreteResources other) { |
90 | if (other instanceof EncodableDiscreteResources) { | 91 | if (other instanceof EncodableDiscreteResources) { |
91 | - return of(parent, Sets.difference(this.values(), other.values())); | 92 | + EncodableDiscreteResources cast = (EncodableDiscreteResources) other; |
93 | + Map<Class<?>, EncodedDiscreteResources> newMap = | ||
94 | + Stream.concat(this.map.entrySet().stream(), cast.map.entrySet().stream()) | ||
95 | + .filter(entry -> this.map.containsKey(entry.getKey())) | ||
96 | + .collect(Collectors.toMap( | ||
97 | + Map.Entry::getKey, | ||
98 | + Map.Entry::getValue, | ||
99 | + EncodedDiscreteResources::difference, | ||
100 | + LinkedHashMap::new)); | ||
101 | + return new EncodableDiscreteResources(parent, newMap); | ||
92 | } else if (other instanceof EmptyDiscreteResources) { | 102 | } else if (other instanceof EmptyDiscreteResources) { |
93 | return this; | 103 | return this; |
94 | } | 104 | } | ... | ... |
... | @@ -32,6 +32,8 @@ import java.util.Set; | ... | @@ -32,6 +32,8 @@ import java.util.Set; |
32 | import java.util.stream.Collectors; | 32 | import java.util.stream.Collectors; |
33 | import java.util.stream.IntStream; | 33 | import java.util.stream.IntStream; |
34 | 34 | ||
35 | +import static com.google.common.base.Preconditions.checkArgument; | ||
36 | + | ||
35 | /** | 37 | /** |
36 | * Represents discrete resources encoded by a codec. | 38 | * Represents discrete resources encoded by a codec. |
37 | */ | 39 | */ |
... | @@ -82,6 +84,15 @@ final class EncodedDiscreteResources { | ... | @@ -82,6 +84,15 @@ final class EncodedDiscreteResources { |
82 | .orElse(false); | 84 | .orElse(false); |
83 | } | 85 | } |
84 | 86 | ||
87 | + EncodedDiscreteResources difference(EncodedDiscreteResources other) { | ||
88 | + checkArgument(this.codec.getClass() == other.codec.getClass()); | ||
89 | + | ||
90 | + RangeSet<Integer> newRangeSet = TreeRangeSet.create(this.rangeSet); | ||
91 | + newRangeSet.removeAll(other.rangeSet); | ||
92 | + | ||
93 | + return new EncodedDiscreteResources(newRangeSet, this.codec); | ||
94 | + } | ||
95 | + | ||
85 | boolean isEmpty() { | 96 | boolean isEmpty() { |
86 | return rangeSet.isEmpty(); | 97 | return rangeSet.isEmpty(); |
87 | } | 98 | } | ... | ... |
... | @@ -50,4 +50,16 @@ public class EncodedDiscreteResourcesTest { | ... | @@ -50,4 +50,16 @@ public class EncodedDiscreteResourcesTest { |
50 | assertThat(sut.contains(res3), is(false)); | 50 | assertThat(sut.contains(res3), is(false)); |
51 | } | 51 | } |
52 | 52 | ||
53 | + @Test | ||
54 | + public void testDifference() { | ||
55 | + DiscreteResource res1 = Resources.discrete(DID, PN, VID1).resource(); | ||
56 | + DiscreteResource res2 = Resources.discrete(DID, PN, VID2).resource(); | ||
57 | + DiscreteResource res3 = Resources.discrete(DID, PN, VID3).resource(); | ||
58 | + | ||
59 | + EncodedDiscreteResources sut = EncodedDiscreteResources.of(ImmutableSet.of(res1, res2), new VlanIdCodec()); | ||
60 | + EncodedDiscreteResources other = EncodedDiscreteResources.of(ImmutableSet.of(res1, res3), new VlanIdCodec()); | ||
61 | + | ||
62 | + assertThat(sut.difference(other), is(EncodedDiscreteResources.of(ImmutableSet.of(res2), new VlanIdCodec()))); | ||
63 | + } | ||
64 | + | ||
53 | } | 65 | } | ... | ... |
-
Please register or login to post a comment