T
- the type of the object to accesspublic abstract class Access<T> extends Object
T
class instances as ordered byte
sequence. All getXXX(input, offset)
should be consistent to each other in terms of
ordered byte sequence each T
instance represents. For example, if some Access
implementation returns ByteOrder.LITTLE_ENDIAN
on byteOrder(input)
call, the following expressions should always have the same value:
getLong(input, 0)
getUnsignedInt(input, 0) | (getUnsignedInt(input, 4) << 32)
getUnsignedInt(input, 0) |
((long) getUnsignedShort(input, 4) << 32) |
((long) getUnsignedByte(input, 6) << 48) |
((long) getUnsignedByte(input, 7) << 56)
getXXX(input, offset)
methods could throw unchecked exceptions when requested bytes
range is outside of the bounds of the byte sequence, represented by the given input
.
However, they could omit checks for better performance.
Only getByte(Object, long)
and byteOrder(Object)
methods are abstract in
this class, so implementing them is sufficient for valid Access
instance, but for
efficiency your should override methods used by target LongHashFunction
implementation.
Access
API is designed for inputs, that actually represent byte sequences that lay
continuously in memory. Theoretically Access
strategy could be implemented for
non-continuous byte sequences, or abstractions which aren't actually present in memory as they
are accessed, but this should be awkward, and hashing using such Access
is expected to
be slow.
Modifier | Constructor and Description |
---|---|
protected |
Access()
Constructor for use in subclasses.
|
Modifier and Type | Method and Description |
---|---|
abstract ByteOrder |
byteOrder(T input)
The byte order in which all multi-byte
getXXX() reads from the given input
are performed. |
abstract int |
getByte(T input,
long offset)
Reads a single byte at the given
offset in the byte sequence represented by the given
input , returned widened to int . |
int |
getInt(T input,
long offset)
Reads
[offset, offset + 3] bytes of the byte sequence represented by the given
input as a single int value. |
long |
getLong(T input,
long offset)
Reads
[offset, offset + 7] bytes of the byte sequence represented by the given
input as a single long value. |
int |
getShort(T input,
long offset)
Reads
[offset, offset + 1] bytes of the byte sequence represented by the given
input as a single short value, returned widened to int . |
int |
getUnsignedByte(T input,
long offset)
Shortcut for
getByte(input, offset) & 0xFF . |
long |
getUnsignedInt(T input,
long offset)
Shortcut for
getInt(input, offset) & 0xFFFFFFFFL . |
int |
getUnsignedShort(T input,
long offset)
Shortcut for
getShort(input, offset) & 0xFFFF . |
static Access<ByteBuffer> |
toByteBuffer()
Returns the
Access to any ByteBuffer . |
static <T extends CharSequence> |
toCharSequence(ByteOrder backingOrder)
|
static <T extends CharSequence> |
toNativeCharSequence()
|
static <T> Access<T> |
unsafe()
Returns the
Access delegating getXXX(input, offset) methods to sun.misc.Unsafe.getXXX(input, offset) . |
public static <T> Access<T> unsafe()
Access
delegating getXXX(input, offset)
methods to sun.misc.Unsafe.getXXX(input, offset)
.
Usage example:
class Pair {
long first, second;
static final long pairDataOffset =
theUnsafe.objectFieldOffset(Pair.class.getDeclaredField("first"));
static long hashPair(Pair pair, LongHashFunction hashFunction) {
return hashFunction.hash(pair, Access.unsafe(), pairDataOffset, 16L);
}
}
null
is a valid input, on accepting null
Unsafe
just interprets
the given offset as a wild memory address. Note that for hashing memory by address there is
a shortcut hashMemory(address, len)
method.
T
- the type of objects to accessAccess
public static Access<ByteBuffer> toByteBuffer()
Access
to any ByteBuffer
. This Access
isn't useful in
the user code, because methods LongHashFunction.hashBytes(ByteBuffer)
and
LongHashFunction.hashBytes(ByteBuffer, int, int)
exist. This Access
could be
used in new LongHashFunction
implementations.Access
to ByteBuffer
spublic static <T extends CharSequence> Access<T> toNativeCharSequence()
Access
to CharSequence
s backed by native char
reads, typically from char[]
array.
Usage example:
static long hashStringBuffer(StringBuffer buffer, LongHashFunction hashFunction) {
return hashFunction.hash(buffer, Access.toNativeCharSequence(),
// * 2L because length is passed in bytes, not chars
0L, buffer.length() * 2L);
}
This method is a shortcut for Access.toCharSequence(ByteOrder.nativeOrder())
.
T
- the CharSequence
subtype (backed by native char reads
) to accessAccess
to CharSequence
s backed by native char
readstoCharSequence(ByteOrder)
public static <T extends CharSequence> Access<T> toCharSequence(ByteOrder backingOrder)
Access
to CharSequence
s backed by char
reads made in
the specified byte order.
Usage example:
static long hashCharBuffer(CharBuffer buffer, LongHashFunction hashFunction) {
return hashFunction.hash(buffer, Access.toCharSequence(buffer.order()),
// * 2L because length is passed in bytes, not chars
0L, buffer.length() * 2L);
}
T
- the CharSequence
subtype to accessbackingOrder
- the byte order of char
reads backing
CharSequences
to accessAccess
to CharSequence
s backed by char
reads made in
the specified byte ordertoNativeCharSequence()
public long getLong(T input, long offset)
[offset, offset + 7]
bytes of the byte sequence represented by the given
input
as a single long
value.input
- the object to accessoffset
- offset to the first byte to read within the byte sequence represented
by the given objectlong
value, in the expected
orderpublic long getUnsignedInt(T input, long offset)
getInt(input, offset) & 0xFFFFFFFFL
. Could be implemented more
efficiently.input
- the object to accessoffset
- offset to the first byte to read within the byte sequence represented
by the given objectpublic int getInt(T input, long offset)
[offset, offset + 3]
bytes of the byte sequence represented by the given
input
as a single int
value.input
- the object to accessoffset
- offset to the first byte to read within the byte sequence represented
by the given objectint
value, in the expected
orderpublic int getUnsignedShort(T input, long offset)
getShort(input, offset) & 0xFFFF
. Could be implemented more
efficiently.input
- the object to accessoffset
- offset to the first byte to read within the byte sequence represented
by the given objectpublic int getShort(T input, long offset)
[offset, offset + 1]
bytes of the byte sequence represented by the given
input
as a single short
value, returned widened to int
.input
- the object to accessoffset
- offset to the first byte to read within the byte sequence represented
by the given objectshort
value, in the expected
order, widened to int
public int getUnsignedByte(T input, long offset)
getByte(input, offset) & 0xFF
. Could be implemented more efficiently.input
- the object to accessoffset
- offset to the byte to read within the byte sequence represented
by the given objectoffset
, interpreted as unsignedpublic abstract int getByte(T input, long offset)
offset
in the byte sequence represented by the given
input
, returned widened to int
.input
- the object to accessoffset
- offset to the byte to read within the byte sequence represented
by the given objectoffset
, widened to int
Copyright © 2014–2017. All rights reserved.