Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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.net.newresource; |
| 17 | |
| 18 | import com.google.common.annotations.Beta; |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 19 | import com.google.common.base.MoreObjects; |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 20 | |
| 21 | import java.util.Objects; |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 22 | import java.util.Optional; |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 23 | |
Sho SHIMIZU | 0e1a476 | 2016-02-11 13:04:41 -0800 | [diff] [blame] | 24 | import static com.google.common.base.Preconditions.checkNotNull; |
| 25 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 26 | /** |
| 27 | * Represents a resource path which specifies a resource which can be measured |
| 28 | * as continuous value. Bandwidth of a link is an example of the resource. |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 29 | */ |
| 30 | @Beta |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 31 | public final class ContinuousResource implements Resource { |
| 32 | private final ContinuousResourceId id; |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 33 | private final double value; |
| 34 | |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 35 | ContinuousResource(ContinuousResourceId id, double value) { |
| 36 | this.id = id; |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 37 | this.value = value; |
| 38 | } |
| 39 | |
Sho SHIMIZU | 7f8a7cb | 2016-01-27 15:09:13 -0800 | [diff] [blame] | 40 | // for serializer |
| 41 | ContinuousResource() { |
| 42 | this.id = null; |
| 43 | this.value = 0; |
| 44 | } |
| 45 | |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 46 | @Override |
| 47 | public ContinuousResourceId id() { |
| 48 | return id; |
| 49 | } |
| 50 | |
Sho SHIMIZU | 003ed32 | 2016-02-11 12:58:42 -0800 | [diff] [blame] | 51 | @Override |
Sho SHIMIZU | 74ac901 | 2016-02-12 10:03:21 -0800 | [diff] [blame] | 52 | public String simpleTypeName() { |
| 53 | return id.simpleTypeName(); |
| 54 | } |
| 55 | |
| 56 | @Override |
Sho SHIMIZU | 003ed32 | 2016-02-11 12:58:42 -0800 | [diff] [blame] | 57 | public boolean isTypeOf(Class<?> type) { |
| 58 | checkNotNull(type); |
| 59 | |
| 60 | String typeName = (String) id.components().get(id.components().size() - 1); |
| 61 | return typeName.equals(type.getCanonicalName()); |
| 62 | } |
| 63 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 64 | /** |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 65 | * Returns the value of the resource amount. |
| 66 | * |
| 67 | * @return the value of the resource amount |
| 68 | */ |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 69 | public double value() { |
| 70 | return value; |
| 71 | } |
| 72 | |
| 73 | @Override |
Sho SHIMIZU | b08d586 | 2016-02-11 12:37:28 -0800 | [diff] [blame] | 74 | public boolean isSubTypeOf(Class<?> ancestor) { |
Sho SHIMIZU | 0e1a476 | 2016-02-11 13:04:41 -0800 | [diff] [blame] | 75 | checkNotNull(ancestor); |
| 76 | |
Sho SHIMIZU | 9c02f3a | 2016-01-29 15:11:59 -0800 | [diff] [blame] | 77 | String typeName = (String) id.components().get(id.components().size() - 1); |
Sho SHIMIZU | b08d586 | 2016-02-11 12:37:28 -0800 | [diff] [blame] | 78 | boolean foundInLeaf = typeName.equals(ancestor.getCanonicalName()); |
Sho SHIMIZU | 9c02f3a | 2016-01-29 15:11:59 -0800 | [diff] [blame] | 79 | boolean foundInAncestor = id.components().subList(0, id.components().size()).stream() |
| 80 | .map(Object::getClass) |
Sho SHIMIZU | b08d586 | 2016-02-11 12:37:28 -0800 | [diff] [blame] | 81 | .filter(x -> x.equals(ancestor)) |
Sho SHIMIZU | 9c02f3a | 2016-01-29 15:11:59 -0800 | [diff] [blame] | 82 | .findAny() |
| 83 | .isPresent(); |
| 84 | return foundInAncestor || foundInLeaf; |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Sho SHIMIZU | f08cb4c | 2016-02-11 18:35:59 -0800 | [diff] [blame] | 87 | /** |
| 88 | * {@inheritDoc} |
| 89 | * |
| 90 | * A user must specify Double.class or double.class to avoid an empty value. |
| 91 | */ |
| 92 | @Override |
| 93 | public <T> Optional<T> valueAs(Class<T> type) { |
| 94 | checkNotNull(type); |
| 95 | |
| 96 | if (type == Object.class || type == double.class || type == Double.class) { |
| 97 | @SuppressWarnings("unchecked") |
| 98 | T value = (T) Double.valueOf(this.value); |
| 99 | return Optional.of(value); |
| 100 | } |
| 101 | |
| 102 | return Optional.empty(); |
| 103 | } |
| 104 | |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 105 | @Override |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 106 | public DiscreteResource child(Object child) { |
| 107 | throw new UnsupportedOperationException(); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public ContinuousResource child(Class<?> child, double value) { |
| 112 | throw new UnsupportedOperationException(); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public Optional<DiscreteResource> parent() { |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 117 | return id.parent().map(x -> Resources.discrete(x).resource()); |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 120 | @Override |
| 121 | public int hashCode() { |
Sho SHIMIZU | 34b55b6 | 2016-01-27 12:49:43 -0800 | [diff] [blame] | 122 | return Objects.hash(id, value); |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public boolean equals(Object obj) { |
| 127 | if (this == obj) { |
| 128 | return true; |
| 129 | } |
| 130 | if (obj == null || getClass() != obj.getClass()) { |
| 131 | return false; |
| 132 | } |
| 133 | final ContinuousResource other = (ContinuousResource) obj; |
Sho SHIMIZU | 34b55b6 | 2016-01-27 12:49:43 -0800 | [diff] [blame] | 134 | return Objects.equals(this.id, other.id) |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 135 | && Objects.equals(this.value, other.value); |
| 136 | } |
| 137 | |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 138 | @Override |
| 139 | public String toString() { |
| 140 | return MoreObjects.toStringHelper(this) |
| 141 | .add("id", id) |
Sho SHIMIZU | f08cb4c | 2016-02-11 18:35:59 -0800 | [diff] [blame] | 142 | .add("value", value) |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 143 | .toString(); |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 144 | } |
| 145 | } |