Sho SHIMIZU
Committed by Gerrit Code Review

Introduce abstract interface for set of discrete resources

Change-Id: Iddbed1a2dd0c6ce3d7288371a47a7b0aeba46803
...@@ -71,7 +71,7 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour ...@@ -71,7 +71,7 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
71 71
72 static final Serializer SERIALIZER = Serializer.using( 72 static final Serializer SERIALIZER = Serializer.using(
73 Arrays.asList(KryoNamespaces.API), 73 Arrays.asList(KryoNamespaces.API),
74 - DiscreteResources.class, 74 + NonEncodableDiscreteResources.class,
75 ContinuousResourceAllocation.class); 75 ContinuousResourceAllocation.class);
76 76
77 // TODO: We should provide centralized values for this 77 // TODO: We should provide centralized values for this
......
...@@ -15,99 +15,31 @@ ...@@ -15,99 +15,31 @@
15 */ 15 */
16 package org.onosproject.store.resource.impl; 16 package org.onosproject.store.resource.impl;
17 17
18 -import com.google.common.base.MoreObjects;
19 -import com.google.common.collect.Sets;
20 import org.onosproject.net.resource.DiscreteResource; 18 import org.onosproject.net.resource.DiscreteResource;
21 import org.onosproject.net.resource.DiscreteResourceId; 19 import org.onosproject.net.resource.DiscreteResourceId;
22 -import org.onosproject.net.resource.Resources;
23 20
24 -import java.util.LinkedHashSet;
25 import java.util.List; 21 import java.util.List;
26 -import java.util.Objects;
27 import java.util.Optional; 22 import java.util.Optional;
28 import java.util.Set; 23 import java.util.Set;
29 24
30 -final class DiscreteResources { 25 +interface DiscreteResources {
31 - private final Set<DiscreteResource> values;
32 -
33 static DiscreteResources empty() { 26 static DiscreteResources empty() {
34 - return new DiscreteResources(); 27 + return NonEncodableDiscreteResources.empty();
35 - }
36 -
37 - private DiscreteResources() {
38 - this.values = new LinkedHashSet<>();
39 - }
40 -
41 - DiscreteResources(List<DiscreteResource> values) {
42 - this.values = new LinkedHashSet<>(values);
43 - }
44 -
45 - private DiscreteResources(Set<DiscreteResource> values) {
46 - this.values = values;
47 } 28 }
48 29
49 - Optional<DiscreteResource> lookup(DiscreteResourceId id) { 30 + Optional<DiscreteResource> lookup(DiscreteResourceId id);
50 - DiscreteResource resource = Resources.discrete(id).resource();
51 - if (values.contains(resource)) {
52 - return Optional.of(resource);
53 - } else {
54 - return Optional.empty();
55 - }
56 - }
57 31
58 - DiscreteResources difference(DiscreteResources other) { 32 + DiscreteResources difference(DiscreteResources other);
59 - return new DiscreteResources(Sets.difference(this.values, other.values));
60 - }
61 33
62 - boolean isEmpty() { 34 + boolean isEmpty();
63 - return values.isEmpty();
64 - }
65 35
66 - boolean containsAny(List<DiscreteResource> other) { 36 + boolean containsAny(List<DiscreteResource> other);
67 - return other.stream().anyMatch(values::contains);
68 - }
69 37
70 // returns a new instance, not mutate the current instance 38 // returns a new instance, not mutate the current instance
71 - DiscreteResources add(DiscreteResources other) { 39 + DiscreteResources add(DiscreteResources other);
72 - Set<DiscreteResource> newValues = new LinkedHashSet<>(this.values);
73 - newValues.addAll(other.values);
74 - return new DiscreteResources(newValues);
75 - }
76 40
77 // returns a new instance, not mutate the current instance 41 // returns a new instance, not mutate the current instance
78 - DiscreteResources remove(List<DiscreteResource> removed) { 42 + DiscreteResources remove(List<DiscreteResource> removed);
79 - Set<DiscreteResource> newValues = new LinkedHashSet<>(this.values);
80 - newValues.removeAll(removed);
81 - return new DiscreteResources(newValues);
82 - }
83 -
84 - Set<DiscreteResource> values() {
85 - // breaks immutability, but intentionally returns the field
86 - // because this class is transient
87 - return values;
88 - }
89 -
90 - @Override
91 - public int hashCode() {
92 - return Objects.hash(values);
93 - }
94 43
95 - @Override 44 + Set<DiscreteResource> values();
96 - public boolean equals(Object obj) {
97 - if (this == obj) {
98 - return true;
99 - }
100 - if (obj == null || getClass() != obj.getClass()) {
101 - return false;
102 - }
103 - final DiscreteResources other = (DiscreteResources) obj;
104 - return Objects.equals(this.values, other.values);
105 - }
106 -
107 - @Override
108 - public String toString() {
109 - return MoreObjects.toStringHelper(this)
110 - .add("values", values)
111 - .toString();
112 - }
113 } 45 }
......
1 +/*
2 + * Copyright 2016-present Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.store.resource.impl;
17 +
18 +import com.google.common.base.MoreObjects;
19 +import com.google.common.collect.Sets;
20 +import org.onosproject.net.resource.DiscreteResource;
21 +import org.onosproject.net.resource.DiscreteResourceId;
22 +import org.onosproject.net.resource.Resources;
23 +
24 +import java.util.LinkedHashSet;
25 +import java.util.List;
26 +import java.util.Objects;
27 +import java.util.Optional;
28 +import java.util.Set;
29 +
30 +final class NonEncodableDiscreteResources implements DiscreteResources {
31 + private final Set<DiscreteResource> values;
32 +
33 + static NonEncodableDiscreteResources empty() {
34 + return new NonEncodableDiscreteResources();
35 + }
36 +
37 + private NonEncodableDiscreteResources() {
38 + this.values = new LinkedHashSet<>();
39 + }
40 +
41 + NonEncodableDiscreteResources(List<DiscreteResource> values) {
42 + this.values = new LinkedHashSet<>(values);
43 + }
44 +
45 + private NonEncodableDiscreteResources(Set<DiscreteResource> values) {
46 + this.values = values;
47 + }
48 +
49 + @Override
50 + public Optional<DiscreteResource> lookup(DiscreteResourceId id) {
51 + DiscreteResource resource = Resources.discrete(id).resource();
52 + if (values.contains(resource)) {
53 + return Optional.of(resource);
54 + } else {
55 + return Optional.empty();
56 + }
57 + }
58 +
59 + @Override
60 + public DiscreteResources difference(DiscreteResources other) {
61 + return new NonEncodableDiscreteResources(Sets.difference(this.values, other.values()));
62 + }
63 +
64 + @Override
65 + public boolean isEmpty() {
66 + return values.isEmpty();
67 + }
68 +
69 + @Override
70 + public boolean containsAny(List<DiscreteResource> other) {
71 + return other.stream().anyMatch(values::contains);
72 + }
73 +
74 + // returns a new instance, not mutate the current instance
75 + @Override
76 + public DiscreteResources add(DiscreteResources other) {
77 + Set<DiscreteResource> newValues = new LinkedHashSet<>(this.values);
78 + newValues.addAll(other.values());
79 + return new NonEncodableDiscreteResources(newValues);
80 + }
81 +
82 + // returns a new instance, not mutate the current instance
83 + @Override
84 + public DiscreteResources remove(List<DiscreteResource> removed) {
85 + Set<DiscreteResource> newValues = new LinkedHashSet<>(this.values);
86 + newValues.removeAll(removed);
87 + return new NonEncodableDiscreteResources(newValues);
88 + }
89 +
90 + @Override
91 + public Set<DiscreteResource> values() {
92 + // breaks immutability, but intentionally returns the field
93 + // because this class is transient
94 + return values;
95 + }
96 +
97 + @Override
98 + public int hashCode() {
99 + return Objects.hash(values);
100 + }
101 +
102 + @Override
103 + public boolean equals(Object obj) {
104 + if (this == obj) {
105 + return true;
106 + }
107 + if (obj == null || getClass() != obj.getClass()) {
108 + return false;
109 + }
110 + final NonEncodableDiscreteResources other = (NonEncodableDiscreteResources) obj;
111 + return Objects.equals(this.values, other.values);
112 + }
113 +
114 + @Override
115 + public String toString() {
116 + return MoreObjects.toStringHelper(this)
117 + .add("values", values)
118 + .toString();
119 + }
120 +}
...@@ -59,7 +59,7 @@ class TransactionalDiscreteResourceSubStore { ...@@ -59,7 +59,7 @@ class TransactionalDiscreteResourceSubStore {
59 return true; 59 return true;
60 } 60 }
61 61
62 - DiscreteResources requested = new DiscreteResources(values); 62 + DiscreteResources requested = new NonEncodableDiscreteResources(values);
63 DiscreteResources oldValues = childMap.putIfAbsent(key, requested); 63 DiscreteResources oldValues = childMap.putIfAbsent(key, requested);
64 if (oldValues == null) { 64 if (oldValues == null) {
65 return true; 65 return true;
......