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 {
@Override
public DiscreteResources add(DiscreteResources other) {
if (other instanceof EncodableDiscreteResources) {
return of(parent, Sets.union(this.values(), other.values()));
EncodableDiscreteResources cast = (EncodableDiscreteResources) other;
LinkedHashMap<Class<?>, EncodedDiscreteResources> newMap =
Stream.concat(this.map.entrySet().stream(), cast.map.entrySet().stream())
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
EncodedDiscreteResources::add,
LinkedHashMap::new
));
return new EncodableDiscreteResources(parent, newMap);
} else if (other instanceof EmptyDiscreteResources) {
return this;
}
......
......@@ -93,6 +93,15 @@ final class EncodedDiscreteResources {
return new EncodedDiscreteResources(newRangeSet, this.codec);
}
EncodedDiscreteResources add(EncodedDiscreteResources other) {
checkArgument(this.codec.getClass() == other.codec.getClass());
RangeSet<Integer> newRangeSet = TreeRangeSet.create(this.rangeSet);
newRangeSet.addAll(other.rangeSet);
return new EncodedDiscreteResources(newRangeSet, this.codec);
}
boolean isEmpty() {
return rangeSet.isEmpty();
}
......
......@@ -62,4 +62,17 @@ public class EncodedDiscreteResourcesTest {
assertThat(sut.difference(other), is(EncodedDiscreteResources.of(ImmutableSet.of(res2), new VlanIdCodec())));
}
@Test
public void testAdd() {
DiscreteResource res1 = Resources.discrete(DID, PN, VID1).resource();
DiscreteResource res2 = Resources.discrete(DID, PN, VID2).resource();
DiscreteResource res3 = Resources.discrete(DID, PN, VID3).resource();
EncodedDiscreteResources sut = EncodedDiscreteResources.of(ImmutableSet.of(res1, res2), new VlanIdCodec());
EncodedDiscreteResources other = EncodedDiscreteResources.of(ImmutableSet.of(res1, res3), new VlanIdCodec());
assertThat(sut.add(other),
is(EncodedDiscreteResources.of(ImmutableSet.of(res1, res2, res3), new VlanIdCodec())));
}
}
......