Sho SHIMIZU | 02e6e20 | 2016-05-18 10:47:47 -0700 | [diff] [blame] | 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 | |
Sho SHIMIZU | 68e8bfa | 2016-06-08 12:01:31 -0700 | [diff] [blame] | 18 | import com.google.common.base.MoreObjects; |
Sho SHIMIZU | 65de961 | 2016-05-19 12:24:20 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Sets; |
Sho SHIMIZU | 02e6e20 | 2016-05-18 10:47:47 -0700 | [diff] [blame] | 20 | import org.onosproject.net.resource.DiscreteResource; |
| 21 | import org.onosproject.net.resource.DiscreteResourceId; |
Sho SHIMIZU | 65de961 | 2016-05-19 12:24:20 -0700 | [diff] [blame] | 22 | import org.onosproject.net.resource.Resources; |
Sho SHIMIZU | 02e6e20 | 2016-05-18 10:47:47 -0700 | [diff] [blame] | 23 | |
Sho SHIMIZU | 65de961 | 2016-05-19 12:24:20 -0700 | [diff] [blame] | 24 | import java.util.LinkedHashSet; |
Sho SHIMIZU | 65de961 | 2016-05-19 12:24:20 -0700 | [diff] [blame] | 25 | import java.util.Map; |
Sho SHIMIZU | 68e8bfa | 2016-06-08 12:01:31 -0700 | [diff] [blame] | 26 | import java.util.Objects; |
Sho SHIMIZU | 02e6e20 | 2016-05-18 10:47:47 -0700 | [diff] [blame] | 27 | import java.util.Optional; |
| 28 | import java.util.Set; |
Sho SHIMIZU | 65de961 | 2016-05-19 12:24:20 -0700 | [diff] [blame] | 29 | import java.util.stream.Collectors; |
| 30 | import java.util.stream.Stream; |
Sho SHIMIZU | 02e6e20 | 2016-05-18 10:47:47 -0700 | [diff] [blame] | 31 | |
| 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 | */ |
| 36 | final class UnifiedDiscreteResources implements DiscreteResources { |
Sho SHIMIZU | f503a62 | 2016-05-25 11:12:23 -0700 | [diff] [blame] | 37 | private final DiscreteResources generics; |
Sho SHIMIZU | 65de961 | 2016-05-19 12:24:20 -0700 | [diff] [blame] | 38 | private final DiscreteResources encodables; |
| 39 | private static final Codecs CODECS = Codecs.getInstance(); |
Sho SHIMIZU | 02e6e20 | 2016-05-18 10:47:47 -0700 | [diff] [blame] | 40 | |
Sho SHIMIZU | 65de961 | 2016-05-19 12:24:20 -0700 | [diff] [blame] | 41 | 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 SHIMIZU | f503a62 | 2016-05-25 11:12:23 -0700 | [diff] [blame] | 49 | GenericDiscreteResources.of(partitioned.get(false)), |
Sho SHIMIZU | 65de961 | 2016-05-19 12:24:20 -0700 | [diff] [blame] | 50 | EncodableDiscreteResources.of(partitioned.get(true)) |
| 51 | ); |
Sho SHIMIZU | 02e6e20 | 2016-05-18 10:47:47 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Sho SHIMIZU | f503a62 | 2016-05-25 11:12:23 -0700 | [diff] [blame] | 54 | private UnifiedDiscreteResources(DiscreteResources generics, DiscreteResources encodables) { |
| 55 | this.generics = generics; |
Sho SHIMIZU | 65de961 | 2016-05-19 12:24:20 -0700 | [diff] [blame] | 56 | this.encodables = encodables; |
Sho SHIMIZU | 02e6e20 | 2016-05-18 10:47:47 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public Optional<DiscreteResource> lookup(DiscreteResourceId id) { |
Sho SHIMIZU | 65de961 | 2016-05-19 12:24:20 -0700 | [diff] [blame] | 61 | if (CODECS.isEncodable(Resources.discrete(id).resource())) { |
| 62 | return encodables.lookup(id); |
| 63 | } |
| 64 | |
Sho SHIMIZU | f503a62 | 2016-05-25 11:12:23 -0700 | [diff] [blame] | 65 | return generics.lookup(id); |
Sho SHIMIZU | 02e6e20 | 2016-05-18 10:47:47 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public DiscreteResources difference(DiscreteResources other) { |
Sho SHIMIZU | ecb7898 | 2016-06-07 16:10:46 -0700 | [diff] [blame] | 70 | 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 SHIMIZU | 4fe6b23 | 2016-06-07 14:40:08 -0700 | [diff] [blame] | 79 | return of(Sets.difference(this.values(), other.values())); |
Sho SHIMIZU | 02e6e20 | 2016-05-18 10:47:47 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public boolean isEmpty() { |
Sho SHIMIZU | f503a62 | 2016-05-25 11:12:23 -0700 | [diff] [blame] | 84 | return generics.isEmpty() && encodables.isEmpty(); |
Sho SHIMIZU | 02e6e20 | 2016-05-18 10:47:47 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
Sho SHIMIZU | 34847b7 | 2016-06-07 14:31:54 -0700 | [diff] [blame] | 88 | 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 SHIMIZU | f503a62 | 2016-05-25 11:12:23 -0700 | [diff] [blame] | 91 | return generics.containsAny(partitioned.get(false)) || encodables.containsAny(partitioned.get(true)); |
Sho SHIMIZU | 02e6e20 | 2016-05-18 10:47:47 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public DiscreteResources add(DiscreteResources other) { |
Sho SHIMIZU | 9e3031a | 2016-06-07 16:32:06 -0700 | [diff] [blame] | 96 | 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 SHIMIZU | 65de961 | 2016-05-19 12:24:20 -0700 | [diff] [blame] | 105 | return of(Sets.union(this.values(), other.values())); |
Sho SHIMIZU | 02e6e20 | 2016-05-18 10:47:47 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Override |
Sho SHIMIZU | 02e6e20 | 2016-05-18 10:47:47 -0700 | [diff] [blame] | 109 | public Set<DiscreteResource> values() { |
Sho SHIMIZU | f503a62 | 2016-05-25 11:12:23 -0700 | [diff] [blame] | 110 | return Stream.concat(encodables.values().stream(), generics.values().stream()) |
Sho SHIMIZU | 65de961 | 2016-05-19 12:24:20 -0700 | [diff] [blame] | 111 | .collect(Collectors.toCollection(LinkedHashSet::new)); |
Sho SHIMIZU | 02e6e20 | 2016-05-18 10:47:47 -0700 | [diff] [blame] | 112 | } |
Sho SHIMIZU | 68e8bfa | 2016-06-08 12:01:31 -0700 | [diff] [blame] | 113 | |
Sho SHIMIZU | 9cc4a24 | 2016-05-26 12:55:35 -0700 | [diff] [blame] | 114 | 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 SHIMIZU | 68e8bfa | 2016-06-08 12:01:31 -0700 | [diff] [blame] | 119 | @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 SHIMIZU | 02e6e20 | 2016-05-18 10:47:47 -0700 | [diff] [blame] | 143 | } |