blob: 486260669543b2a446d7e76f5e1cbdd0258893b7 [file] [log] [blame]
Sho SHIMIZU02e6e202016-05-18 10:47:47 -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
Sho SHIMIZU68e8bfa2016-06-08 12:01:31 -070018import com.google.common.base.MoreObjects;
Sho SHIMIZU65de9612016-05-19 12:24:20 -070019import com.google.common.collect.Sets;
Sho SHIMIZU02e6e202016-05-18 10:47:47 -070020import org.onosproject.net.resource.DiscreteResource;
21import org.onosproject.net.resource.DiscreteResourceId;
Sho SHIMIZU65de9612016-05-19 12:24:20 -070022import org.onosproject.net.resource.Resources;
Sho SHIMIZU02e6e202016-05-18 10:47:47 -070023
Sho SHIMIZU65de9612016-05-19 12:24:20 -070024import java.util.LinkedHashSet;
Sho SHIMIZU65de9612016-05-19 12:24:20 -070025import java.util.Map;
Sho SHIMIZU68e8bfa2016-06-08 12:01:31 -070026import java.util.Objects;
Sho SHIMIZU02e6e202016-05-18 10:47:47 -070027import java.util.Optional;
28import java.util.Set;
Sho SHIMIZU65de9612016-05-19 12:24:20 -070029import java.util.stream.Collectors;
30import java.util.stream.Stream;
Sho SHIMIZU02e6e202016-05-18 10:47:47 -070031
32/**
33 * Represents a set of resources containing resources that can be encoded as integer
34 * and those that can't encoded as integer.
35 */
36final class UnifiedDiscreteResources implements DiscreteResources {
Sho SHIMIZUf503a622016-05-25 11:12:23 -070037 private final DiscreteResources generics;
Sho SHIMIZU65de9612016-05-19 12:24:20 -070038 private final DiscreteResources encodables;
39 private static final Codecs CODECS = Codecs.getInstance();
Sho SHIMIZU02e6e202016-05-18 10:47:47 -070040
Sho SHIMIZU65de9612016-05-19 12:24:20 -070041 static DiscreteResources of(Set<DiscreteResource> resources) {
42 if (resources.isEmpty()) {
43 return DiscreteResources.empty();
44 }
45
46 Map<Boolean, Set<DiscreteResource>> partitioned = resources.stream()
47 .collect(Collectors.partitioningBy(CODECS::isEncodable, Collectors.toCollection(LinkedHashSet::new)));
48 return new UnifiedDiscreteResources(
Sho SHIMIZUf503a622016-05-25 11:12:23 -070049 GenericDiscreteResources.of(partitioned.get(false)),
Sho SHIMIZU65de9612016-05-19 12:24:20 -070050 EncodableDiscreteResources.of(partitioned.get(true))
51 );
Sho SHIMIZU02e6e202016-05-18 10:47:47 -070052 }
53
Sho SHIMIZUf503a622016-05-25 11:12:23 -070054 private UnifiedDiscreteResources(DiscreteResources generics, DiscreteResources encodables) {
55 this.generics = generics;
Sho SHIMIZU65de9612016-05-19 12:24:20 -070056 this.encodables = encodables;
Sho SHIMIZU02e6e202016-05-18 10:47:47 -070057 }
58
59 @Override
60 public Optional<DiscreteResource> lookup(DiscreteResourceId id) {
Sho SHIMIZU65de9612016-05-19 12:24:20 -070061 if (CODECS.isEncodable(Resources.discrete(id).resource())) {
62 return encodables.lookup(id);
63 }
64
Sho SHIMIZUf503a622016-05-25 11:12:23 -070065 return generics.lookup(id);
Sho SHIMIZU02e6e202016-05-18 10:47:47 -070066 }
67
68 @Override
69 public DiscreteResources difference(DiscreteResources other) {
Sho SHIMIZUecb78982016-06-07 16:10:46 -070070 if (other instanceof UnifiedDiscreteResources) {
71 UnifiedDiscreteResources cast = (UnifiedDiscreteResources) other;
72 return new UnifiedDiscreteResources(
73 this.generics.difference(cast.generics),
74 this.encodables.difference(cast.encodables));
75 } else if (other instanceof EmptyDiscreteResources) {
76 return this;
77 }
78
Sho SHIMIZU4fe6b232016-06-07 14:40:08 -070079 return of(Sets.difference(this.values(), other.values()));
Sho SHIMIZU02e6e202016-05-18 10:47:47 -070080 }
81
82 @Override
83 public boolean isEmpty() {
Sho SHIMIZUf503a622016-05-25 11:12:23 -070084 return generics.isEmpty() && encodables.isEmpty();
Sho SHIMIZU02e6e202016-05-18 10:47:47 -070085 }
86
87 @Override
Sho SHIMIZU34847b72016-06-07 14:31:54 -070088 public boolean containsAny(Set<DiscreteResource> other) {
89 Map<Boolean, Set<DiscreteResource>> partitioned = other.stream()
90 .collect(Collectors.partitioningBy(CODECS::isEncodable, Collectors.toCollection(LinkedHashSet::new)));
Sho SHIMIZUf503a622016-05-25 11:12:23 -070091 return generics.containsAny(partitioned.get(false)) || encodables.containsAny(partitioned.get(true));
Sho SHIMIZU02e6e202016-05-18 10:47:47 -070092 }
93
94 @Override
95 public DiscreteResources add(DiscreteResources other) {
Sho SHIMIZU9e3031a2016-06-07 16:32:06 -070096 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
Sho SHIMIZU65de9612016-05-19 12:24:20 -0700105 return of(Sets.union(this.values(), other.values()));
Sho SHIMIZU02e6e202016-05-18 10:47:47 -0700106 }
107
108 @Override
Sho SHIMIZU02e6e202016-05-18 10:47:47 -0700109 public Set<DiscreteResource> values() {
Sho SHIMIZUf503a622016-05-25 11:12:23 -0700110 return Stream.concat(encodables.values().stream(), generics.values().stream())
Sho SHIMIZU65de9612016-05-19 12:24:20 -0700111 .collect(Collectors.toCollection(LinkedHashSet::new));
Sho SHIMIZU02e6e202016-05-18 10:47:47 -0700112 }
Sho SHIMIZU68e8bfa2016-06-08 12:01:31 -0700113
Sho SHIMIZU9cc4a242016-05-26 12:55:35 -0700114 public <T> Set<DiscreteResource> valuesOf(Class<T> cls) {
115 return Stream.concat(encodables.valuesOf(cls).stream(), generics.valuesOf(cls).stream())
116 .collect(Collectors.toCollection(LinkedHashSet::new));
117 }
118
Sho SHIMIZU68e8bfa2016-06-08 12:01:31 -0700119 @Override
120 public int hashCode() {
121 return Objects.hash(generics, encodables);
122 }
123
124 @Override
125 public boolean equals(Object obj) {
126 if (this == obj) {
127 return true;
128 }
129 if (obj == null || getClass() != obj.getClass()) {
130 return false;
131 }
132 final UnifiedDiscreteResources other = (UnifiedDiscreteResources) obj;
133 return Objects.equals(this.generics, other.generics)
134 && Objects.equals(this.encodables, other.encodables);
135 }
136
137 @Override
138 public String toString() {
139 return MoreObjects.toStringHelper(this)
140 .add("values", values())
141 .toString();
142 }
Sho SHIMIZU02e6e202016-05-18 10:47:47 -0700143}