blob: 7ed8847d718d0ef0eff167c1b8dbde74e1c5d80b [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 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 */
Brian O'Connor6de2e202015-05-21 14:30:41 -070016package org.onosproject.net.resource.link;
Toshio Koidec9051db2014-10-20 15:18:37 -070017
Sho SHIMIZU4fea4fd2015-05-07 11:50:23 -070018import org.onosproject.net.IndexedLambda;
19
Toshio Koidec9051db2014-10-20 15:18:37 -070020import java.util.Objects;
21
Sho SHIMIZU4fea4fd2015-05-07 11:50:23 -070022import static com.google.common.base.Preconditions.checkNotNull;
23
Toshio Koidec9051db2014-10-20 15:18:37 -070024/**
25 * Representation of lambda resource.
26 */
Sho SHIMIZU94b7ff42015-05-06 17:51:49 -070027public final class LambdaResource extends LinkResource {
Toshio Koidec9051db2014-10-20 15:18:37 -070028
Sho SHIMIZU4fea4fd2015-05-07 11:50:23 -070029 private final IndexedLambda lambda;
Toshio Koidec9051db2014-10-20 15:18:37 -070030
31 /**
32 * Creates a new instance with given lambda.
33 *
Sho SHIMIZU4fea4fd2015-05-07 11:50:23 -070034 * @param lambda lambda to be assigned
Toshio Koidec9051db2014-10-20 15:18:37 -070035 */
Sho SHIMIZU4fea4fd2015-05-07 11:50:23 -070036 private LambdaResource(IndexedLambda lambda) {
37 this.lambda = checkNotNull(lambda);
Toshio Koidec9051db2014-10-20 15:18:37 -070038 }
39
Thomas Vachuska7d0032b2014-11-04 17:39:57 -080040 // Constructor for serialization
Sho SHIMIZU94b7ff42015-05-06 17:51:49 -070041 private LambdaResource() {
Sho SHIMIZU4fea4fd2015-05-07 11:50:23 -070042 this.lambda = null;
Thomas Vachuska7d0032b2014-11-04 17:39:57 -080043 }
44
Toshio Koidec9051db2014-10-20 15:18:37 -070045 /**
Sho SHIMIZU4fea4fd2015-05-07 11:50:23 -070046 * Creates a new instance with the given index of lambda.
Toshio Koidec9051db2014-10-20 15:18:37 -070047 *
Sho SHIMIZU4fea4fd2015-05-07 11:50:23 -070048 * @param lambda index value of lambda to be assigned
49 * @return {@link LambdaResource} instance with the given lambda
Toshio Koidec9051db2014-10-20 15:18:37 -070050 */
Sho SHIMIZU94b7ff42015-05-06 17:51:49 -070051 public static LambdaResource valueOf(int lambda) {
Sho SHIMIZU4fea4fd2015-05-07 11:50:23 -070052 return valueOf(new IndexedLambda(lambda));
53 }
54
55 /**
56 * Creates a new instance with the given lambda.
57 *
58 * @param lambda lambda to be assigned
59 * @return {@link LambdaResource} instance with the given lambda
60 */
61 public static LambdaResource valueOf(IndexedLambda lambda) {
Sho SHIMIZU94b7ff42015-05-06 17:51:49 -070062 return new LambdaResource(lambda);
Toshio Koidec9051db2014-10-20 15:18:37 -070063 }
64
65 /**
66 * Returns lambda as an int value.
Toshio Koide485b4782014-10-20 19:34:21 -070067 *
Toshio Koidec9051db2014-10-20 15:18:37 -070068 * @return lambda as an int value
69 */
70 public int toInt() {
Sho SHIMIZU4fea4fd2015-05-07 11:50:23 -070071 return (int) lambda.index();
Toshio Koidec9051db2014-10-20 15:18:37 -070072 }
73
74 @Override
75 public boolean equals(Object obj) {
Sho SHIMIZU94b7ff42015-05-06 17:51:49 -070076 if (obj instanceof LambdaResource) {
77 LambdaResource that = (LambdaResource) obj;
Toshio Koidec9051db2014-10-20 15:18:37 -070078 return Objects.equals(this.lambda, that.lambda);
79 }
80 return false;
81 }
82
83 @Override
84 public int hashCode() {
Sho SHIMIZU4fea4fd2015-05-07 11:50:23 -070085 return lambda.hashCode();
Toshio Koidec9051db2014-10-20 15:18:37 -070086 }
87
88 @Override
89 public String toString() {
90 return String.valueOf(this.lambda);
91 }
92
93}