Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 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 { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 26 | private final byte[] key; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 27 | protected static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray(); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 28 | |
| 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 Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 55 | /** |
| 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 Chan | b3ef1fd | 2016-05-12 20:49:39 -0700 | [diff] [blame] | 68 | return "0x" + new String(hexChars); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 69 | } |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 70 | } |