001 /*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements. See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership. The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License. You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied. See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 *
020 */
021 package org.apache.qpid.management.domain.model.type;
022
023 import java.io.Serializable;
024 import java.util.Arrays;
025 import java.util.UUID;
026
027 import org.apache.qpid.transport.codec.Encoder;
028
029 /**
030 * It is a simple wrapper for a byte array (for example a 128bin).
031 * It is used to let QMan deal with an object instead of an array.
032 *
033 * @author Andrea Gazzarini
034 */
035 public final class Binary implements Serializable
036 {
037 private static final long serialVersionUID = -6865585077320637567L;
038
039 // Marker internal (empty) interface
040 private interface State extends Serializable{}
041
042 /**
043 * Internal state of this object used to denote the situation when the hashcode() method has never been called.
044 * After the hashcode has been computed this class switches the state of the outer object to the next state.
045 */
046 State hashCodeNotYetComputed = new State()
047 {
048 private static final long serialVersionUID = 221632033761266959L;
049
050 @Override
051 public int hashCode ()
052 {
053 hashCode = Arrays.hashCode(_bytes);
054 state = hashCodeAlreadyComputed;
055 return hashCode;
056 }
057 };
058
059 /**
060 * Internal state of this object used to denote the situation where the hashcode() method has already been computed.
061 * Simply it returns the just computed value for the hashcode.
062 */
063 State hashCodeAlreadyComputed = new State()
064 {
065 private static final long serialVersionUID = 221632033761266959L;
066
067 @Override
068 public int hashCode ()
069 {
070 return hashCode;
071 }
072 };
073
074 private final UUID uuid;
075 private final byte [] _bytes;
076 private long _first;
077 private int hashCode;
078
079 /** Current state (hashcode computation). */
080 State state = hashCodeNotYetComputed;
081
082 /**
083 * Builds a new binary with the given byte array.
084 *
085 * @param bytes the wrapped data.
086 */
087 public Binary(byte [] bytes)
088 {
089 this._bytes = bytes;
090 byte [] array = new byte [8];
091 System.arraycopy(_bytes, 0, array, 0, 8);
092 _first = unpack64(array);
093 uuid = UUID.randomUUID();
094 }
095
096 @Override
097 public int hashCode ()
098 {
099 return state.hashCode();
100 }
101
102 @Override
103 public boolean equals (Object obj)
104 {
105 try
106 {
107 Binary binary = (Binary)obj;
108 return Arrays.equals(_bytes, binary._bytes);
109 } catch (Exception exception)
110 {
111 return false;
112 }
113 }
114
115 /**
116 * Encodes the content (wrapped byte array) of this instance using the given encoder.
117 *
118 * @param encoder the encoder used to encode instance content.
119 */
120 public void encode(Encoder encoder)
121 {
122 encoder.writeBin128(_bytes);
123 }
124
125 @Override
126 public String toString ()
127 {
128 return uuid.toString();
129 }
130
131 /**
132 * Returns the bank identifier derived from this object identifier.
133 *
134 * @return the bank identifier derived from this object identifier.
135 */
136 public long getBankId()
137 {
138 return _first & 0x000000000FFFFFFF;
139 }
140
141 /**
142 * Returns the broker identifier derived from this object identifier.
143 *
144 * @return the broker identifier derived from this object identifier.
145 */
146 public long getBrokerId()
147 {
148 return (_first & 281474708275200L) >> 28;
149 }
150
151 public final long unpack64(byte data[]) {
152 return (
153 ((long) (data[0] & 0xff) << 56) |
154 ((long)(data[1] & 0xff) << 48) |
155 ((long)(data[2] & 0xff) << 40) |
156 ((long)(data[3] & 0xff) << 32) |
157 ((long)(data[4] & 0xff) << 24) |
158 ((long)(data[5] & 0xff) << 16) |
159 ((long)(data[6] & 0xff) << 8) |
160 (long) data[7] & 0xff);
161 }
162 }
|