Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | */ |
| 16 | package org.onosproject.net.group; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | |
| 20 | import java.util.Arrays; |
| 21 | |
| 22 | /** |
| 23 | * Default implementation of group key interface. |
| 24 | */ |
| 25 | public class DefaultGroupKey implements GroupKey { |
| 26 | |
| 27 | private final byte[] key; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 28 | protected static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray(); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 29 | |
| 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 Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 56 | /** |
| 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 Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 72 | } |