下面列出了com.google.common.primitives.UnsignedBytes#lexicographicalComparator ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public ByteLexicoderTest() {
super(
Lexicoders.BYTE,
Byte.MIN_VALUE,
Byte.MAX_VALUE,
new Byte[] {
(byte) -10,
Byte.MIN_VALUE,
(byte) 11,
(byte) -122,
(byte) 122,
(byte) -100,
(byte) 100,
Byte.MAX_VALUE,
(byte) 0},
UnsignedBytes.lexicographicalComparator());
}
public DoubleLexicoderTest() {
super(
Lexicoders.DOUBLE,
-Double.MAX_VALUE,
Double.MAX_VALUE,
new Double[] {
-10d,
-Double.MAX_VALUE,
11d,
-14.2,
14.2,
-100.002,
100.002,
-11d,
Double.MAX_VALUE,
0d},
UnsignedBytes.lexicographicalComparator());
}
public FloatLexicoderTest() {
super(
Lexicoders.FLOAT,
-Float.MAX_VALUE,
Float.MAX_VALUE,
new Float[] {
-10f,
-Float.MAX_VALUE,
11f,
-14.2f,
14.2f,
-100.002f,
100.002f,
-11f,
Float.MAX_VALUE,
0f},
UnsignedBytes.lexicographicalComparator());
}
@SuppressWarnings({ "unchecked", "rawtypes" })
protected void createIndexes(DB database)
{
//TIMESTAMP INDEX
Tuple2Comparator<Long, byte[]> comparator = new Fun.Tuple2Comparator<Long, byte[]>(Fun.COMPARATOR, UnsignedBytes.lexicographicalComparator());
NavigableSet<Tuple2<Integer, byte[]>> heightIndex = database.createTreeSet("transactions_index_timestamp")
.comparator(comparator)
.makeOrGet();
NavigableSet<Tuple2<Integer, byte[]>> descendingHeightIndex = database.createTreeSet("transactions_index_timestamp_descending")
.comparator(new ReverseComparator(comparator))
.makeOrGet();
createIndex(TIMESTAMP_INDEX, heightIndex, descendingHeightIndex, new Fun.Function2<Long, byte[], Transaction>() {
@Override
public Long run(byte[] key, Transaction value) {
return value.getTimestamp();
}
});
}
@Override
public void startTx(Transaction tx) {
this.tx = tx;
if (inProgressChanges == null) {
inProgressChanges = new TreeSet<>(UnsignedBytes.lexicographicalComparator());
for (long inProgressTx : tx.getInProgress()) {
inProgressChanges.add(Bytes.concat(fenceId, Longs.toByteArray(inProgressTx)));
}
}
}
public ShortLexicoderTest() {
super(
Lexicoders.SHORT,
Short.MIN_VALUE,
Short.MAX_VALUE,
new Short[] {(short) -10, Short.MIN_VALUE, (short) 2678, Short.MAX_VALUE, (short) 0},
UnsignedBytes.lexicographicalComparator());
}
@Test
public void testEqualsPerformance() {
boolean testEnabled = false;
if(testEnabled) {
final int ITERATIONS = 10000000;
long start1 = System.currentTimeMillis();
for (int i = 0; i < ITERATIONS; i++) {
Comparator<byte[]> comparator = UnsignedBytes
.lexicographicalComparator();
comparator.compare(wrapper1.getData(),
wrapper2.getData());
}
System.out.println(System.currentTimeMillis() - start1 + "ms");
long start2 = System.currentTimeMillis();
for (int i = 0; i < ITERATIONS; i++) {
Arrays.equals(wrapper1.getData(), wrapper2.getData());
}
System.out.println(System.currentTimeMillis() - start2 + "ms");
long start3 = System.currentTimeMillis();
for (int i = 0; i < ITERATIONS; i++) {
FastByteComparisons.compareTo(wrapper1.getData(), 0, wrapper1.getData().length, wrapper2.getData(), 0, wrapper1.getData().length);
}
System.out.println(System.currentTimeMillis() - start3 + "ms");
}
}
public TransactionDatabase(TransactionDatabase parent)
{
this.parent = parent;
//OPEN MAP
this.transactionMap = new TreeMap<byte[], byte[]>(UnsignedBytes.lexicographicalComparator());
}
public LongLexicoderTest() {
super(
Lexicoders.LONG,
Long.MIN_VALUE,
Long.MAX_VALUE,
new Long[] {-10l, Long.MIN_VALUE, 2678l, Long.MAX_VALUE, 0l},
UnsignedBytes.lexicographicalComparator());
}
/**
* Creates redeem script with given public keys and threshold. Given public keys will be placed in
* redeem script in the lexicographical sorting order.
*/
public static Script createRedeemScript(int threshold, List<byte[]> pubkeys) {
pubkeys = new ArrayList<byte[]>(pubkeys);
final Comparator comparator = UnsignedBytes.lexicographicalComparator();
Collections.sort(pubkeys, new Comparator<byte[]>() {
@Override
public int compare(byte[] k1, byte[] k2) {
return comparator.compare(k1, k2);
}
});
return ScriptBuilder.createMultiSigOutputScript(threshold, pubkeys);
}
@Override
protected Map<byte[], Long> getMemoryMap()
{
return new TreeMap<byte[], Long>(UnsignedBytes.lexicographicalComparator());
}
public boolean rangeContains(@Nonnull byte[] address) {
Comparator<byte[]> comparator = UnsignedBytes.lexicographicalComparator();
return (comparator.compare(address, getRangeStart().getAddress()) >= 0)
&& (comparator.compare(address, getRangeEnd().getAddress()) <= 0);
}
@Override
protected Map<byte[], byte[]> getMemoryMap()
{
return new TreeMap<byte[], byte[]>(UnsignedBytes.lexicographicalComparator());
}
@Override
protected Map<byte[], Block> getMemoryMap()
{
return new TreeMap<byte[], Block>(UnsignedBytes.lexicographicalComparator());
}
@Override
protected Map<byte[], byte[]> getMemoryMap()
{
return new TreeMap<byte[], byte[]>(UnsignedBytes.lexicographicalComparator());
}
@Override
protected Map<byte[], Integer> getMemoryMap()
{
return new TreeMap<byte[], Integer>(UnsignedBytes.lexicographicalComparator());
}
@Override
protected Map<byte[], byte[]> getMemoryMap()
{
return new TreeMap<byte[], byte[]>(UnsignedBytes.lexicographicalComparator());
}
public boolean rangeContains(@Nonnull byte[] address) {
Comparator<byte[]> comparator = UnsignedBytes.lexicographicalComparator();
return (comparator.compare(address, getRangeStart().getAddress()) >= 0)
&& (comparator.compare(address, getRangeEnd().getAddress()) <= 0);
}
@Override
protected Map<byte[], Name> getMemoryMap()
{
return new TreeMap<byte[], Name>(UnsignedBytes.lexicographicalComparator());
}
@Override
protected Map<byte[], Transaction> getMemoryMap()
{
return new TreeMap<byte[], Transaction>(UnsignedBytes.lexicographicalComparator());
}