blob: 122d1598e344a879e8fa4bec9ac6a4f8da481295 [file] [log] [blame]
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -07003 *
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 */
16package org.onosproject.net.group;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19
20import java.util.Arrays;
21
22/**
23 * Default implementation of group key interface.
24 */
25public class DefaultGroupKey implements GroupKey {
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070026 private final byte[] key;
Ray Milkey2e27ec52018-01-31 17:21:40 -080027 private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070028
29 public DefaultGroupKey(byte[] key) {
30 this.key = checkNotNull(key);
31 }
32
33 @Override
34 public byte[] key() {
35 return key;
36 }
37
38 @Override
39 public boolean equals(Object o) {
40 if (this == o) {
41 return true;
42 }
43 if (!(o instanceof DefaultGroupKey)) {
44 return false;
45 }
46 DefaultGroupKey that = (DefaultGroupKey) o;
47 return (Arrays.equals(this.key, that.key));
48 }
49
50 @Override
51 public int hashCode() {
52 return Arrays.hashCode(this.key);
53 }
54
Saurav Das8a0732e2015-11-20 15:27:53 -080055 /**
56 * Returns a hex string representation of the byte array that is used
57 * as a group key. This solution was adapted from
58 * http://stackoverflow.com/questions/9655181/
59 */
60 @Override
61 public String toString() {
62 char[] hexChars = new char[key.length * 2];
63 for (int j = 0; j < key.length; j++) {
64 int v = key[j] & 0xFF;
65 hexChars[j * 2] = HEX_ARRAY[v >>> 4];
66 hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
67 }
Charles Chanb3ef1fd2016-05-12 20:49:39 -070068 return "0x" + new String(hexChars);
Saurav Das8a0732e2015-11-20 15:27:53 -080069 }
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070070}