Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame] | 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 | 758cf63 | 2016-05-25 15:25:59 -0700 | [diff] [blame] | 19 | import com.google.common.collect.DiscreteDomain; |
| 20 | import com.google.common.collect.Range; |
| 21 | import com.google.common.collect.RangeSet; |
| 22 | import com.google.common.collect.TreeRangeSet; |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame] | 23 | import org.onlab.util.Tools; |
| 24 | import org.onosproject.net.resource.DiscreteResource; |
| 25 | import org.onosproject.net.resource.DiscreteResourceCodec; |
| 26 | import org.onosproject.net.resource.DiscreteResourceId; |
| 27 | import org.onosproject.net.resource.Resources; |
| 28 | |
| 29 | import java.util.LinkedHashSet; |
Sho SHIMIZU | 68e8bfa | 2016-06-08 12:01:31 -0700 | [diff] [blame] | 30 | import java.util.Objects; |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame] | 31 | import java.util.Set; |
| 32 | import java.util.stream.Collectors; |
Sho SHIMIZU | 758cf63 | 2016-05-25 15:25:59 -0700 | [diff] [blame] | 33 | import java.util.stream.IntStream; |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame] | 34 | |
Sho SHIMIZU | b42d383 | 2016-06-09 14:21:17 -0700 | [diff] [blame] | 35 | import static com.google.common.base.Preconditions.checkArgument; |
| 36 | |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame] | 37 | /** |
| 38 | * Represents discrete resources encoded by a codec. |
| 39 | */ |
| 40 | final class EncodedDiscreteResources { |
Sho SHIMIZU | 758cf63 | 2016-05-25 15:25:59 -0700 | [diff] [blame] | 41 | private final RangeSet<Integer> rangeSet; |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame] | 42 | private final DiscreteResourceCodec codec; |
| 43 | |
Sho SHIMIZU | 758cf63 | 2016-05-25 15:25:59 -0700 | [diff] [blame] | 44 | EncodedDiscreteResources(RangeSet<Integer> rangeSet, DiscreteResourceCodec codec) { |
| 45 | this.rangeSet = rangeSet; |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame] | 46 | this.codec = codec; |
| 47 | } |
| 48 | |
| 49 | static EncodedDiscreteResources of(Set<DiscreteResource> resources, DiscreteResourceCodec codec) { |
Sho SHIMIZU | 758cf63 | 2016-05-25 15:25:59 -0700 | [diff] [blame] | 50 | RangeSet<Integer> rangeSet = TreeRangeSet.create(); |
| 51 | resources.stream() |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame] | 52 | .map(x -> x.valueAs(Object.class)) |
| 53 | .flatMap(Tools::stream) |
| 54 | .map(x -> codec.encode(x)) |
Sho SHIMIZU | 758cf63 | 2016-05-25 15:25:59 -0700 | [diff] [blame] | 55 | .map(Range::singleton) |
| 56 | .map(x -> x.canonical(DiscreteDomain.integers())) |
| 57 | .forEach(rangeSet::add); |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame] | 58 | |
Sho SHIMIZU | 758cf63 | 2016-05-25 15:25:59 -0700 | [diff] [blame] | 59 | return new EncodedDiscreteResources(rangeSet, codec); |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Sho SHIMIZU | 758cf63 | 2016-05-25 15:25:59 -0700 | [diff] [blame] | 62 | RangeSet<Integer> rangeSet() { |
| 63 | return rangeSet; |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | DiscreteResourceCodec codec() { |
| 67 | return codec; |
| 68 | } |
| 69 | |
Sho SHIMIZU | 758cf63 | 2016-05-25 15:25:59 -0700 | [diff] [blame] | 70 | Set<DiscreteResource> values(DiscreteResourceId parent) { |
| 71 | return rangeSet.asRanges().stream() |
| 72 | .flatMapToInt(x1 -> IntStream.range(x1.lowerEndpoint(), x1.upperEndpoint())) |
| 73 | .boxed() |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame] | 74 | .map(x -> codec.decode(x)) |
| 75 | .map(x -> Resources.discrete(parent, x).resource()) |
| 76 | .collect(Collectors.toCollection(LinkedHashSet::new)); |
| 77 | } |
| 78 | |
Sho SHIMIZU | 0e77c50 | 2016-06-13 16:08:37 -0700 | [diff] [blame] | 79 | Class<?> encodedClass() { |
| 80 | Range<Integer> firstRange = rangeSet.asRanges().iterator().next(); |
| 81 | return codec.decode(firstRange.lowerEndpoint()).getClass(); |
| 82 | } |
| 83 | |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame] | 84 | @SuppressWarnings("unchecked") |
| 85 | boolean contains(DiscreteResource resource) { |
Sho SHIMIZU | 13bf46e | 2016-06-01 12:23:15 -0700 | [diff] [blame] | 86 | return resource.valueAs(Object.class) |
| 87 | .map(x -> codec.encode(x)) |
| 88 | .map(rangeSet::contains) |
| 89 | .orElse(false); |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Sho SHIMIZU | b42d383 | 2016-06-09 14:21:17 -0700 | [diff] [blame] | 92 | EncodedDiscreteResources difference(EncodedDiscreteResources other) { |
| 93 | checkArgument(this.codec.getClass() == other.codec.getClass()); |
| 94 | |
| 95 | RangeSet<Integer> newRangeSet = TreeRangeSet.create(this.rangeSet); |
| 96 | newRangeSet.removeAll(other.rangeSet); |
| 97 | |
| 98 | return new EncodedDiscreteResources(newRangeSet, this.codec); |
| 99 | } |
| 100 | |
Sho SHIMIZU | b15b2ea | 2016-06-09 15:35:59 -0700 | [diff] [blame] | 101 | EncodedDiscreteResources add(EncodedDiscreteResources other) { |
| 102 | checkArgument(this.codec.getClass() == other.codec.getClass()); |
| 103 | |
| 104 | RangeSet<Integer> newRangeSet = TreeRangeSet.create(this.rangeSet); |
| 105 | newRangeSet.addAll(other.rangeSet); |
| 106 | |
| 107 | return new EncodedDiscreteResources(newRangeSet, this.codec); |
| 108 | } |
| 109 | |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame] | 110 | boolean isEmpty() { |
Sho SHIMIZU | 758cf63 | 2016-05-25 15:25:59 -0700 | [diff] [blame] | 111 | return rangeSet.isEmpty(); |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame] | 112 | } |
Sho SHIMIZU | 68e8bfa | 2016-06-08 12:01:31 -0700 | [diff] [blame] | 113 | |
| 114 | @Override |
| 115 | public int hashCode() { |
| 116 | return Objects.hash(rangeSet, codec); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public boolean equals(Object obj) { |
| 121 | if (this == obj) { |
| 122 | return true; |
| 123 | } |
| 124 | if (obj == null || getClass() != obj.getClass()) { |
| 125 | return false; |
| 126 | } |
| 127 | final EncodedDiscreteResources other = (EncodedDiscreteResources) obj; |
| 128 | return Objects.equals(this.rangeSet, other.rangeSet) |
| 129 | && Objects.equals(this.codec, other.codec); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public String toString() { |
| 134 | return MoreObjects.toStringHelper(this) |
| 135 | .add("rangeSet", rangeSet) |
| 136 | .add("codec", codec) |
| 137 | .toString(); |
| 138 | } |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame] | 139 | } |