KeyAnalyzer.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4.trie;

  18. import java.io.Serializable;
  19. import java.util.Comparator;

  20. /**
  21.  * Defines the interface to analyze {@link org.apache.commons.collections4.Trie Trie} keys on a bit level.
  22.  * {@link KeyAnalyzer}'s methods return the length of the key in bits, whether or not a bit is set,
  23.  * and bits per element in the key.
  24.  * <p>
  25.  * Additionally, a method determines if a key is a prefix of another
  26.  * key and returns the bit index where one key is different from another
  27.  * key (if the key and found key are equal than the return value is
  28.  * {@link #EQUAL_BIT_KEY}).
  29.  * </p>
  30.  *
  31.  * @param <K> the type of objects that may be compared by this analyzer
  32.  * @since 4.0
  33.  */
  34. public abstract class KeyAnalyzer<K> implements Comparator<K>, Serializable {

  35.     /** Serialization version */
  36.     private static final long serialVersionUID = -20497563720380683L;

  37.     /**
  38.      * Returned by {@link #bitIndex(Object, int, int, Object, int, int)}
  39.      * if key's bits are all 0.
  40.      */
  41.     public static final int NULL_BIT_KEY = -1;

  42.     /**
  43.      * Returned by {@link #bitIndex(Object, int, int, Object, int, int)} if key and found key are equal.
  44.      * This is a very specific case and shouldn't happen on a regular basis.
  45.      */
  46.     public static final int EQUAL_BIT_KEY = -2;

  47.     /**
  48.      * Used to test a {@code bitIndex} in {@link #isOutOfBoundsIndex(int)}.
  49.      */
  50.     public static final int OUT_OF_BOUNDS_BIT_KEY = -3;

  51.     /**
  52.      * Returns true if bitIndex is a {@link KeyAnalyzer#EQUAL_BIT_KEY}.
  53.      */
  54.     static boolean isEqualBitKey(final int bitIndex) {
  55.         return bitIndex == EQUAL_BIT_KEY;
  56.     }

  57.     /**
  58.      * Returns true if bitIndex is a {@link KeyAnalyzer#NULL_BIT_KEY}.
  59.      */
  60.     static boolean isNullBitKey(final int bitIndex) {
  61.         return bitIndex == NULL_BIT_KEY;
  62.     }

  63.     /**
  64.      * Returns true if bitIndex is a {@link KeyAnalyzer#OUT_OF_BOUNDS_BIT_KEY}.
  65.      */
  66.     static boolean isOutOfBoundsIndex(final int bitIndex) {
  67.         return bitIndex == OUT_OF_BOUNDS_BIT_KEY;
  68.     }

  69.     /**
  70.      * Returns true if the given bitIndex is valid.
  71.      * Indices are considered valid if they're between 0 and {@link Integer#MAX_VALUE}
  72.      */
  73.     static boolean isValidBitIndex(final int bitIndex) {
  74.         return bitIndex >= 0;
  75.     }

  76.     /**
  77.      * Constructs a new instance.
  78.      */
  79.     public KeyAnalyzer() {
  80.         // empty
  81.     }

  82.     /**
  83.      * Returns the n-th different bit between key and other. This starts the comparison in
  84.      * key at 'offsetInBits' and goes for 'lengthInBits' bits, and compares to the other key starting
  85.      * at 'otherOffsetInBits' and going for 'otherLengthInBits' bits.
  86.      *
  87.      * @param key  the key to use
  88.      * @param offsetInBits  the bit offset in the key
  89.      * @param lengthInBits  the maximum key length in bits to use
  90.      * @param other  the other key to use
  91.      * @param otherOffsetInBits  the bit offset in the other key
  92.      * @param otherLengthInBits  the maximum key length in bits for the other key
  93.      * @return the bit index where the key and other first differ
  94.      */
  95.     public abstract int bitIndex(K key, int offsetInBits, int lengthInBits,
  96.                                  K other, int otherOffsetInBits, int otherLengthInBits);

  97.     /**
  98.      * Returns the number of bits per element in the key.
  99.      * This is only useful for variable-length keys, such as Strings.
  100.      *
  101.      * @return the number of bits per element
  102.      */
  103.     public abstract int bitsPerElement();

  104.     @Override
  105.     @SuppressWarnings("unchecked")
  106.     public int compare(final K o1, final K o2) {
  107.         if (o1 == null) {
  108.             return o2 == null ? 0 : -1;
  109.         }
  110.         if (o2 == null) {
  111.             return 1;
  112.         }

  113.         return ((Comparable<K>) o1).compareTo(o2);
  114.     }

  115.     /**
  116.      * Returns whether or not a bit is set.
  117.      *
  118.      * @param key  the key to check, may not be null
  119.      * @param bitIndex  the bit index to check
  120.      * @param lengthInBits  the maximum key length in bits to check
  121.      * @return {@code true} if the bit is set in the given key and
  122.      *   {@code bitIndex} &lt; {@code lengthInBits}, {@code false} otherwise.
  123.      */
  124.     public abstract boolean isBitSet(K key, int bitIndex, int lengthInBits);

  125.     /**
  126.      * Determines whether or not the given prefix (from offset to length) is a prefix of the given key.
  127.      *
  128.      * @param prefix  the prefix to check
  129.      * @param offsetInBits  the bit offset in the key
  130.      * @param lengthInBits  the maximum key length in bits to use
  131.      * @param key  the key to check
  132.      * @return {@code true} if this is a valid prefix for the given key
  133.      */
  134.     public abstract boolean isPrefix(K prefix, int offsetInBits, int lengthInBits, K key);

  135.     /**
  136.      * Returns the length of the Key in bits.
  137.      *
  138.      * @param key  the key
  139.      * @return the bit length of the key
  140.      */
  141.     public abstract int lengthInBits(K key);

  142. }