|
19 | 19 | */
|
20 | 20 | package org.dcache.nfs.util;
|
21 | 21 |
|
22 |
| -import java.io.Serializable; |
| 22 | +import java.nio.ByteBuffer; |
23 | 23 | import java.util.Arrays;
|
24 |
| - |
25 |
| -import com.google.common.io.BaseEncoding; |
| 24 | +import java.util.Base64; |
26 | 25 |
|
27 | 26 | /**
|
28 |
| - * A helper class for opaque data manipulations. Enabled opaque date to be used as a key in {@link java.util.Collection} |
| 27 | + * Describes something that can be used as a key in {@link java.util.Map} and that can be converted to a {@code byte[]} |
| 28 | + * and a Base64 string representation. |
29 | 29 | */
|
30 |
| -public class Opaque implements Serializable { |
31 |
| - |
32 |
| - private static final long serialVersionUID = 1532238396149112674L; |
33 |
| - |
34 |
| - private final byte[] _opaque; |
35 |
| - |
36 |
| - public Opaque(byte[] opaque) { |
37 |
| - _opaque = opaque; |
| 30 | +public interface Opaque { |
| 31 | + /** |
| 32 | + * Returns an {@link Opaque} instance based on a copy of the given bytes. |
| 33 | + * |
| 34 | + * @param bytes The bytes. |
| 35 | + * @return The {@link Opaque} instance. |
| 36 | + */ |
| 37 | + static Opaque forBytes(byte[] bytes) { |
| 38 | + return new OpaqueImpl(bytes.clone()); |
38 | 39 | }
|
39 | 40 |
|
40 |
| - public byte[] getOpaque() { |
41 |
| - return _opaque; |
| 41 | + /** |
| 42 | + * Returns an {@link Opaque} instance based on a copy of the {@code length} bytes from the given {@link ByteBuffer}. |
| 43 | + * |
| 44 | + * @param buf The buffer. |
| 45 | + * @param length The number of bytes. |
| 46 | + * @return The {@link Opaque} instance. |
| 47 | + */ |
| 48 | + static Opaque forBytes(ByteBuffer buf, int length) { |
| 49 | + byte[] bytes = new byte[length]; |
| 50 | + buf.get(bytes); |
| 51 | + |
| 52 | + return new OpaqueImpl(bytes); |
42 | 53 | }
|
43 | 54 |
|
44 |
| - @Override |
45 |
| - public int hashCode() { |
46 |
| - return Arrays.hashCode(_opaque); |
| 55 | + /** |
| 56 | + * Default implementation for {@link #hashCode()}. |
| 57 | + * |
| 58 | + * @param obj The instance object. |
| 59 | + * @return The hash code. |
| 60 | + * @see #hashCode() |
| 61 | + */ |
| 62 | + static int defaultHashCode(Opaque obj) { |
| 63 | + return Arrays.hashCode(obj.toBytes()); |
47 | 64 | }
|
48 | 65 |
|
49 |
| - @Override |
50 |
| - public boolean equals(Object o) { |
51 |
| - if (o == this) { |
| 66 | + /** |
| 67 | + * Default implementation for {@link #equals(Object)}. |
| 68 | + * |
| 69 | + * @param obj The instance object. |
| 70 | + * @param other The other object. |
| 71 | + * @return {@code true} if equal. |
| 72 | + * @see #equals(Object) |
| 73 | + */ |
| 74 | + static boolean defaultEquals(Opaque obj, Object other) { |
| 75 | + if (other == obj) { |
52 | 76 | return true;
|
53 | 77 | }
|
54 |
| - if (!(o instanceof Opaque)) { |
| 78 | + if (!(other instanceof Opaque)) { |
55 | 79 | return false;
|
56 | 80 | }
|
| 81 | + return Arrays.equals(obj.toBytes(), ((Opaque) other).toBytes()); |
| 82 | + } |
57 | 83 |
|
58 |
| - return Arrays.equals(_opaque, ((Opaque) o)._opaque); |
| 84 | + /** |
| 85 | + * Returns a byte-representation of this opaque object. |
| 86 | + * |
| 87 | + * @return A new array. |
| 88 | + */ |
| 89 | + byte[] toBytes(); |
| 90 | + |
| 91 | + /** |
| 92 | + * Returns the number of bytes in this opaque object; |
| 93 | + * |
| 94 | + * @return The number of bytes; |
| 95 | + */ |
| 96 | + int numBytes(); |
| 97 | + |
| 98 | + /** |
| 99 | + * Returns a Base64 string representing this opaque object. |
| 100 | + * |
| 101 | + * @return A Base64 string. |
| 102 | + */ |
| 103 | + String toBase64(); |
| 104 | + |
| 105 | + /** |
| 106 | + * Writes the bytes of this {@link Opaque} to the given {@link ByteBuffer}. |
| 107 | + * |
| 108 | + * @param buf The target buffer. |
| 109 | + */ |
| 110 | + default void putBytes(ByteBuffer buf) { |
| 111 | + buf.put(toBytes()); |
59 | 112 | }
|
60 | 113 |
|
| 114 | + /** |
| 115 | + * Returns the hashCode based on the byte-representation of this instance. |
| 116 | + * <p> |
| 117 | + * This method must behave like {@link #defaultHashCode(Opaque)}, but may be optimized. |
| 118 | + * |
| 119 | + * @return The hashCode. |
| 120 | + */ |
| 121 | + @Override |
| 122 | + int hashCode(); |
| 123 | + |
| 124 | + /** |
| 125 | + * Compares this object to another one. |
| 126 | + * <p> |
| 127 | + * This method must behave like {@link #defaultEquals(Opaque, Object)}, but may be optimized. |
| 128 | + * |
| 129 | + * @return {@code true} if both objects are equal. |
| 130 | + */ |
61 | 131 | @Override
|
62 |
| - public String toString() { |
63 |
| - StringBuilder sb = new StringBuilder(); |
64 |
| - sb.append('[').append(BaseEncoding.base16().lowerCase().encode(_opaque)).append(']'); |
65 |
| - return sb.toString(); |
| 132 | + boolean equals(Object o); |
| 133 | + |
| 134 | + final class OpaqueImpl implements Opaque { |
| 135 | + private final byte[] _opaque; |
| 136 | + private String base64 = null; |
| 137 | + |
| 138 | + private OpaqueImpl(byte[] opaque) { |
| 139 | + _opaque = opaque; |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public byte[] toBytes() { |
| 144 | + return _opaque.clone(); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public String toBase64() { |
| 149 | + if (base64 == null) { |
| 150 | + base64 = Base64.getEncoder().withoutPadding().encodeToString(_opaque); |
| 151 | + } |
| 152 | + return base64; |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public void putBytes(ByteBuffer buf) { |
| 157 | + buf.put(_opaque); |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public int hashCode() { |
| 162 | + return Arrays.hashCode(_opaque); |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public boolean equals(Object o) { |
| 167 | + if (o == this) { |
| 168 | + return true; |
| 169 | + } |
| 170 | + if (!(o instanceof Opaque)) { |
| 171 | + return false; |
| 172 | + } |
| 173 | + |
| 174 | + if (o instanceof OpaqueImpl) { |
| 175 | + return Arrays.equals(_opaque, ((OpaqueImpl) o)._opaque); |
| 176 | + } else { |
| 177 | + return Arrays.equals(_opaque, ((Opaque) o).toBytes()); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Returns a (potentially non-stable) debug string. |
| 183 | + * |
| 184 | + * @see #toBase64() |
| 185 | + */ |
| 186 | + @Override |
| 187 | + public String toString() { |
| 188 | + return super.toString() + "[" + toBase64() + "]"; |
| 189 | + } |
| 190 | + |
| 191 | + @Override |
| 192 | + public int numBytes() { |
| 193 | + return _opaque.length; |
| 194 | + } |
66 | 195 | }
|
67 | 196 | }
|
0 commit comments