blob: 10c6ad1cfa657c1c65a025f04b0efd5961f04244 [file] [log] [blame]
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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 {
26
27 private final byte[] key;
Saurav Das8a0732e2015-11-20 15:27:53 -080028 protected static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070029
30 public DefaultGroupKey(byte[] key) {
31 this.key = checkNotNull(key);
32 }
33
34 @Override
35 public byte[] key() {
36 return key;
37 }
38
39 @Override
40 public boolean equals(Object o) {
41 if (this == o) {
42 return true;
43 }
44 if (!(o instanceof DefaultGroupKey)) {
45 return false;
46 }
47 DefaultGroupKey that = (DefaultGroupKey) o;
48 return (Arrays.equals(this.key, that.key));
49 }
50
51 @Override
52 public int hashCode() {
53 return Arrays.hashCode(this.key);
54 }
55
Saurav Das8a0732e2015-11-20 15:27:53 -080056 /**
57 * Returns a hex string representation of the byte array that is used
58 * as a group key. This solution was adapted from
59 * http://stackoverflow.com/questions/9655181/
60 */
61 @Override
62 public String toString() {
63 char[] hexChars = new char[key.length * 2];
64 for (int j = 0; j < key.length; j++) {
65 int v = key[j] & 0xFF;
66 hexChars[j * 2] = HEX_ARRAY[v >>> 4];
67 hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
68 }
69 return "GroupKey:0x" + new String(hexChars);
70 }
71
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070072}