blob: e56aa17017b5838ec8a0a933fa2e2800c8ebd764 [file] [log] [blame]
FrankWang2674e452018-05-24 17:13:35 +08001/*
2 * Copyright 2018-present Open Networking Foundation
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
17package org.onosproject.net.pi.runtime;
18
19import com.google.common.annotations.Beta;
20import com.google.common.base.Objects;
21import org.onosproject.net.pi.model.PiRegisterId;
22
23import static com.google.common.base.Preconditions.checkArgument;
24import static com.google.common.base.Preconditions.checkNotNull;
25
26/**
27 * Identifier of a register cell in a protocol-independent pipeline.
28 */
29@Beta
30public final class PiRegisterCellId {
31
32 private final PiRegisterId registerId;
33 private final long index;
34
35 private PiRegisterCellId(PiRegisterId registerId, long index) {
36 this.registerId = registerId;
37 this.index = index;
38 }
39
40 /**
41 * Returns the identifier of the register instance where this cell is
42 * contained.
43 *
44 * @return register identifier
45 */
46 public PiRegisterId registerId() {
47 return registerId;
48 }
49
50 /**
51 * Returns the register index to which this cell ID is associated.
52 *
53 * @return register index
54 */
55 public long index() {
56 return index;
57 }
58
59 /**
60 * Return a register cell ID for the given register ID and index.
61 *
62 * @param registerId register ID
63 * @param index index
64 * @return register cell ID
65 */
66 public static PiRegisterCellId of(PiRegisterId registerId, long index) {
67 checkNotNull(registerId);
68 checkArgument(index >= 0, "Index must be a positive number");
69 return new PiRegisterCellId(registerId, index);
70 }
71
72 @Override
73 public boolean equals(Object obj) {
74 if (this == obj) {
75 return true;
76 }
77 if (obj == null || getClass() != obj.getClass()) {
78 return false;
79 }
80 final PiRegisterCellId other = (PiRegisterCellId) obj;
81 return Objects.equal(this.registerId, other.registerId)
82 && Objects.equal(this.index, other.index);
83 }
84
85 @Override
86 public int hashCode() {
87 return Objects.hashCode(registerId, index);
88 }
89
90 @Override
91 public String toString() {
92 return registerId.toString() + ':' + String.valueOf(index);
93 }
94}