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; |
| 19 | |
| 20 | import java.util.Objects; |
| 21 | |
| 22 | /** |
| 23 | * Represents a resource path which specifies a resource which can be measured |
| 24 | * as a discrete unit. A VLAN ID and a MPLS label of a link are examples of the resource. |
| 25 | * <p> |
| 26 | * Note: This class is exposed to the public, but intended to be used in the resource API |
| 27 | * implementation only. It is not for resource API user. |
| 28 | * </p> |
| 29 | */ |
| 30 | @Beta |
| 31 | // TODO: consider how to restrict the visibility |
| 32 | public final class DiscreteResource extends Resource { |
| 33 | protected DiscreteResource() { |
| 34 | super(); |
| 35 | } |
| 36 | |
| 37 | DiscreteResource(ResourceId id) { |
| 38 | super(id); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * The user of this methods must receive the return value as the correct type. |
| 43 | * Otherwise, this methods throws an exception. |
| 44 | * |
| 45 | * @param <T> type of the return value |
| 46 | * @return the volume of this resource |
| 47 | */ |
| 48 | @SuppressWarnings("unchecked") |
| 49 | @Override |
| 50 | // TODO: consider receiving Class<T> as an argument. Which approach is convenient? |
| 51 | public <T> T volume() { |
| 52 | return (T) last(); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public int hashCode() { |
| 57 | // the value returing from volume() is excluded due to optimization |
| 58 | return id().hashCode(); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public boolean equals(Object obj) { |
| 63 | if (this == obj) { |
| 64 | return true; |
| 65 | } |
| 66 | if (obj == null || getClass() != obj.getClass()) { |
| 67 | return false; |
| 68 | } |
| 69 | final DiscreteResource other = (DiscreteResource) obj; |
| 70 | // the value returing from volume() is excluded due to optimization |
| 71 | return Objects.equals(this.id(), other.id()); |
| 72 | } |
| 73 | } |