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 | |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 21 | import java.util.List; |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 22 | import java.util.Objects; |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 23 | import java.util.Optional; |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * Represents a resource path which specifies a resource which can be measured |
| 27 | * as continuous value. Bandwidth of a link is an example of the resource. |
| 28 | * <p> |
| 29 | * Note: This class is exposed to the public, but intended to be used in the resource API |
| 30 | * implementation only. It is not for resource API user. |
| 31 | */ |
| 32 | @Beta |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 33 | public final class ContinuousResource implements Resource { |
| 34 | private final ContinuousResourceId id; |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 35 | private final double value; |
| 36 | |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 37 | ContinuousResource(ContinuousResourceId id, double value) { |
| 38 | this.id = id; |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 39 | this.value = value; |
| 40 | } |
| 41 | |
Sho SHIMIZU | 7f8a7cb | 2016-01-27 15:09:13 -0800 | [diff] [blame] | 42 | // for serializer |
| 43 | ContinuousResource() { |
| 44 | this.id = null; |
| 45 | this.value = 0; |
| 46 | } |
| 47 | |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 48 | @Override |
| 49 | public ContinuousResourceId id() { |
| 50 | return id; |
| 51 | } |
| 52 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 53 | /** |
| 54 | * The user of this methods must receive the return value as Double or double. |
| 55 | * Otherwise, this methods throws an exception. |
| 56 | * |
| 57 | * @param <T> type of the return value |
| 58 | * @return the volume of this resource |
| 59 | */ |
| 60 | @SuppressWarnings("unchecked") |
| 61 | @Override |
| 62 | public <T> T volume() { |
| 63 | return (T) Double.valueOf(value); |
| 64 | } |
| 65 | |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 66 | /** |
| 67 | * Returns the value of the resource amount. |
| 68 | * |
| 69 | * @return the value of the resource amount |
| 70 | */ |
| 71 | // FIXME: overlapping a purpose with volume() |
| 72 | public double value() { |
| 73 | return value; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public List<Object> components() { |
Sho SHIMIZU | 42ac51f | 2016-01-27 12:59:31 -0800 | [diff] [blame] | 78 | return id.components(); |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public Object last() { |
Sho SHIMIZU | 42ac51f | 2016-01-27 12:59:31 -0800 | [diff] [blame] | 83 | if (id.components().isEmpty()) { |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 84 | return null; |
| 85 | } |
Sho SHIMIZU | 42ac51f | 2016-01-27 12:59:31 -0800 | [diff] [blame] | 86 | return id.components().get(id.components().size() - 1); |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public DiscreteResource child(Object child) { |
| 91 | throw new UnsupportedOperationException(); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public ContinuousResource child(Class<?> child, double value) { |
| 96 | throw new UnsupportedOperationException(); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public Optional<DiscreteResource> parent() { |
Sho SHIMIZU | f95b96e | 2016-01-25 19:35:15 -0800 | [diff] [blame^] | 101 | return id.parent().map(x -> Resource.discrete(x).resource()); |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 104 | @Override |
| 105 | public int hashCode() { |
Sho SHIMIZU | 34b55b6 | 2016-01-27 12:49:43 -0800 | [diff] [blame] | 106 | return Objects.hash(id, value); |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public boolean equals(Object obj) { |
| 111 | if (this == obj) { |
| 112 | return true; |
| 113 | } |
| 114 | if (obj == null || getClass() != obj.getClass()) { |
| 115 | return false; |
| 116 | } |
| 117 | final ContinuousResource other = (ContinuousResource) obj; |
Sho SHIMIZU | 34b55b6 | 2016-01-27 12:49:43 -0800 | [diff] [blame] | 118 | return Objects.equals(this.id, other.id) |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 119 | && Objects.equals(this.value, other.value); |
| 120 | } |
| 121 | |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 122 | @Override |
| 123 | public String toString() { |
| 124 | return MoreObjects.toStringHelper(this) |
| 125 | .add("id", id) |
| 126 | .add("volume", value) |
| 127 | .toString(); |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 128 | } |
| 129 | } |