Remove trailing spaces.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1477794 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2013-04-30 19:42:03 +00:00
parent d31ea317a5
commit 9af55b6479
14 changed files with 669 additions and 669 deletions

View File

@ -18,12 +18,12 @@ package org.apache.commons.collections4.trie;
/**
* TODO: add javadoc
*
*
* @since 4.0
* @version $Id$
*/
public abstract class AbstractKeyAnalyzer<K> implements KeyAnalyzer<K> {
private static final long serialVersionUID = -20497563720380683L;
/**
@ -36,34 +36,34 @@ public abstract class AbstractKeyAnalyzer<K> implements KeyAnalyzer<K> {
} else if (o2 == null) {
return 1;
}
return ((Comparable<K>)o1).compareTo(o2);
}
/**
/**
* Returns true if bitIndex is a {@link KeyAnalyzer#OUT_OF_BOUNDS_BIT_KEY}
*/
static boolean isOutOfBoundsIndex(final int bitIndex) {
return bitIndex == OUT_OF_BOUNDS_BIT_KEY;
}
/**
/**
* Returns true if bitIndex is a {@link KeyAnalyzer#EQUAL_BIT_KEY}
*/
static boolean isEqualBitKey(final int bitIndex) {
return bitIndex == EQUAL_BIT_KEY;
}
/**
* Returns true if bitIndex is a {@link KeyAnalyzer#NULL_BIT_KEY}
/**
* Returns true if bitIndex is a {@link KeyAnalyzer#NULL_BIT_KEY}
*/
static boolean isNullBitKey(final int bitIndex) {
return bitIndex == NULL_BIT_KEY;
}
/**
* Returns true if the given bitIndex is valid. Indices
* are considered valid if they're between 0 and
/**
* Returns true if the given bitIndex is valid. Indices
* are considered valid if they're between 0 and
* {@link Integer#MAX_VALUE}
*/
static boolean isValidBitIndex(final int bitIndex) {

View File

@ -23,43 +23,43 @@ import java.util.Map;
import org.apache.commons.collections4.Trie;
/**
* This class provides some basic {@link Trie} functionality and
* This class provides some basic {@link Trie} functionality and
* utility methods for actual {@link Trie} implementations.
*
*
* @since 4.0
* @version $Id$
*/
abstract class AbstractTrie<K, V> extends AbstractMap<K, V>
abstract class AbstractTrie<K, V> extends AbstractMap<K, V>
implements Trie<K, V>, Serializable {
private static final long serialVersionUID = 5826987063535505652L;
// TODO Privatise fields?
/**
* The {@link KeyAnalyzer} that's being used to build the
* The {@link KeyAnalyzer} that's being used to build the
* PATRICIA {@link Trie}.
*/
protected final KeyAnalyzer<? super K> keyAnalyzer;
/**
/**
* Constructs a new {@link Trie} using the given {@link KeyAnalyzer}.
*/
public AbstractTrie(final KeyAnalyzer<? super K> keyAnalyzer) {
if (keyAnalyzer == null) {
throw new NullPointerException("keyAnalyzer");
}
this.keyAnalyzer = keyAnalyzer;
}
/**
* Returns the {@link KeyAnalyzer} that constructed the {@link Trie}.
*/
public KeyAnalyzer<? super K> getKeyAnalyzer() {
return keyAnalyzer;
}
/**
* {@inheritDoc}
*/
@ -70,7 +70,7 @@ abstract class AbstractTrie<K, V> extends AbstractMap<K, V>
}
return entry.getKey();
}
/**
* {@inheritDoc}
*/
@ -81,7 +81,7 @@ abstract class AbstractTrie<K, V> extends AbstractMap<K, V>
}
return entry.getValue();
}
@Override
public String toString() {
final StringBuilder buffer = new StringBuilder();
@ -92,7 +92,7 @@ abstract class AbstractTrie<K, V> extends AbstractMap<K, V>
buffer.append("}\n");
return buffer.toString();
}
/**
* A utility method to cast keys. It actually doesn't
* cast anything. It's just fooling the compiler!
@ -101,33 +101,33 @@ abstract class AbstractTrie<K, V> extends AbstractMap<K, V>
final K castKey(final Object key) {
return (K)key;
}
/**
* Returns the length of the given key in bits
*
*
* @see KeyAnalyzer#lengthInBits(Object)
*/
final int lengthInBits(final K key) {
if (key == null) {
return 0;
}
return keyAnalyzer.lengthInBits(key);
}
/**
* Returns the number of bits per element in the key
*
*
* @see KeyAnalyzer#bitsPerElement()
*/
final int bitsPerElement() {
return keyAnalyzer.bitsPerElement();
}
/**
* Returns whether or not the given bit on the
* Returns whether or not the given bit on the
* key is set or false if the key is null.
*
*
* @see KeyAnalyzer#isBitSet(Object, int, int)
*/
final boolean isBitSet(final K key, final int bitIndex, final int lengthInBits) {
@ -136,15 +136,15 @@ abstract class AbstractTrie<K, V> extends AbstractMap<K, V>
}
return keyAnalyzer.isBitSet(key, bitIndex, lengthInBits);
}
/**
* Utility method for calling {@link KeyAnalyzer#bitIndex(Object, int, int, Object, int, int)}
*/
final int bitIndex(final K key, final K foundKey) {
return keyAnalyzer.bitIndex(key, 0, lengthInBits(key),
return keyAnalyzer.bitIndex(key, 0, lengthInBits(key),
foundKey, 0, lengthInBits(foundKey));
}
/**
* An utility method for calling {@link KeyAnalyzer#compare(Object, Object)}
*/
@ -154,43 +154,43 @@ abstract class AbstractTrie<K, V> extends AbstractMap<K, V>
} else if (other == null) {
return false;
}
return keyAnalyzer.compare(key, other) == 0;
}
/**
* Returns true if both values are either null or equal
*/
static boolean compare(final Object a, final Object b) {
return a == null ? b == null : a.equals(b);
}
/**
* A basic implementation of {@link Entry}
*/
abstract static class BasicEntry<K, V> implements Map.Entry<K, V>, Serializable {
private static final long serialVersionUID = -944364551314110330L;
protected K key;
protected V value;
private final int hashCode;
public BasicEntry(final K key) {
this.key = key;
this.hashCode = key != null ? key.hashCode() : 0;
}
public BasicEntry(final K key, final V value) {
this.key = key;
this.value = value;
this.hashCode = (key != null ? key.hashCode() : 0)
^ (value != null ? value.hashCode() : 0);
}
/**
* Replaces the current key and value with the provided
* key &amp; value
@ -199,14 +199,14 @@ abstract class AbstractTrie<K, V> extends AbstractMap<K, V>
this.key = key;
return setValue(value);
}
/**
* {@inheritDoc}
*/
public K getKey() {
return key;
}
/**
* {@inheritDoc}
*/
@ -222,12 +222,12 @@ abstract class AbstractTrie<K, V> extends AbstractMap<K, V>
this.value = value;
return previous;
}
@Override
public int hashCode() {
return hashCode;
}
@Override
public boolean equals(final Object o) {
if (o == this) {
@ -235,15 +235,15 @@ abstract class AbstractTrie<K, V> extends AbstractMap<K, V>
} else if (!(o instanceof Map.Entry)) {
return false;
}
final Map.Entry<?, ?> other = (Map.Entry<?, ?>)o;
if (compare(key, other.getKey())
if (compare(key, other.getKey())
&& compare(value, other.getValue())) {
return true;
}
return false;
}
@Override
public String toString() {
return key + "=" + value;

View File

@ -18,49 +18,49 @@ package org.apache.commons.collections4.trie;
/**
* A {@link KeyAnalyzer} for byte[]s.
*
*
* @since 4.0
* @version $Id$
*/
public class ByteArrayKeyAnalyzer extends AbstractKeyAnalyzer<byte[]> {
private static final long serialVersionUID = 7382825097492285877L;
/**
* A singleton instance of {@link ByteArrayKeyAnalyzer}
*/
public static final ByteArrayKeyAnalyzer INSTANCE
public static final ByteArrayKeyAnalyzer INSTANCE
= new ByteArrayKeyAnalyzer(Integer.MAX_VALUE);
/**
* The length of an {@link Byte} in bits
*/
public static final int LENGTH = Byte.SIZE;
/**
* A bit mask where the first bit is 1 and the others are zero
*/
private static final int MSB = 0x80;
/**
* A place holder for null
*/
private static final byte[] NULL = new byte[0];
/**
* The maximum length of a key in bits
*/
private final int maxLengthInBits;
public ByteArrayKeyAnalyzer(final int maxLengthInBits) {
if (maxLengthInBits < 0) {
throw new IllegalArgumentException(
"maxLengthInBits=" + maxLengthInBits);
}
this.maxLengthInBits = maxLengthInBits;
}
/**
* Returns a bit mask where the given bit is set
*/
@ -75,36 +75,36 @@ public class ByteArrayKeyAnalyzer extends AbstractKeyAnalyzer<byte[]> {
public int getMaxLengthInBits() {
return maxLengthInBits;
}
/**
* {@inheritDoc}
*/
public int bitsPerElement() {
return LENGTH;
}
/**
* {@inheritDoc}
*/
public int lengthInBits(final byte[] key) {
return key != null ? key.length * bitsPerElement() : 0;
}
/**
* {@inheritDoc}
*/
public boolean isBitSet(final byte[] key, final int bitIndex, final int lengthInBits) {
if (key == null) {
if (key == null) {
return false;
}
final int prefix = maxLengthInBits - lengthInBits;
final int keyBitIndex = bitIndex - prefix;
if (keyBitIndex >= lengthInBits || keyBitIndex < 0) {
return false;
}
final int index = keyBitIndex / LENGTH;
final int bit = keyBitIndex % LENGTH;
return (key[index] & mask(bit)) != 0;
@ -113,66 +113,66 @@ public class ByteArrayKeyAnalyzer extends AbstractKeyAnalyzer<byte[]> {
/**
* {@inheritDoc}
*/
public int bitIndex(final byte[] key, final int offsetInBits, final int lengthInBits,
public int bitIndex(final byte[] key, final int offsetInBits, final int lengthInBits,
byte[] other, final int otherOffsetInBits, final int otherLengthInBits) {
if (other == null) {
other = NULL;
}
boolean allNull = true;
final int length = Math.max(lengthInBits, otherLengthInBits);
final int prefix = maxLengthInBits - length;
if (prefix < 0) {
return KeyAnalyzer.OUT_OF_BOUNDS_BIT_KEY;
}
for (int i = 0; i < length; i++) {
final int index = prefix + offsetInBits + i;
final boolean value = isBitSet(key, index, lengthInBits);
if (value) {
allNull = false;
}
final int otherIndex = prefix + otherOffsetInBits + i;
final boolean otherValue = isBitSet(other, otherIndex, otherLengthInBits);
if (value != otherValue) {
return index;
}
}
if (allNull) {
return KeyAnalyzer.NULL_BIT_KEY;
}
return KeyAnalyzer.EQUAL_BIT_KEY;
}
/**
* {@inheritDoc}
*/
public boolean isPrefix(final byte[] prefix, final int offsetInBits,
public boolean isPrefix(final byte[] prefix, final int offsetInBits,
final int lengthInBits, final byte[] key) {
final int keyLength = lengthInBits(key);
if (lengthInBits > keyLength) {
return false;
}
final int elements = lengthInBits - offsetInBits;
for (int i = 0; i < elements; i++) {
if (isBitSet(prefix, i+offsetInBits, lengthInBits)
if (isBitSet(prefix, i+offsetInBits, lengthInBits)
!= isBitSet(key, i, keyLength)) {
return false;
}
}
return true;
}
/**
* {@inheritDoc}
*/
@ -183,11 +183,11 @@ public class ByteArrayKeyAnalyzer extends AbstractKeyAnalyzer<byte[]> {
} else if (o2 == null) {
return 1;
}
if (o1.length != o2.length) {
return o1.length - o2.length;
}
for (int i = 0; i < o1.length; i++) {
final int diff = (o1[i] & 0xFF) - (o2[i] & 0xFF);
if (diff != 0) {

View File

@ -18,29 +18,29 @@ package org.apache.commons.collections4.trie;
/**
* A {@link KeyAnalyzer} for {@link Byte}s.
*
*
* @since 4.0
* @version $Id$
*/
public class ByteKeyAnalyzer extends AbstractKeyAnalyzer<Byte> {
private static final long serialVersionUID = 3395803342983289829L;
/**
* A singleton instance of {@link ByteKeyAnalyzer}
*/
public static final ByteKeyAnalyzer INSTANCE = new ByteKeyAnalyzer();
/**
* The length of an {@link Byte} in bits
*/
public static final int LENGTH = Byte.SIZE;
/**
* A bit mask where the first bit is 1 and the others are zero
*/
private static final int MSB = 0x80;
/**
* Returns a bit mask where the given bit is set
*/
@ -54,7 +54,7 @@ public class ByteKeyAnalyzer extends AbstractKeyAnalyzer<Byte> {
public int bitsPerElement() {
return 1;
}
/**
* {@inheritDoc}
*/
@ -72,21 +72,21 @@ public class ByteKeyAnalyzer extends AbstractKeyAnalyzer<Byte> {
/**
* {@inheritDoc}
*/
public int bitIndex(final Byte key, final int offsetInBits, final int lengthInBits,
public int bitIndex(final Byte key, final int offsetInBits, final int lengthInBits,
final Byte other, final int otherOffsetInBits, final int otherLengthInBits) {
if (offsetInBits != 0 || otherOffsetInBits != 0) {
throw new IllegalArgumentException("offsetInBits=" + offsetInBits
throw new IllegalArgumentException("offsetInBits=" + offsetInBits
+ ", otherOffsetInBits=" + otherOffsetInBits);
}
final byte keyValue = key.byteValue();
if (keyValue == 0) {
return NULL_BIT_KEY;
}
final byte otherValue = other != null ? other.byteValue() : 0;
if (keyValue != otherValue) {
final int xorValue = keyValue ^ otherValue;
for (int i = 0; i < LENGTH; i++) {
@ -95,24 +95,24 @@ public class ByteKeyAnalyzer extends AbstractKeyAnalyzer<Byte> {
}
}
}
return KeyAnalyzer.EQUAL_BIT_KEY;
}
/**
* {@inheritDoc}
*/
public boolean isPrefix(final Byte prefix, final int offsetInBits,
public boolean isPrefix(final Byte prefix, final int offsetInBits,
final int lengthInBits, final Byte key) {
final int value1 = prefix.byteValue() << offsetInBits;
final int value2 = key.byteValue();
int mask = 0;
for (int i = 0; i < lengthInBits; i++) {
mask |= 0x1 << i;
}
return (value1 & mask) == (value2 & mask);
}
}

View File

@ -18,12 +18,12 @@ package org.apache.commons.collections4.trie;
/**
* An {@link KeyAnalyzer} for {@code char[]}s.
*
*
* @since 4.0
* @version $Id$
*/
public class CharArrayKeyAnalyzer extends AbstractKeyAnalyzer<char[]> {
private static final long serialVersionUID = -8167897361549463457L;
/**

View File

@ -18,29 +18,29 @@ package org.apache.commons.collections4.trie;
/**
* A {@link KeyAnalyzer} for {@link Integer}s.
*
*
* @since 4.0
* @version $Id$
*/
public class IntegerKeyAnalyzer extends AbstractKeyAnalyzer<Integer> {
private static final long serialVersionUID = 4928508653722068982L;
/**
* A singleton instance of {@link IntegerKeyAnalyzer}
*/
public static final IntegerKeyAnalyzer INSTANCE = new IntegerKeyAnalyzer();
/**
* The length of an {@link Integer} in bits
*/
public static final int LENGTH = Integer.SIZE;
/**
* A bit mask where the first bit is 1 and the others are zero
*/
private static final int MSB = 0x80000000;
/**
* Returns a bit mask where the given bit is set
*/
@ -54,7 +54,7 @@ public class IntegerKeyAnalyzer extends AbstractKeyAnalyzer<Integer> {
public int bitsPerElement() {
return 1;
}
/**
* {@inheritDoc}
*/
@ -72,21 +72,21 @@ public class IntegerKeyAnalyzer extends AbstractKeyAnalyzer<Integer> {
/**
* {@inheritDoc}
*/
public int bitIndex(final Integer key, final int offsetInBits, final int lengthInBits,
public int bitIndex(final Integer key, final int offsetInBits, final int lengthInBits,
final Integer other, final int otherOffsetInBits, final int otherLengthInBits) {
if (offsetInBits != 0 || otherOffsetInBits != 0) {
throw new IllegalArgumentException("offsetInBits=" + offsetInBits
throw new IllegalArgumentException("offsetInBits=" + offsetInBits
+ ", otherOffsetInBits=" + otherOffsetInBits);
}
final int keyValue = key.intValue();
if (keyValue == 0) {
return NULL_BIT_KEY;
}
final int otherValue = other != null ? other.intValue() : 0;
if (keyValue != otherValue) {
final int xorValue = keyValue ^ otherValue;
for (int i = 0; i < LENGTH; i++) {
@ -95,24 +95,24 @@ public class IntegerKeyAnalyzer extends AbstractKeyAnalyzer<Integer> {
}
}
}
return KeyAnalyzer.EQUAL_BIT_KEY;
}
/**
* {@inheritDoc}
*/
public boolean isPrefix(final Integer prefix, final int offsetInBits,
public boolean isPrefix(final Integer prefix, final int offsetInBits,
final int lengthInBits, final Integer key) {
final int value1 = prefix.intValue() << offsetInBits;
final int value2 = key.intValue();
int mask = 0;
for (int i = 0; i < lengthInBits; i++) {
mask |= 0x1 << i;
}
return (value1 & mask) == (value2 & mask);
}
}

View File

@ -19,57 +19,57 @@ package org.apache.commons.collections4.trie;
import java.io.Serializable;
import java.util.Comparator;
/**
* Defines the interface to analyze {@link org.apache.commons.collections4.Trie Trie} keys on a bit level.
* {@link KeyAnalyzer}'s methods return the length of the key in bits,
* whether or not a bit is set, and bits per element in the key.
/**
* Defines the interface to analyze {@link org.apache.commons.collections4.Trie Trie} keys on a bit level.
* {@link KeyAnalyzer}'s methods return the length of the key in bits,
* whether or not a bit is set, and bits per element in the key.
* <p>
* Additionally, a method determines if a key is a prefix of another
* key and returns the bit index where one key is different from another
* key (if the key and found key are equal than the return value is
* Additionally, a method determines if a key is a prefix of another
* key and returns the bit index where one key is different from another
* key (if the key and found key are equal than the return value is
* {@link #EQUAL_BIT_KEY}).
*
*
* @since 4.0
* @version $Id$
*/
public interface KeyAnalyzer<K> extends Comparator<K>, Serializable {
/**
* Returned by {@link #bitIndex(Object, int, int, Object, int, int)}
* if key's bits are all 0
/**
* Returned by {@link #bitIndex(Object, int, int, Object, int, int)}
* if key's bits are all 0
*/
public static final int NULL_BIT_KEY = -1;
/**
* Returned by {@link #bitIndex(Object, int, int, Object, int, int)}
* if key and found key are equal. This is a very very specific case
/**
* Returned by {@link #bitIndex(Object, int, int, Object, int, int)}
* if key and found key are equal. This is a very very specific case
* and shouldn't happen on a regular basis
*/
public static final int EQUAL_BIT_KEY = -2;
public static final int OUT_OF_BOUNDS_BIT_KEY = -3;
/**
* Returns the number of bits per element in the key.
* This is only useful for variable-length keys, such as Strings.
*
*
* @return the number of bits per element
*/
public int bitsPerElement();
/**
/**
* Returns the length of the Key in bits.
*
*
* @param key the key
* @return the bit length of the key
*/
public int lengthInBits(K key);
/**
/**
* Returns whether or not a bit is set.
*/
public boolean isBitSet(K key, int bitIndex, int lengthInBits);
/**
* Returns the n-th different bit between key and found. This starts the comparison in
* key at 'keyStart' and goes for 'keyLength' bits, and compares to the found key starting
@ -77,7 +77,7 @@ public interface KeyAnalyzer<K> extends Comparator<K>, Serializable {
*/
public int bitIndex(K key, int offsetInBits, int lengthInBits,
K other, int otherOffsetInBits, int otherLengthInBits);
/**
* Determines whether or not the given prefix (from offset to length) is a prefix of the given key.
*/

View File

@ -18,29 +18,29 @@ package org.apache.commons.collections4.trie;
/**
* A {@link KeyAnalyzer} for {@link Long}s.
*
*
* @since 4.0
* @version $Id$
*/
public class LongKeyAnalyzer extends AbstractKeyAnalyzer<Long> {
private static final long serialVersionUID = -4119639247588227409L;
/**
* A singleton instance of {@link LongKeyAnalyzer}
*/
public static final LongKeyAnalyzer INSTANCE = new LongKeyAnalyzer();
/**
* The length of an {@link Long} in bits
*/
public static final int LENGTH = Long.SIZE;
/**
* A bit mask where the first bit is 1 and the others are zero
*/
private static final long MSB = 0x8000000000000000L;
/**
* Returns a bit mask where the given bit is set
*/
@ -54,7 +54,7 @@ public class LongKeyAnalyzer extends AbstractKeyAnalyzer<Long> {
public int bitsPerElement() {
return 1;
}
/**
* {@inheritDoc}
*/
@ -72,21 +72,21 @@ public class LongKeyAnalyzer extends AbstractKeyAnalyzer<Long> {
/**
* {@inheritDoc}
*/
public int bitIndex(final Long key, final int offsetInBits, final int lengthInBits,
public int bitIndex(final Long key, final int offsetInBits, final int lengthInBits,
final Long other, final int otherOffsetInBits, final int otherLengthInBits) {
if (offsetInBits != 0 || otherOffsetInBits != 0) {
throw new IllegalArgumentException("offsetInBits=" + offsetInBits
throw new IllegalArgumentException("offsetInBits=" + offsetInBits
+ ", otherOffsetInBits=" + otherOffsetInBits);
}
final long keyValue = key.longValue();
if (keyValue == 0L) {
return NULL_BIT_KEY;
}
final long otherValue = other != null ? other.longValue() : 0L;
if (keyValue != otherValue) {
final long xorValue = keyValue ^ otherValue;
for (int i = 0; i < LENGTH; i++) {
@ -95,24 +95,24 @@ public class LongKeyAnalyzer extends AbstractKeyAnalyzer<Long> {
}
}
}
return KeyAnalyzer.EQUAL_BIT_KEY;
}
/**
* {@inheritDoc}
*/
public boolean isPrefix(final Long prefix, final int offsetInBits,
public boolean isPrefix(final Long prefix, final int offsetInBits,
final int lengthInBits, final Long key) {
final long value1 = prefix.longValue() << offsetInBits;
final long value2 = key.longValue();
long mask = 0L;
for (int i = 0; i < lengthInBits; i++) {
mask |= 0x1L << i;
}
return (value1 & mask) == (value2 & mask);
}
}

View File

@ -18,29 +18,29 @@ package org.apache.commons.collections4.trie;
/**
* A {@link KeyAnalyzer} for {@link Short}s.
*
*
* @since 4.0
* @version $Id$
*/
public class ShortKeyAnalyzer implements KeyAnalyzer<Short> {
private static final long serialVersionUID = -8631376733513512017L;
/**
* A singleton instance of {@link ShortKeyAnalyzer}
*/
public static final ShortKeyAnalyzer INSTANCE = new ShortKeyAnalyzer();
/**
* The length of an {@link Short} in bits
*/
public static final int LENGTH = Short.SIZE;
/**
* A bit mask where the first bit is 1 and the others are zero
*/
private static final int MSB = 0x8000;
/**
* Returns a bit mask where the given bit is set
*/
@ -54,7 +54,7 @@ public class ShortKeyAnalyzer implements KeyAnalyzer<Short> {
public int bitsPerElement() {
return 1;
}
/**
* {@inheritDoc}
*/
@ -72,21 +72,21 @@ public class ShortKeyAnalyzer implements KeyAnalyzer<Short> {
/**
* {@inheritDoc}
*/
public int bitIndex(final Short key, final int offsetInBits, final int lengthInBits,
public int bitIndex(final Short key, final int offsetInBits, final int lengthInBits,
final Short other, final int otherOffsetInBits, final int otherLengthInBits) {
if (offsetInBits != 0 || otherOffsetInBits != 0) {
throw new IllegalArgumentException("offsetInBits=" + offsetInBits
throw new IllegalArgumentException("offsetInBits=" + offsetInBits
+ ", otherOffsetInBits=" + otherOffsetInBits);
}
final int keyValue = key.shortValue();
if (keyValue == 0) {
return NULL_BIT_KEY;
}
final int otherValue = other != null ? other.shortValue() : 0;
if (keyValue != otherValue) {
final int xorValue = keyValue ^ otherValue;
for (int i = 0; i < LENGTH; i++) {
@ -95,7 +95,7 @@ public class ShortKeyAnalyzer implements KeyAnalyzer<Short> {
}
}
}
return KeyAnalyzer.EQUAL_BIT_KEY;
}
@ -105,21 +105,21 @@ public class ShortKeyAnalyzer implements KeyAnalyzer<Short> {
public int compare(final Short o1, final Short o2) {
return o1.compareTo(o2);
}
/**
* {@inheritDoc}
*/
public boolean isPrefix(final Short prefix, final int offsetInBits,
public boolean isPrefix(final Short prefix, final int offsetInBits,
final int lengthInBits, final Short key) {
final int value1 = prefix.shortValue() << offsetInBits;
final int value2 = key.shortValue();
int mask = 0;
for (int i = 0; i < lengthInBits; i++) {
mask |= 0x1 << i;
}
return (value1 & mask) == (value2 & mask);
}
}

View File

@ -18,72 +18,72 @@ package org.apache.commons.collections4.trie;
/**
* An {@link KeyAnalyzer} for {@link String}s.
*
*
* @since 4.0
* @version $Id$
*/
public class StringKeyAnalyzer extends AbstractKeyAnalyzer<String> {
private static final long serialVersionUID = -7032449491269434877L;
/**
* A singleton instance of {@link StringKeyAnalyzer}
*/
public static final StringKeyAnalyzer INSTANCE = new StringKeyAnalyzer();
/**
* The number of bits per {@link Character}
*/
public static final int LENGTH = Character.SIZE;
/**
* A bit mask where the first bit is 1 and the others are zero
*/
private static final int MSB = 0x8000;
/**
* Returns a bit mask where the given bit is set
*/
private static int mask(final int bit) {
return MSB >>> bit;
}
/**
* {@inheritDoc}
*/
public int bitsPerElement() {
return LENGTH;
}
/**
* {@inheritDoc}
*/
public int lengthInBits(final String key) {
return key != null ? key.length() * LENGTH : 0;
}
/**
* {@inheritDoc}
*/
public int bitIndex(final String key, final int offsetInBits, final int lengthInBits,
final String other, final int otherOffsetInBits, final int otherLengthInBits) {
boolean allNull = true;
if (offsetInBits % LENGTH != 0 || otherOffsetInBits % LENGTH != 0
if (offsetInBits % LENGTH != 0 || otherOffsetInBits % LENGTH != 0
|| lengthInBits % LENGTH != 0 || otherLengthInBits % LENGTH != 0) {
throw new IllegalArgumentException(
"The offsets and lengths must be at Character boundaries");
}
final int beginIndex1 = offsetInBits / LENGTH;
final int beginIndex2 = otherOffsetInBits / LENGTH;
final int endIndex1 = beginIndex1 + lengthInBits / LENGTH;
final int endIndex2 = beginIndex2 + otherLengthInBits / LENGTH;
final int length = Math.max(endIndex1, endIndex2);
// Look at each character, and if they're different
// then figure out which bit makes the difference
// and return it.
@ -91,38 +91,38 @@ public class StringKeyAnalyzer extends AbstractKeyAnalyzer<String> {
for(int i = 0; i < length; i++) {
final int index1 = beginIndex1 + i;
final int index2 = beginIndex2 + i;
if (index1 >= endIndex1) {
k = 0;
} else {
k = key.charAt(index1);
}
if (other == null || index2 >= endIndex2) {
f = 0;
} else {
f = other.charAt(index2);
}
if (k != f) {
final int x = k ^ f;
return i * LENGTH + Integer.numberOfLeadingZeros(x) - LENGTH;
}
if (k != 0) {
allNull = false;
}
}
// All bits are 0
if (allNull) {
return KeyAnalyzer.NULL_BIT_KEY;
}
// Both keys are equal
return KeyAnalyzer.EQUAL_BIT_KEY;
}
/**
* {@inheritDoc}
*/
@ -130,23 +130,23 @@ public class StringKeyAnalyzer extends AbstractKeyAnalyzer<String> {
if (key == null || bitIndex >= lengthInBits) {
return false;
}
final int index = bitIndex / LENGTH;
final int bit = bitIndex % LENGTH;
return (key.charAt(index) & mask(bit)) != 0;
}
/**
* {@inheritDoc}
*/
public boolean isPrefix(final String prefix, final int offsetInBits,
public boolean isPrefix(final String prefix, final int offsetInBits,
final int lengthInBits, final String key) {
if (offsetInBits % LENGTH != 0 || lengthInBits % LENGTH != 0) {
throw new IllegalArgumentException(
"Cannot determine prefix outside of Character boundaries");
}
final String s1 = prefix.substring(offsetInBits / LENGTH, lengthInBits / LENGTH);
return key.startsWith(s1);
}

View File

@ -29,19 +29,19 @@ import org.apache.commons.collections4.collection.SynchronizedCollection;
/**
* A synchronized {@link Trie}.
*
*
* @since 4.0
* @version $Id$
*/
public class SynchronizedTrie<K, V> implements Trie<K, V>, Serializable {
private static final long serialVersionUID = 3121878833178676939L;
private final Trie<K, V> delegate;
/**
* Factory method to create a synchronized trie.
*
*
* @param <K> the key type
* @param <V> the value type
* @param trie the trie to decorate, must not be null
@ -55,7 +55,7 @@ public class SynchronizedTrie<K, V> implements Trie<K, V>, Serializable {
//-----------------------------------------------------------------------
/**
* Constructor that wraps (not copies).
*
*
* @param trie the trie to decorate, must not be null
* @throws IllegalArgumentException if set is null
*/
@ -85,7 +85,7 @@ public class SynchronizedTrie<K, V> implements Trie<K, V>, Serializable {
public synchronized Entry<K, V> traverse(final Cursor<? super K, ? super V> cursor) {
return delegate.traverse(cursor);
}
public synchronized Set<Entry<K, V>> entrySet() {
return Collections.synchronizedSet(delegate.entrySet());
}
@ -129,7 +129,7 @@ public class SynchronizedTrie<K, V> implements Trie<K, V>, Serializable {
public synchronized V remove(final Object key) {
return delegate.remove(key);
}
public synchronized K lastKey() {
return delegate.lastKey();
}
@ -141,7 +141,7 @@ public class SynchronizedTrie<K, V> implements Trie<K, V>, Serializable {
public synchronized SortedMap<K, V> tailMap(final K fromKey) {
return Collections.synchronizedSortedMap(delegate.tailMap(fromKey));
}
public synchronized Comparator<? super K> comparator() {
return delegate.comparator();
}
@ -153,7 +153,7 @@ public class SynchronizedTrie<K, V> implements Trie<K, V>, Serializable {
public synchronized SortedMap<K, V> headMap(final K toKey) {
return Collections.synchronizedSortedMap(delegate.headMap(toKey));
}
public synchronized SortedMap<K, V> getPrefixedBy(final K key, final int offset, final int length) {
return Collections.synchronizedSortedMap(delegate.getPrefixedBy(key, offset, length));
}
@ -170,7 +170,7 @@ public class SynchronizedTrie<K, V> implements Trie<K, V>, Serializable {
return Collections.synchronizedSortedMap(delegate.getPrefixedByBits(key, lengthInBits));
}
public synchronized SortedMap<K, V> getPrefixedByBits(final K key,
public synchronized SortedMap<K, V> getPrefixedByBits(final K key,
final int offsetInBits, final int lengthInBits) {
return Collections.synchronizedSortedMap(delegate.getPrefixedByBits(key, offsetInBits, lengthInBits));
}
@ -183,12 +183,12 @@ public class SynchronizedTrie<K, V> implements Trie<K, V>, Serializable {
public synchronized int hashCode() {
return delegate.hashCode();
}
@Override
public synchronized boolean equals(final Object obj) {
return delegate.equals(obj);
}
@Override
public synchronized String toString() {
return delegate.toString();

View File

@ -29,19 +29,19 @@ import org.apache.commons.collections4.Unmodifiable;
/**
* An unmodifiable {@link Trie}.
*
*
* @since 4.0
* @version $Id$
*/
public class UnmodifiableTrie<K, V> implements Trie<K, V>, Serializable, Unmodifiable {
private static final long serialVersionUID = -7156426030315945159L;
private final Trie<K, V> delegate;
/**
* Factory method to create a unmodifiable trie.
*
*
* @param <K> the key type
* @param <V> the value type
* @param trie the trie to decorate, must not be null
@ -55,7 +55,7 @@ public class UnmodifiableTrie<K, V> implements Trie<K, V>, Serializable, Unmodif
//-----------------------------------------------------------------------
/**
* Constructor that wraps (not copies).
*
*
* @param trie the trie to decorate, must not be null
* @throws IllegalArgumentException if trie is null
*/
@ -65,7 +65,7 @@ public class UnmodifiableTrie<K, V> implements Trie<K, V>, Serializable, Unmodif
}
this.delegate = trie;
}
public Entry<K, V> select(final K key, final Cursor<? super K, ? super V> cursor) {
final Cursor<K, V> c = new Cursor<K, V>() {
public Decision select(final Map.Entry<? extends K, ? extends V> entry) {
@ -78,11 +78,11 @@ public class UnmodifiableTrie<K, V> implements Trie<K, V>, Serializable, Unmodif
// other decisions are fine
break;
}
return decision;
}
};
return delegate.select(key, c);
}
@ -110,18 +110,18 @@ public class UnmodifiableTrie<K, V> implements Trie<K, V>, Serializable, Unmodif
// other decisions are fine
break;
}
return decision;
}
};
return delegate.traverse(c);
}
public Set<Entry<K, V>> entrySet() {
return Collections.unmodifiableSet(delegate.entrySet());
}
public Set<K> keySet() {
return Collections.unmodifiableSet(delegate.keySet());
}
@ -182,7 +182,7 @@ public class UnmodifiableTrie<K, V> implements Trie<K, V>, Serializable, Unmodif
public SortedMap<K, V> tailMap(final K fromKey) {
return Collections.unmodifiableSortedMap(delegate.tailMap(fromKey));
}
public SortedMap<K, V> getPrefixedBy(final K key, final int offset, final int length) {
return Collections.unmodifiableSortedMap(
delegate.getPrefixedBy(key, offset, length));
@ -202,7 +202,7 @@ public class UnmodifiableTrie<K, V> implements Trie<K, V>, Serializable, Unmodif
return Collections.unmodifiableSortedMap(
delegate.getPrefixedByBits(key, lengthInBits));
}
public SortedMap<K, V> getPrefixedByBits(final K key, final int offsetInBits, final int lengthInBits) {
return Collections.unmodifiableSortedMap(delegate.getPrefixedByBits(key, offsetInBits, lengthInBits));
}
@ -210,21 +210,21 @@ public class UnmodifiableTrie<K, V> implements Trie<K, V>, Serializable, Unmodif
public Comparator<? super K> comparator() {
return delegate.comparator();
}
public int size() {
return delegate.size();
}
@Override
public int hashCode() {
return delegate.hashCode();
}
@Override
public boolean equals(final Object obj) {
return delegate.equals(obj);
}
@Override
public String toString() {
return delegate.toString();