| 1 | | (1) | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | |
| 34 | | | package org.jcoderz.guidelines.snippets; |
| 35 | | | |
| 36 | | | |
| 37 | | | import java.io.File; |
| 38 | | | import java.io.FileInputStream; |
| 39 | | | import java.io.IOException; |
| 40 | | | import java.io.Serializable; |
| 41 | | | |
| 42 | | | |
| 43 | | | |
| 44 | | | |
| 45 | | | |
| 46 | | | |
| 47 | | | |
| 48 | | | @author |
| 49 | | | |
| 50 | | | public final class TransactionId |
| 51 | | | implements Comparable, Serializable |
| 52 | | | { |
| 53 | | | |
| 54 | | | public static final String TYPE_NAME = "TX_ID"; |
| 55 | | | |
| 56 | | | |
| 57 | | | private static final int NUMBER_OF_BITS_PER_INT = 32; |
| 58 | | | |
| 59 | | | private static final int BUFFER_SIZE = 4096; |
| 60 | | | |
| 61 | | | private static final int BUFFER_MULTIPLIER = 2; |
| 62 | | | |
| 63 | | | private static final long serialVersionUID = -7064645359225861305L; |
| 64 | | | |
| 65 | | | |
| 66 | | | private final long mTransactionId; |
| 67 | | | |
| 68 | | | |
| 69 | | | |
| 70 | | | |
| 71 | | | |
| 72 | | | @param |
| 73 | | | <code></code> |
| 74 | | | @throws |
| 75 | | | |
| 76 | | | |
| 77 | | | private TransactionId (long transactionId) |
| 78 | | | throws IllegalArgumentException |
| 79 | 0 | | { |
| 80 | 0 | | if (transactionId < 0) |
| 81 | | | { |
| 82 | 0 | | throw new IllegalArgumentException(TYPE_NAME + " " |
| 83 | | | + String.valueOf(transactionId) |
| 84 | | | + "Value must be positive."); |
| 85 | | | } |
| 86 | 0 | | mTransactionId = transactionId; |
| 87 | 0 | | } |
| 88 | | | |
| 89 | | | |
| 90 | | | |
| 91 | | | |
| 92 | | | |
| 93 | | | @param <code></code> |
| 94 | | | @return |
| 95 | | | @throws |
| 96 | | | |
| 97 | | | |
| 98 | | | public static TransactionId fromString (String s) |
| 99 | | | throws IllegalArgumentException |
| 100 | | | { |
| 101 | | | final TransactionId result; |
| 102 | | | try |
| 103 | | | { |
| 104 | 0 | | result = new TransactionId(Long.parseLong(s)); |
| 105 | | | } |
| 106 | 0 | | catch (NumberFormatException ex) |
| 107 | | | { |
| 108 | 0 | | final IllegalArgumentException iaex |
| 109 | | | = new IllegalArgumentException( |
| 110 | | | TYPE_NAME + " Failed to parse the value."); |
| 111 | 0 | | iaex.initCause(ex); |
| 112 | 0 | (2) | throw iaex; |
| 113 | | | } |
| 114 | 0 | (3) | catch (NullPointerException ex) |
| 115 | | | { |
| 116 | 0 | | final IllegalArgumentException iaex |
| 117 | | | = new IllegalArgumentException( |
| 118 | | | TYPE_NAME + " Value must not be null."); |
| 119 | 0 | | iaex.initCause(ex); |
| 120 | 0 | (4) | throw iaex; |
| 121 | 0 | | } |
| 122 | 0 | | return result; |
| 123 | | | } |
| 124 | | | |
| 125 | | | |
| 126 | | | <code></code> |
| 127 | | | |
| 128 | | | @param <code></code> |
| 129 | | | @return |
| 130 | | | @throws |
| 131 | | | |
| 132 | | | |
| 133 | | | public static TransactionId fromLong (long l) |
| 134 | | | throws IllegalArgumentException |
| 135 | | | { |
| 136 | 0 | | return new TransactionId(l); |
| 137 | | | } |
| 138 | | | |
| 139 | | | |
| 140 | | | |
| 141 | | | |
| 142 | | | @return |
| 143 | | | |
| 144 | | | public String toString () |
| 145 | | | { |
| 146 | 0 | | return Long.toString(mTransactionId); |
| 147 | | | } |
| 148 | | | |
| 149 | | | |
| 150 | | | |
| 151 | | | |
| 152 | | | @return |
| 153 | | | |
| 154 | | | public long toLong () |
| 155 | | | { |
| 156 | 0 | | return mTransactionId; |
| 157 | | | } |
| 158 | | | |
| 159 | | | |
| 160 | | | |
| 161 | | | |
| 162 | | | @param <code></code> |
| 163 | | | |
| 164 | | | @return |
| 165 | | | |
| 166 | | | |
| 167 | | | public boolean equals (Object obj) |
| 168 | | | { |
| 169 | 0 | | boolean result = false; |
| 170 | | | |
| 171 | 0 | | if (obj instanceof TransactionId) |
| 172 | | | { |
| 173 | 0 | | result = (mTransactionId |
| 174 | | | == (((TransactionId) obj).mTransactionId)); |
| 175 | | | } |
| 176 | | | |
| 177 | 0 | | return result; |
| 178 | | | } |
| 179 | | | |
| 180 | | | |
| 181 | | | |
| 182 | | | {@link } |
| 183 | | | |
| 184 | | | @param |
| 185 | | | @return |
| 186 | | | <code></code> |
| 187 | | | |
| 188 | | | |
| 189 | | | public int compareTo (Object o) |
| 190 | | | { |
| 191 | | | final int result; |
| 192 | | | |
| 193 | | | |
| 194 | 0 | | if (mTransactionId < ((TransactionId) o).mTransactionId) |
| 195 | | | { |
| 196 | 0 | | result = -1; |
| 197 | | | } |
| 198 | 0 | | else if (mTransactionId > ((TransactionId) o).mTransactionId) |
| 199 | | | { |
| 200 | 0 | | result = 1; |
| 201 | | | } |
| 202 | | | else |
| 203 | | | { |
| 204 | 0 | | result = 0; |
| 205 | | | } |
| 206 | 0 | | return result; |
| 207 | | | } |
| 208 | | | |
| 209 | | | |
| 210 | | | |
| 211 | | | |
| 212 | | | @return |
| 213 | | | |
| 214 | | | public int hashCode () |
| 215 | | | { |
| 216 | 0 | | return (int) (mTransactionId |
| 217 | | | ^ (mTransactionId >>> NUMBER_OF_BITS_PER_INT)); |
| 218 | | | } |
| 219 | | | |
| 220 | | | |
| 221 | | | |
| 222 | | | |
| 223 | | | @param |
| 224 | | | @return |
| 225 | | | @throws |
| 226 | | | |
| 227 | | (5) | private static byte[] readFully (File file) |
| 228 | | | throws IOException |
| 229 | | | { |
| 230 | 0 | (6) | final FileInputStream in = new FileInputStream(file); |
| 231 | 0 | | byte[] buffer = new byte[BUFFER_SIZE]; |
| 232 | | | int read; |
| 233 | 0 | | int pos = 0; |
| 234 | | | |
| 235 | 0 | | while ((read = in.read(buffer, pos, buffer.length - pos)) > 0) |
| 236 | | | { |
| 237 | 0 | | pos += read; |
| 238 | 0 | | if (pos == buffer.length) |
| 239 | | | { |
| 240 | 0 | (7) | byte[] newBuffer |
| 241 | | | = new byte[buffer.length * BUFFER_MULTIPLIER]; |
| 242 | | | |
| 243 | 0 | | System.arraycopy(buffer, 0, newBuffer, 0, buffer.length); |
| 244 | 0 | | buffer = newBuffer; |
| 245 | 0 | | } |
| 246 | | | } |
| 247 | | | |
| 248 | 0 | | if (pos != buffer.length) |
| 249 | | | { |
| 250 | 0 | (8) | byte[] newBuffer = new byte[pos]; |
| 251 | | | |
| 252 | 0 | | System.arraycopy(buffer, 0, newBuffer, 0, pos); |
| 253 | 0 | | buffer = newBuffer; |
| 254 | | | } |
| 255 | | | |
| 256 | 0 | | return buffer; |
| 257 | | | } |
| 258 | | | } |
| 259 | | | |