Java Code Examples for com.google.common.primitives.Booleans#compare()

The following examples show how to use com.google.common.primitives.Booleans#compare() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Cut.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public int compareTo(Cut<C> that) {
  if (that == belowAll()) {
    return 1;
  }
  if (that == aboveAll()) {
    return -1;
  }
  int result = Range.compareOrThrow(endpoint, that.endpoint);
  if (result != 0) {
    return result;
  }
  // same value. below comes before above
  return Booleans.compare(this instanceof AboveValue, that instanceof AboveValue);
}
 
Example 2
Source File: Cut.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public int compareTo(Cut<C> that) {
  if (that == belowAll()) {
    return 1;
  }
  if (that == aboveAll()) {
    return -1;
  }
  int result = Range.compareOrThrow(endpoint, that.endpoint);
  if (result != 0) {
    return result;
  }
  // same value. below comes before above
  return Booleans.compare(this instanceof AboveValue, that instanceof AboveValue);
}
 
Example 3
Source File: Cut.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public int compareTo(Cut<C> that) {
  if (that == belowAll()) {
    return 1;
  }
  if (that == aboveAll()) {
    return -1;
  }
  int result = Range.compareOrThrow(endpoint, that.endpoint);
  if (result != 0) {
    return result;
  }
  // same value. below comes before above
  return Booleans.compare(this instanceof AboveValue, that instanceof AboveValue);
}
 
Example 4
Source File: Cut.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public int compareTo(Cut<C> that) {
  if (that == belowAll()) {
    return 1;
  }
  if (that == aboveAll()) {
    return -1;
  }
  int result = Range.compareOrThrow(endpoint, that.endpoint);
  if (result != 0) {
    return result;
  }
  // same value. below comes before above
  return Booleans.compare(this instanceof AboveValue, that instanceof AboveValue);
}
 
Example 5
Source File: PBoolean.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public int compareTo(Object lhs, Object rhs, PDataType rhsType) {
    if (lhs == rhs) {
        return 0;
    }
    if (lhs == null) {
        return -1;
    }
    if (rhs == null) {
        return 1;
    }
    return Booleans.compare((Boolean) lhs, (Boolean) rhs);
}
 
Example 6
Source File: Cut.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public int compareTo(Cut<C> that) {
  if (that == belowAll()) {
    return 1;
  }
  if (that == aboveAll()) {
    return -1;
  }
  int result = Range.compareOrThrow(endpoint, that.endpoint);
  if (result != 0) {
    return result;
  }
  // same value. below comes before above
  return Booleans.compare(this instanceof AboveValue, that instanceof AboveValue);
}
 
Example 7
Source File: BaseBooleanEncodedValue.java    From HeyGirl with Apache License 2.0 4 votes vote down vote up
@Override
public int compareTo(@Nonnull EncodedValue o) {
    int res = Ints.compare(getValueType(), o.getValueType());
    if (res != 0) return res;
    return Booleans.compare(getValue(), ((BooleanEncodedValue)o).getValue());
}
 
Example 8
Source File: BaseBooleanEncodedValue.java    From ZjDroid with Apache License 2.0 4 votes vote down vote up
@Override
public int compareTo(@Nonnull EncodedValue o) {
    int res = Ints.compare(getValueType(), o.getValueType());
    if (res != 0) return res;
    return Booleans.compare(getValue(), ((BooleanEncodedValue)o).getValue());
}
 
Example 9
Source File: BaseBooleanEncodedValue.java    From AppTroy with Apache License 2.0 4 votes vote down vote up
@Override
public int compareTo(@Nonnull EncodedValue o) {
    int res = Ints.compare(getValueType(), o.getValueType());
    if (res != 0) return res;
    return Booleans.compare(getValue(), ((BooleanEncodedValue)o).getValue());
}
 
Example 10
Source File: Quicksortables.java    From util with Apache License 2.0 4 votes vote down vote up
public static int compare(boolean[] a, int i, int j) {
    return Booleans.compare(a[i], a[j]);
}
 
Example 11
Source File: BaseBooleanEncodedValue.java    From zjdroid with Apache License 2.0 4 votes vote down vote up
@Override
public int compareTo(@Nonnull EncodedValue o) {
    int res = Ints.compare(getValueType(), o.getValueType());
    if (res != 0) return res;
    return Booleans.compare(getValue(), ((BooleanEncodedValue)o).getValue());
}
 
Example 12
Source File: SegmentRecoveryComparator.java    From big-c with Apache License 2.0 4 votes vote down vote up
@Override
public int compare(
    Entry<AsyncLogger, PrepareRecoveryResponseProto> a,
    Entry<AsyncLogger, PrepareRecoveryResponseProto> b) {
  
  PrepareRecoveryResponseProto r1 = a.getValue();
  PrepareRecoveryResponseProto r2 = b.getValue();
  
  // A response that has data for a segment is always better than one
  // that doesn't.
  if (r1.hasSegmentState() != r2.hasSegmentState()) {
    return Booleans.compare(r1.hasSegmentState(), r2.hasSegmentState());
  }
  
  if (!r1.hasSegmentState()) {
    // Neither has a segment, so neither can be used for recover.
    // Call them equal.
    return 0;
  }
  
  // They both have a segment.
  SegmentStateProto r1Seg = r1.getSegmentState();
  SegmentStateProto r2Seg = r2.getSegmentState();
  
  Preconditions.checkArgument(r1Seg.getStartTxId() == r2Seg.getStartTxId(),
      "Should only be called with responses for corresponding segments: " +
      "%s and %s do not have the same start txid.", r1, r2);

  // If one is in-progress but the other is finalized,
  // the finalized one is greater.
  if (r1Seg.getIsInProgress() != r2Seg.getIsInProgress()) {
    return Booleans.compare(!r1Seg.getIsInProgress(), !r2Seg.getIsInProgress());
  }
  
  if (!r1Seg.getIsInProgress()) {
    // If both are finalized, they should match lengths
    if (r1Seg.getEndTxId() != r2Seg.getEndTxId()) {
      throw new AssertionError("finalized segs with different lengths: " + 
          r1 + ", " + r2);
    }
    return 0;
  }
  
  // Both are in-progress.
  long r1SeenEpoch = Math.max(r1.getAcceptedInEpoch(), r1.getLastWriterEpoch());
  long r2SeenEpoch = Math.max(r2.getAcceptedInEpoch(), r2.getLastWriterEpoch());
  
  return ComparisonChain.start()
      .compare(r1SeenEpoch, r2SeenEpoch)
      .compare(r1.getSegmentState().getEndTxId(), r2.getSegmentState().getEndTxId())
      .result();
}
 
Example 13
Source File: PBoolean.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@Override
public int compareTo(Object lhs, Object rhs, PDataType rhsType) {
  return Booleans.compare((Boolean) lhs, (Boolean) rhs);
}
 
Example 14
Source File: SegmentRecoveryComparator.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@Override
public int compare(
    Entry<AsyncLogger, PrepareRecoveryResponseProto> a,
    Entry<AsyncLogger, PrepareRecoveryResponseProto> b) {
  
  PrepareRecoveryResponseProto r1 = a.getValue();
  PrepareRecoveryResponseProto r2 = b.getValue();
  
  // A response that has data for a segment is always better than one
  // that doesn't.
  if (r1.hasSegmentState() != r2.hasSegmentState()) {
    return Booleans.compare(r1.hasSegmentState(), r2.hasSegmentState());
  }
  
  if (!r1.hasSegmentState()) {
    // Neither has a segment, so neither can be used for recover.
    // Call them equal.
    return 0;
  }
  
  // They both have a segment.
  SegmentStateProto r1Seg = r1.getSegmentState();
  SegmentStateProto r2Seg = r2.getSegmentState();
  
  Preconditions.checkArgument(r1Seg.getStartTxId() == r2Seg.getStartTxId(),
      "Should only be called with responses for corresponding segments: " +
      "%s and %s do not have the same start txid.", r1, r2);

  // If one is in-progress but the other is finalized,
  // the finalized one is greater.
  if (r1Seg.getIsInProgress() != r2Seg.getIsInProgress()) {
    return Booleans.compare(!r1Seg.getIsInProgress(), !r2Seg.getIsInProgress());
  }
  
  if (!r1Seg.getIsInProgress()) {
    // If both are finalized, they should match lengths
    if (r1Seg.getEndTxId() != r2Seg.getEndTxId()) {
      throw new AssertionError("finalized segs with different lengths: " + 
          r1 + ", " + r2);
    }
    return 0;
  }
  
  // Both are in-progress.
  long r1SeenEpoch = Math.max(r1.getAcceptedInEpoch(), r1.getLastWriterEpoch());
  long r2SeenEpoch = Math.max(r2.getAcceptedInEpoch(), r2.getLastWriterEpoch());
  
  return ComparisonChain.start()
      .compare(r1SeenEpoch, r2SeenEpoch)
      .compare(r1.getSegmentState().getEndTxId(), r2.getSegmentState().getEndTxId())
      .result();
}
 
Example 15
Source File: DoubleMath.java    From codebuff with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * Compares {@code a} and {@code b} "fuzzily," with a tolerance for nearly-equal values.
 *
 * <p>This method is equivalent to
 * {@code fuzzyEquals(a, b, tolerance) ? 0 : Double.compare(a, b)}. In particular, like
 * {@link Double#compare(double, double)}, it treats all NaN values as equal and greater than all
 * other values (including {@link Double#POSITIVE_INFINITY}).
 *
 * <p>This is <em>not</em> a total ordering and is <em>not</em> suitable for use in
 * {@link Comparable#compareTo} implementations. In particular, it is not transitive.
 *
 * @throws IllegalArgumentException if {@code tolerance} is {@code < 0} or NaN
 * @since 13.0
 */


public static int fuzzyCompare(double a, double b, double tolerance) {
  if (fuzzyEquals(a, b, tolerance)) {
    return 0;
  } else if (a < b) {
    return -1;
  } else if (a > b) {
    return 1;
  } else {
    return Booleans.compare(Double.isNaN(a), Double.isNaN(b));
  }
}
 
Example 16
Source File: DoubleMath.java    From codebuff with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * Compares {@code a} and {@code b} "fuzzily," with a tolerance for nearly-equal values.
 *
 * <p>This method is equivalent to
 * {@code fuzzyEquals(a, b, tolerance) ? 0 : Double.compare(a, b)}. In particular, like
 * {@link Double#compare(double, double)}, it treats all NaN values as equal and greater than all
 * other values (including {@link Double#POSITIVE_INFINITY}).
 *
 * <p>This is <em>not</em> a total ordering and is <em>not</em> suitable for use in
 * {@link Comparable#compareTo} implementations. In particular, it is not transitive.
 *
 * @throws IllegalArgumentException if {@code tolerance} is {@code < 0} or NaN
 * @since 13.0
 */


public static int fuzzyCompare(double a, double b, double tolerance) {
  if (fuzzyEquals(a, b, tolerance)) {
    return 0;
  } else if (a < b) {
    return -1;
  } else if (a > b) {
    return 1;
  } else {
    return Booleans.compare(Double.isNaN(a), Double.isNaN(b));
  }
}
 
Example 17
Source File: DoubleMath.java    From codebuff with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * Compares {@code a} and {@code b} "fuzzily," with a tolerance for nearly-equal values.
 *
 * <p>This method is equivalent to
 * {@code fuzzyEquals(a, b, tolerance) ? 0 : Double.compare(a, b)}. In particular, like
 * {@link Double#compare(double, double)}, it treats all NaN values as equal and greater than all
 * other values (including {@link Double#POSITIVE_INFINITY}).
 *
 * <p>This is <em>not</em> a total ordering and is <em>not</em> suitable for use in
 * {@link Comparable#compareTo} implementations. In particular, it is not transitive.
 *
 * @throws IllegalArgumentException if {@code tolerance} is {@code < 0} or NaN
 * @since 13.0
 */


public static int fuzzyCompare(double a, double b, double tolerance) {
  if (fuzzyEquals(a, b, tolerance)) {
    return 0;
  } else if (a < b) {
    return -1;
  } else if (a > b) {
    return 1;
  } else {
    return Booleans.compare(Double.isNaN(a), Double.isNaN(b));
  }
}
 
Example 18
Source File: BooleanExtensions.java    From xtext-lib with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * The binary <code>greaterThan</code> operator for boolean values.
 * {@code false} is considered less than {@code true}.
 * 
 * @see Boolean#compareTo(Boolean)
 * @see Booleans#compare(boolean, boolean)
 * @param a  a boolean.
 * @param b  another boolean.
 * @return   <code>Booleans.compare(a, b)&gt;0</code>
 * @since 2.4
 */
@Pure
@Inline(value = "($3.compare($1, $2) > 0)", imported = Booleans.class)
public static boolean operator_greaterThan(boolean a, boolean b) {
	return Booleans.compare(a, b) > 0;
}
 
Example 19
Source File: BooleanExtensions.java    From xtext-lib with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * The binary <code>lessEqualsThan</code> operator for boolean values.
 * {@code false} is considered less than {@code true}.
 * 
 * @see Boolean#compareTo(Boolean)
 * @see Booleans#compare(boolean, boolean)
 * @param a  a boolean.
 * @param b  another boolean.
 * @return   <code>Booleans.compare(a, b)&lt;=0</code>
 * @since 2.4
 */
@Pure
@Inline(value = "($3.compare($1, $2) <= 0)", imported = Booleans.class)
public static boolean operator_lessEqualsThan(boolean a, boolean b) {
	return Booleans.compare(a, b) <= 0;
}
 
Example 20
Source File: BooleanExtensions.java    From xtext-lib with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * The binary <code>lessThan</code> operator for boolean values.
 * {@code false} is considered less than {@code true}.
 * 
 * @see Boolean#compareTo(Boolean)
 * @see Booleans#compare(boolean, boolean)
 * @param a  a boolean.
 * @param b  another boolean.
 * @return   <code>Booleans.compare(a, b)&lt;0</code>
 * @since 2.4
 */
@Pure
@Inline(value = "($3.compare($1, $2) < 0)", imported = Booleans.class)
public static boolean operator_lessThan(boolean a, boolean b) {
	return Booleans.compare(a, b) < 0;
}