Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [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 | */ |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 16 | package org.onosproject.net.resource; |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 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 | |
Sho SHIMIZU | caa2f7b | 2016-02-12 17:38:49 -0800 | [diff] [blame] | 60 | return id.isTypeOf(type); |
Sho SHIMIZU | 003ed32 | 2016-02-11 12:58:42 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 63 | /** |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 64 | * Returns the value of the resource amount. |
| 65 | * |
| 66 | * @return the value of the resource amount |
| 67 | */ |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 68 | public double value() { |
| 69 | return value; |
| 70 | } |
| 71 | |
| 72 | @Override |
Sho SHIMIZU | b08d586 | 2016-02-11 12:37:28 -0800 | [diff] [blame] | 73 | public boolean isSubTypeOf(Class<?> ancestor) { |
Sho SHIMIZU | 0e1a476 | 2016-02-11 13:04:41 -0800 | [diff] [blame] | 74 | checkNotNull(ancestor); |
| 75 | |
Sho SHIMIZU | caa2f7b | 2016-02-12 17:38:49 -0800 | [diff] [blame] | 76 | return id.isSubTypeOf(ancestor); |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Sho SHIMIZU | f08cb4c | 2016-02-11 18:35:59 -0800 | [diff] [blame] | 79 | /** |
| 80 | * {@inheritDoc} |
| 81 | * |
| 82 | * A user must specify Double.class or double.class to avoid an empty value. |
| 83 | */ |
| 84 | @Override |
| 85 | public <T> Optional<T> valueAs(Class<T> type) { |
| 86 | checkNotNull(type); |
| 87 | |
| 88 | if (type == Object.class || type == double.class || type == Double.class) { |
| 89 | @SuppressWarnings("unchecked") |
| 90 | T value = (T) Double.valueOf(this.value); |
| 91 | return Optional.of(value); |
| 92 | } |
| 93 | |
| 94 | return Optional.empty(); |
| 95 | } |
| 96 | |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 97 | @Override |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 98 | public DiscreteResource child(Object child) { |
| 99 | throw new UnsupportedOperationException(); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public ContinuousResource child(Class<?> child, double value) { |
| 104 | throw new UnsupportedOperationException(); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public Optional<DiscreteResource> parent() { |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 109 | return id.parent().map(x -> Resources.discrete(x).resource()); |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 112 | @Override |
| 113 | public int hashCode() { |
Sho SHIMIZU | 34b55b6 | 2016-01-27 12:49:43 -0800 | [diff] [blame] | 114 | return Objects.hash(id, value); |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public boolean equals(Object obj) { |
| 119 | if (this == obj) { |
| 120 | return true; |
| 121 | } |
| 122 | if (obj == null || getClass() != obj.getClass()) { |
| 123 | return false; |
| 124 | } |
| 125 | final ContinuousResource other = (ContinuousResource) obj; |
Sho SHIMIZU | 34b55b6 | 2016-01-27 12:49:43 -0800 | [diff] [blame] | 126 | return Objects.equals(this.id, other.id) |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 127 | && Objects.equals(this.value, other.value); |
| 128 | } |
| 129 | |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 130 | @Override |
| 131 | public String toString() { |
| 132 | return MoreObjects.toStringHelper(this) |
| 133 | .add("id", id) |
Sho SHIMIZU | f08cb4c | 2016-02-11 18:35:59 -0800 | [diff] [blame] | 134 | .add("value", value) |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 135 | .toString(); |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 136 | } |
| 137 | } |