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 { ...@@ -109,9 +109,13 @@ final class EncodableDiscreteResources implements DiscreteResources {
109 109
110 @Override 110 @Override
111 public DiscreteResources add(DiscreteResources other) { 111 public DiscreteResources add(DiscreteResources other) {
112 - Set<DiscreteResource> union = Sets.union(values(), other.values()); 112 + if (other instanceof EncodableDiscreteResources) {
113 + return of(parent, Sets.union(this.values(), other.values()));
114 + } else if (other instanceof EmptyDiscreteResources) {
115 + return this;
116 + }
113 117
114 - return of(parent, union); 118 + return DiscreteResources.of(Sets.union(this.values(), other.values()));
115 } 119 }
116 120
117 @Override 121 @Override
......
...@@ -81,9 +81,13 @@ final class GenericDiscreteResources implements DiscreteResources { ...@@ -81,9 +81,13 @@ final class GenericDiscreteResources implements DiscreteResources {
81 // returns a new instance, not mutate the current instance 81 // returns a new instance, not mutate the current instance
82 @Override 82 @Override
83 public DiscreteResources add(DiscreteResources other) { 83 public DiscreteResources add(DiscreteResources other) {
84 - Set<DiscreteResource> newValues = new LinkedHashSet<>(this.values); 84 + if (other instanceof GenericDiscreteResources) {
85 - newValues.addAll(other.values()); 85 + return new GenericDiscreteResources(Sets.union(this.values(), other.values()));
86 - return new GenericDiscreteResources(newValues); 86 + } else if (other instanceof EmptyDiscreteResources) {
87 + return this;
88 + }
89 +
90 + return DiscreteResources.of(Sets.union(this.values(), other.values()));
87 } 91 }
88 92
89 @Override 93 @Override
......
...@@ -93,6 +93,15 @@ final class UnifiedDiscreteResources implements DiscreteResources { ...@@ -93,6 +93,15 @@ final class UnifiedDiscreteResources implements DiscreteResources {
93 93
94 @Override 94 @Override
95 public DiscreteResources add(DiscreteResources other) { 95 public DiscreteResources add(DiscreteResources other) {
96 + if (other instanceof UnifiedDiscreteResources) {
97 + UnifiedDiscreteResources cast = (UnifiedDiscreteResources) other;
98 + return new UnifiedDiscreteResources(
99 + this.generics.add(cast.generics),
100 + this.encodables.add(cast.encodables));
101 + } else if (other instanceof EmptyDiscreteResources) {
102 + return this;
103 + }
104 +
96 return of(Sets.union(this.values(), other.values())); 105 return of(Sets.union(this.values(), other.values()));
97 } 106 }
98 107
......