FrankWang | ea70de3 | 2018-05-08 15:41:25 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.net.pi.runtime.data; |
| 18 | |
| 19 | import com.google.common.annotations.Beta; |
| 20 | import com.google.common.base.Objects; |
| 21 | import com.google.common.collect.ImmutableList; |
| 22 | import org.onosproject.net.pi.model.PiData; |
| 23 | |
| 24 | import java.util.List; |
| 25 | import java.util.StringJoiner; |
| 26 | |
| 27 | /** |
| 28 | * Tuple entity in a protocol-independent pipeline. |
| 29 | */ |
| 30 | @Beta |
| 31 | public final class PiTuple implements PiData { |
| 32 | private final ImmutableList<PiData> tuple; |
| 33 | |
| 34 | /** |
| 35 | * Creates a new protocol-independent tuple instance for the given collection of PiData. |
| 36 | * |
| 37 | * @param tuple the collection of PiData |
| 38 | */ |
| 39 | private PiTuple(List<PiData> tuple) { |
| 40 | this.tuple = ImmutableList.copyOf(tuple); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Returns a new protocol-independent tuple. |
| 45 | * @param tuple the list of PiData |
| 46 | * @return tuple |
| 47 | */ |
| 48 | public static PiTuple of(List<PiData> tuple) { |
| 49 | return new PiTuple(tuple); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Return protocol-independent tuple instance. |
| 54 | * |
| 55 | * @return the list of PiData |
| 56 | */ |
| 57 | public List<PiData> tuple() { |
| 58 | return this.tuple; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public Type type() { |
| 63 | return Type.TUPLE; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public boolean equals(Object o) { |
| 68 | if (this == o) { |
| 69 | return true; |
| 70 | } |
| 71 | if (o == null || getClass() != o.getClass()) { |
| 72 | return false; |
| 73 | } |
| 74 | PiTuple tp = (PiTuple) o; |
| 75 | return Objects.equal(tuple, tp.tuple); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public int hashCode() { |
| 80 | return Objects.hashCode(tuple); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public String toString() { |
| 85 | StringJoiner stringParams = new StringJoiner(", ", "(", ")"); |
| 86 | this.tuple().forEach(p -> stringParams.add(p.toString())); |
| 87 | return stringParams.toString(); |
| 88 | } |
| 89 | } |