blob: 4e4943553386fa19da782d1d046fcf6b7d241578 [file] [log] [blame]
Sho SHIMIZUb85000d2016-05-17 14:00:05 -07001/*
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 */
16package org.onosproject.store.resource.impl;
17
18import com.google.common.base.MoreObjects;
19import com.google.common.collect.Sets;
20import org.onosproject.net.resource.DiscreteResource;
21import org.onosproject.net.resource.DiscreteResourceId;
22import org.onosproject.net.resource.Resources;
23
24import java.util.LinkedHashSet;
25import java.util.List;
26import java.util.Objects;
27import java.util.Optional;
28import java.util.Set;
29
30final class DiscreteResources {
31 private final Set<DiscreteResource> values;
32
33 static DiscreteResources empty() {
34 return new DiscreteResources();
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 }
48
49 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
58 DiscreteResources difference(DiscreteResources other) {
59 return new DiscreteResources(Sets.difference(this.values, other.values));
60 }
61
62 boolean isEmpty() {
63 return values.isEmpty();
64 }
65
66 boolean containsAny(List<DiscreteResource> other) {
67 return other.stream().anyMatch(values::contains);
68 }
69
70 // returns a new instance, not mutate the current instance
71 DiscreteResources add(DiscreteResources other) {
72 Set<DiscreteResource> newValues = new LinkedHashSet<>(this.values);
73 newValues.addAll(other.values);
74 return new DiscreteResources(newValues);
75 }
76
77 // returns a new instance, not mutate the current instance
78 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
95 @Override
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}