Sho SHIMIZU
Committed by Gerrit Code Review

Avoid extracting resoure set from backing range set

This is one of fixes for ONOS-4684

Change-Id: I5192df366d16b8aad5f8987abbf539897ca0e51c
...@@ -122,7 +122,16 @@ final class EncodableDiscreteResources implements DiscreteResources { ...@@ -122,7 +122,16 @@ final class EncodableDiscreteResources implements DiscreteResources {
122 @Override 122 @Override
123 public DiscreteResources add(DiscreteResources other) { 123 public DiscreteResources add(DiscreteResources other) {
124 if (other instanceof EncodableDiscreteResources) { 124 if (other instanceof EncodableDiscreteResources) {
125 - return of(parent, Sets.union(this.values(), other.values())); 125 + EncodableDiscreteResources cast = (EncodableDiscreteResources) other;
126 + LinkedHashMap<Class<?>, EncodedDiscreteResources> newMap =
127 + Stream.concat(this.map.entrySet().stream(), cast.map.entrySet().stream())
128 + .collect(Collectors.toMap(
129 + Map.Entry::getKey,
130 + Map.Entry::getValue,
131 + EncodedDiscreteResources::add,
132 + LinkedHashMap::new
133 + ));
134 + return new EncodableDiscreteResources(parent, newMap);
126 } else if (other instanceof EmptyDiscreteResources) { 135 } else if (other instanceof EmptyDiscreteResources) {
127 return this; 136 return this;
128 } 137 }
......
...@@ -93,6 +93,15 @@ final class EncodedDiscreteResources { ...@@ -93,6 +93,15 @@ final class EncodedDiscreteResources {
93 return new EncodedDiscreteResources(newRangeSet, this.codec); 93 return new EncodedDiscreteResources(newRangeSet, this.codec);
94 } 94 }
95 95
96 + EncodedDiscreteResources add(EncodedDiscreteResources other) {
97 + checkArgument(this.codec.getClass() == other.codec.getClass());
98 +
99 + RangeSet<Integer> newRangeSet = TreeRangeSet.create(this.rangeSet);
100 + newRangeSet.addAll(other.rangeSet);
101 +
102 + return new EncodedDiscreteResources(newRangeSet, this.codec);
103 + }
104 +
96 boolean isEmpty() { 105 boolean isEmpty() {
97 return rangeSet.isEmpty(); 106 return rangeSet.isEmpty();
98 } 107 }
......
...@@ -62,4 +62,17 @@ public class EncodedDiscreteResourcesTest { ...@@ -62,4 +62,17 @@ public class EncodedDiscreteResourcesTest {
62 assertThat(sut.difference(other), is(EncodedDiscreteResources.of(ImmutableSet.of(res2), new VlanIdCodec()))); 62 assertThat(sut.difference(other), is(EncodedDiscreteResources.of(ImmutableSet.of(res2), new VlanIdCodec())));
63 } 63 }
64 64
65 + @Test
66 + public void testAdd() {
67 + DiscreteResource res1 = Resources.discrete(DID, PN, VID1).resource();
68 + DiscreteResource res2 = Resources.discrete(DID, PN, VID2).resource();
69 + DiscreteResource res3 = Resources.discrete(DID, PN, VID3).resource();
70 +
71 + EncodedDiscreteResources sut = EncodedDiscreteResources.of(ImmutableSet.of(res1, res2), new VlanIdCodec());
72 + EncodedDiscreteResources other = EncodedDiscreteResources.of(ImmutableSet.of(res1, res3), new VlanIdCodec());
73 +
74 + assertThat(sut.add(other),
75 + is(EncodedDiscreteResources.of(ImmutableSet.of(res1, res2, res3), new VlanIdCodec())));
76 + }
77 +
65 } 78 }
......