/** * Generates a new random {@code SpanId}. * * @param random The random number generator. * @return a valid new {@code SpanId}. * @since 0.5 */ public static SpanId generateRandomId(Random random) { long id; do { id = random.nextLong(); } while (id == INVALID_ID); return new SpanId(id); }
/** * Generates a new random {@code SpanId}. * * @param random The random number generator. * @return a valid new {@code SpanId}. */ public static SpanId generateRandomId(Random random) { byte[] bytes = new byte[SIZE]; do { random.nextBytes(bytes); } while (Arrays.equals(bytes, INVALID.bytes)); return new SpanId(bytes); }
/** * Returns a {@code SpanId} whose representation is copied from the {@code src} beginning at the * {@code srcOffset} offset. * * @param src the buffer where the representation of the {@code SpanId} is copied. * @param srcOffset the offset in the buffer where the representation of the {@code SpanId} * begins. * @return a {@code SpanId} whose representation is copied from the buffer. * @throws NullPointerException if {@code src} is null. * @throws IndexOutOfBoundsException if {@code srcOffset+SpanId.SIZE} is greater than {@code * src.length}. */ public static SpanId fromBytes(byte[] src, int srcOffset) { byte[] bytes = new byte[SIZE]; System.arraycopy(src, srcOffset, bytes, 0, SIZE); return new SpanId(bytes); }
/** * Returns a {@code SpanId} built from a byte representation. * * <p>Equivalent with: * * <pre>{@code * SpanId.fromBytes(buffer, 0); * }</pre> * * @param buffer the representation of the {@code SpanId}. * @return a {@code SpanId} whose representation is given by the {@code buffer} parameter. * @throws NullPointerException if {@code buffer} is null. * @throws IllegalArgumentException if {@code buffer.length} is not {@link SpanId#SIZE}. */ public static SpanId fromBytes(byte[] buffer) { checkNotNull(buffer, "buffer"); checkArgument(buffer.length == SIZE, "Invalid size: expected %s, got %s", SIZE, buffer.length); byte[] bytesCopied = Arrays.copyOf(buffer, SIZE); return new SpanId(bytesCopied); }
/** * Returns a {@code SpanId} built from a lowercase base16 representation. * * @param src the lowercase base16 representation. * @param srcOffset the offset in the buffer where the representation of the {@code SpanId} * begins. * @return a {@code SpanId} built from a lowercase base16 representation. * @throws NullPointerException if {@code src} is null. * @throws IllegalArgumentException if not enough characters in the {@code src} from the {@code * srcOffset}. * @since 0.11 */ public static SpanId fromLowerBase16(CharSequence src, int srcOffset) { Utils.checkNotNull(src, "src"); return new SpanId(BigendianEncoding.longFromBase16String(src, srcOffset)); }
/** * Returns a {@code SpanId} whose representation is copied from the {@code src} beginning at the * {@code srcOffset} offset. * * @param src the buffer where the representation of the {@code SpanId} is copied. * @param srcOffset the offset in the buffer where the representation of the {@code SpanId} * begins. * @return a {@code SpanId} whose representation is copied from the buffer. * @throws NullPointerException if {@code src} is null. * @throws IndexOutOfBoundsException if {@code srcOffset+SpanId.SIZE} is greater than {@code * src.length}. * @since 0.5 */ public static SpanId fromBytes(byte[] src, int srcOffset) { Utils.checkNotNull(src, "src"); return new SpanId(BigendianEncoding.longFromByteArray(src, srcOffset)); }
/** * Returns a {@code SpanId} built from a lowercase base16 representation. * * @param src the lowercase base16 representation. * @return a {@code SpanId} built from a lowercase base16 representation. * @throws NullPointerException if {@code src} is null. * @throws IllegalArgumentException if {@code src.length} is not {@code 2 * SpanId.SIZE} OR if the * {@code str} has invalid characters. */ public static SpanId fromLowerBase16(CharSequence src) { checkArgument( src.length() == 2 * SIZE, "Invalid size: expected %s, got %s", 2 * SIZE, src.length()); byte[] bytes = BaseEncoding.base16().lowerCase().decode(src); return new SpanId(bytes); }