blob: c11f9f6650a26af488fd5e13dd3f715c0e04c88a [file] [log] [blame]
Sho SHIMIZUc0e010dd2016-05-02 14:46:22 -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.net.resource;
17
18import com.google.common.annotations.Beta;
19import com.google.common.collect.ImmutableSet;
20
21import java.util.Objects;
22import java.util.Optional;
23import java.util.Set;
24
25import static com.google.common.base.Preconditions.checkArgument;
26import static com.google.common.base.Preconditions.checkNotNull;
27import static com.google.common.base.Preconditions.checkState;
28
29/**
30 * Represents a set of discrete type resources.
31 * This class is intended to be used by ConsistentResourceStore though it is exposed to the public.
32 */
33@Beta
34public final class DiscreteResourceSet {
35 private final Set<DiscreteResource> values;
36 private final DiscreteResourceCodec codec;
37
Sho SHIMIZU9e00ba72016-05-04 14:00:28 -070038 private static final DiscreteResourceSet EMPTY = new DiscreteResourceSet(ImmutableSet.of(), NoOpCodec.INSTANCE);
39
Sho SHIMIZUc0e010dd2016-05-02 14:46:22 -070040 /**
41 * Creates an instance with resources and the codec for them.
42 *
43 * @param values resources to be contained in the instance
44 * @param codec codec for the specified resources
45 * @return an instance
46 */
47 public static DiscreteResourceSet of(Set<DiscreteResource> values, DiscreteResourceCodec codec) {
48 checkNotNull(values);
49 checkNotNull(codec);
50 checkArgument(!values.isEmpty());
51
52 return new DiscreteResourceSet(ImmutableSet.copyOf(values), codec);
53 }
54
55 /**
56 * Creates the instance representing an empty resource set.
57 *
58 * @return an empty resource set
59 */
60 public static DiscreteResourceSet empty() {
Sho SHIMIZU9e00ba72016-05-04 14:00:28 -070061 return EMPTY;
Sho SHIMIZUc0e010dd2016-05-02 14:46:22 -070062 }
63
64 private DiscreteResourceSet(Set<DiscreteResource> values, DiscreteResourceCodec codec) {
65 this.values = values;
66 this.codec = codec;
67 }
68
69 private DiscreteResourceSet() {
70 this.values = null;
71 this.codec = null;
72 }
73
74 /**
75 * Returns resources contained in this instance.
76 *
77 * @return resources
78 */
79 public Set<DiscreteResource> values() {
80 return values;
81 }
82
83 /**
84 * Returns the parent resource of the resources contained in this instance.
85 *
86 * @return the parent resource of the resources
87 */
88 public DiscreteResourceId parent() {
89 if (values.isEmpty()) {
90 // Dummy value avoiding null
91 return ResourceId.ROOT;
92 }
93 Optional<DiscreteResourceId> parent = values.iterator().next().id().parent();
94 checkState(parent.isPresent());
95
96 return parent.get();
97 }
98
99 /**
100 * Returns the codec for the resources contained in this instance.
101 *
102 * @return the codec for the resources
103 */
104 public DiscreteResourceCodec codec() {
105 return codec;
106 }
107
108 @Override
109 public int hashCode() {
110 return Objects.hash(values, codec);
111 }
112
113 @Override
114 public boolean equals(Object obj) {
115 if (this == obj) {
116 return true;
117 }
118
119 if (obj == null || getClass() != obj.getClass()) {
120 return false;
121 }
122
123 final DiscreteResourceSet other = (DiscreteResourceSet) obj;
124 return Objects.equals(this.values, other.values)
125 && Objects.equals(this.codec, other.codec);
126 }
127
Sho SHIMIZUe4f76ed2016-05-19 11:40:31 -0700128 public boolean contains(DiscreteResource resource) {
129 return values.contains(resource);
130 }
Sho SHIMIZUc0e010dd2016-05-02 14:46:22 -0700131}