Sho SHIMIZU

Add short-cut path taking when the paremter type is equal to own type

Change-Id: Ifbc3ea11c901b1496adcc8d0a372c86cd27969e2
......@@ -109,9 +109,13 @@ final class EncodableDiscreteResources implements DiscreteResources {
@Override
public DiscreteResources add(DiscreteResources other) {
Set<DiscreteResource> union = Sets.union(values(), other.values());
if (other instanceof EncodableDiscreteResources) {
return of(parent, Sets.union(this.values(), other.values()));
} else if (other instanceof EmptyDiscreteResources) {
return this;
}
return of(parent, union);
return DiscreteResources.of(Sets.union(this.values(), other.values()));
}
@Override
......
......@@ -81,9 +81,13 @@ final class GenericDiscreteResources implements DiscreteResources {
// returns a new instance, not mutate the current instance
@Override
public DiscreteResources add(DiscreteResources other) {
Set<DiscreteResource> newValues = new LinkedHashSet<>(this.values);
newValues.addAll(other.values());
return new GenericDiscreteResources(newValues);
if (other instanceof GenericDiscreteResources) {
return new GenericDiscreteResources(Sets.union(this.values(), other.values()));
} else if (other instanceof EmptyDiscreteResources) {
return this;
}
return DiscreteResources.of(Sets.union(this.values(), other.values()));
}
@Override
......
......@@ -93,6 +93,15 @@ final class UnifiedDiscreteResources implements DiscreteResources {
@Override
public DiscreteResources add(DiscreteResources other) {
if (other instanceof UnifiedDiscreteResources) {
UnifiedDiscreteResources cast = (UnifiedDiscreteResources) other;
return new UnifiedDiscreteResources(
this.generics.add(cast.generics),
this.encodables.add(cast.encodables));
} else if (other instanceof EmptyDiscreteResources) {
return this;
}
return of(Sets.union(this.values(), other.values()));
}
......