Example #1
0
  /**
   * Performs a comparison of the contents of substring filters.
   *
   * @param f1 The first filter for which to make the determination.
   * @param f2 The second filter for which to make the determination.
   * @return A negative value if the first filter should be ordered before the second, a positive
   *     value if the first filter should be ordered after the second, or zero if there is no
   *     difference in their relative orders.
   */
  private static int compareSubstring(final Filter f1, final Filter f2) {
    final byte[] sI1 = f1.getSubInitialBytes();
    final byte[] sI2 = f2.getSubInitialBytes();
    if (sI1 == null) {
      if (sI2 != null) {
        return -1;
      }
    } else if (sI2 == null) {
      return 1;
    } else {
      final int cmpValue = compare(sI1, sI2);
      if (cmpValue != 0) {
        return cmpValue;
      }
    }

    final byte[][] sA1 = f1.getSubAnyBytes();
    final byte[][] sA2 = f2.getSubAnyBytes();
    if (sA1.length == 0) {
      if (sA2.length > 0) {
        return -1;
      }
    } else if (sA2.length == 0) {
      return 1;
    } else {
      final int minLength = Math.min(sA1.length, sA2.length);
      for (int i = 0; i < minLength; i++) {
        final int cmpValue = compare(sA1[i], sA2[i]);
        if (cmpValue != 0) {
          return cmpValue;
        }
      }

      if (sA1.length < sA2.length) {
        return -1;
      } else if (sA2.length < sA1.length) {
        return 1;
      }
    }

    final byte[] sF1 = f1.getSubFinalBytes();
    final byte[] sF2 = f2.getSubFinalBytes();
    if (sF1 == null) {
      if (sF2 != null) {
        return -1;
      } else {
        return 0;
      }
    } else if (sF2 == null) {
      return 1;
    } else {
      return compare(sF1, sF2);
    }
  }
Example #2
0
  /**
   * Performs a comparison of the contents of the provided byte arrays.
   *
   * @param a1 The first array to be compared.
   * @param a2 The second array to be compared.
   * @return An integer value denoting the comparison value.
   */
  private static int compare(final byte[] a1, final byte[] a2) {
    final int length = Math.min(a1.length, a2.length);

    for (int i = 0; i < length; i++) {
      final int b1 = 0xFF & a1[i];
      final int b2 = 0xFF & a2[i];
      if (b1 != b2) {
        return b1 - b2;
      }
    }

    return (a1.length - a2.length);
  }