blob: 1959a5969b265f99288b57e1dbd920115484c07c [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 SHIMIZU4fe6b232016-06-07 14:40:08 -070070 return of(Sets.difference(this.values(), other.values()));
Sho SHIMIZU02e6e202016-05-18 10:47:47 -070071 }
72
73 @Override
74 public boolean isEmpty() {
Sho SHIMIZUf503a622016-05-25 11:12:23 -070075 return generics.isEmpty() && encodables.isEmpty();
Sho SHIMIZU02e6e202016-05-18 10:47:47 -070076 }
77
78 @Override
Sho SHIMIZU34847b72016-06-07 14:31:54 -070079 public boolean containsAny(Set<DiscreteResource> other) {
80 Map<Boolean, Set<DiscreteResource>> partitioned = other.stream()
81 .collect(Collectors.partitioningBy(CODECS::isEncodable, Collectors.toCollection(LinkedHashSet::new)));
Sho SHIMIZUf503a622016-05-25 11:12:23 -070082 return generics.containsAny(partitioned.get(false)) || encodables.containsAny(partitioned.get(true));
Sho SHIMIZU02e6e202016-05-18 10:47:47 -070083 }
84
85 @Override
86 public DiscreteResources add(DiscreteResources other) {
Sho SHIMIZU65de9612016-05-19 12:24:20 -070087 return of(Sets.union(this.values(), other.values()));
Sho SHIMIZU02e6e202016-05-18 10:47:47 -070088 }
89
90 @Override
Sho SHIMIZU34847b72016-06-07 14:31:54 -070091 public DiscreteResources remove(Set<DiscreteResource> removed) {
92 return of(Sets.difference(values(), removed));
Sho SHIMIZU02e6e202016-05-18 10:47:47 -070093 }
94
95 @Override
96 public Set<DiscreteResource> values() {
Sho SHIMIZUf503a622016-05-25 11:12:23 -070097 return Stream.concat(encodables.values().stream(), generics.values().stream())
Sho SHIMIZU65de9612016-05-19 12:24:20 -070098 .collect(Collectors.toCollection(LinkedHashSet::new));
Sho SHIMIZU02e6e202016-05-18 10:47:47 -070099 }
Sho SHIMIZU68e8bfa2016-06-08 12:01:31 -0700100
101 @Override
102 public int hashCode() {
103 return Objects.hash(generics, encodables);
104 }
105
106 @Override
107 public boolean equals(Object obj) {
108 if (this == obj) {
109 return true;
110 }
111 if (obj == null || getClass() != obj.getClass()) {
112 return false;
113 }
114 final UnifiedDiscreteResources other = (UnifiedDiscreteResources) obj;
115 return Objects.equals(this.generics, other.generics)
116 && Objects.equals(this.encodables, other.encodables);
117 }
118
119 @Override
120 public String toString() {
121 return MoreObjects.toStringHelper(this)
122 .add("values", values())
123 .toString();
124 }
Sho SHIMIZU02e6e202016-05-18 10:47:47 -0700125}