diff --git a/lucene/core/src/java/org/apache/lucene/index/BufferedDeletesStream.java b/lucene/core/src/java/org/apache/lucene/index/BufferedDeletesStream.java index 24aac298b91..5ef2b9ce79b 100644 --- a/lucene/core/src/java/org/apache/lucene/index/BufferedDeletesStream.java +++ b/lucene/core/src/java/org/apache/lucene/index/BufferedDeletesStream.java @@ -134,14 +134,7 @@ class BufferedDeletesStream { private static final Comparator sortSegInfoByDelGen = new Comparator() { @Override public int compare(SegmentInfoPerCommit si1, SegmentInfoPerCommit si2) { - final long cmp = si1.getBufferedDeletesGen() - si2.getBufferedDeletesGen(); - if (cmp > 0) { - return 1; - } else if (cmp < 0) { - return -1; - } else { - return 0; - } + return Long.compare(si1.getBufferedDeletesGen(), si2.getBufferedDeletesGen()); } }; diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexCommit.java b/lucene/core/src/java/org/apache/lucene/index/IndexCommit.java index 08f77286552..115f218cfd2 100644 --- a/lucene/core/src/java/org/apache/lucene/index/IndexCommit.java +++ b/lucene/core/src/java/org/apache/lucene/index/IndexCommit.java @@ -119,12 +119,6 @@ public abstract class IndexCommit implements Comparable { long gen = getGeneration(); long comgen = commit.getGeneration(); - if (gen < comgen) { - return -1; - } else if (gen > comgen) { - return 1; - } else { - return 0; - } + return Long.compare(gen, comgen); } } diff --git a/lucene/core/src/java/org/apache/lucene/index/LogMergePolicy.java b/lucene/core/src/java/org/apache/lucene/index/LogMergePolicy.java index a3c954aba94..b35f2aa8c6b 100644 --- a/lucene/core/src/java/org/apache/lucene/index/LogMergePolicy.java +++ b/lucene/core/src/java/org/apache/lucene/index/LogMergePolicy.java @@ -545,13 +545,7 @@ public abstract class LogMergePolicy extends MergePolicy { // Sorts largest to smallest @Override public int compareTo(SegmentInfoAndLevel other) { - if (level < other.level) { - return 1; - } else if (level > other.level) { - return -1; - } else { - return 0; - } + return Float.compare(other.level, level); } } diff --git a/lucene/core/src/java/org/apache/lucene/search/ConjunctionScorer.java b/lucene/core/src/java/org/apache/lucene/search/ConjunctionScorer.java index 588c097f59d..d285c481c27 100644 --- a/lucene/core/src/java/org/apache/lucene/search/ConjunctionScorer.java +++ b/lucene/core/src/java/org/apache/lucene/search/ConjunctionScorer.java @@ -47,7 +47,7 @@ class ConjunctionScorer extends Scorer { ArrayUtil.mergeSort(docsAndFreqs, new Comparator() { @Override public int compare(DocsAndFreqs o1, DocsAndFreqs o2) { - return Long.signum(o1.cost - o2.cost); + return Long.compare(o1.cost, o2.cost); } }); diff --git a/lucene/core/src/java/org/apache/lucene/search/FieldComparator.java b/lucene/core/src/java/org/apache/lucene/search/FieldComparator.java index 476b5738ab5..ee9668e674e 100644 --- a/lucene/core/src/java/org/apache/lucene/search/FieldComparator.java +++ b/lucene/core/src/java/org/apache/lucene/search/FieldComparator.java @@ -19,7 +19,6 @@ package org.apache.lucene.search; import java.io.IOException; -import org.apache.lucene.index.AtomicReader; // javadocs import org.apache.lucene.index.AtomicReaderContext; import org.apache.lucene.index.BinaryDocValues; import org.apache.lucene.index.SortedDocValues; @@ -235,7 +234,7 @@ public abstract class FieldComparator { @Override public int compare(int slot1, int slot2) { - return values[slot1] - values[slot2]; + return Byte.compare(values[slot1], values[slot2]); } @Override @@ -247,7 +246,7 @@ public abstract class FieldComparator { v2 = missingValue; } - return bottom - v2; + return Byte.compare(bottom, v2); } @Override @@ -287,7 +286,7 @@ public abstract class FieldComparator { if (docsWithField != null && docValue == 0 && !docsWithField.get(doc)) { docValue = missingValue; } - return docValue - value.byteValue(); + return Byte.compare(docValue, value.byteValue()); } } @@ -307,15 +306,7 @@ public abstract class FieldComparator { @Override public int compare(int slot1, int slot2) { - final double v1 = values[slot1]; - final double v2 = values[slot2]; - if (v1 > v2) { - return 1; - } else if (v1 < v2) { - return -1; - } else { - return 0; - } + return Double.compare(values[slot1], values[slot2]); } @Override @@ -327,13 +318,7 @@ public abstract class FieldComparator { v2 = missingValue; } - if (bottom > v2) { - return 1; - } else if (bottom < v2) { - return -1; - } else { - return 0; - } + return Double.compare(bottom, v2); } @Override @@ -375,13 +360,7 @@ public abstract class FieldComparator { if (docsWithField != null && docValue == 0 && !docsWithField.get(doc)) { docValue = missingValue; } - if (docValue < value) { - return -1; - } else if (docValue > value) { - return 1; - } else { - return 0; - } + return Double.compare(docValue, value); } } @@ -401,17 +380,7 @@ public abstract class FieldComparator { @Override public int compare(int slot1, int slot2) { - // TODO: are there sneaky non-branch ways to compute - // sign of float? - final float v1 = values[slot1]; - final float v2 = values[slot2]; - if (v1 > v2) { - return 1; - } else if (v1 < v2) { - return -1; - } else { - return 0; - } + return Float.compare(values[slot1], values[slot2]); } @Override @@ -423,14 +392,8 @@ public abstract class FieldComparator { if (docsWithField != null && v2 == 0 && !docsWithField.get(doc)) { v2 = missingValue; } - - if (bottom > v2) { - return 1; - } else if (bottom < v2) { - return -1; - } else { - return 0; - } + + return Float.compare(bottom, v2); } @Override @@ -472,13 +435,7 @@ public abstract class FieldComparator { if (docsWithField != null && docValue == 0 && !docsWithField.get(doc)) { docValue = missingValue; } - if (docValue < value) { - return -1; - } else if (docValue > value) { - return 1; - } else { - return 0; - } + return Float.compare(docValue, value); } } @@ -498,7 +455,7 @@ public abstract class FieldComparator { @Override public int compare(int slot1, int slot2) { - return values[slot1] - values[slot2]; + return Short.compare(values[slot1], values[slot2]); } @Override @@ -510,7 +467,7 @@ public abstract class FieldComparator { v2 = missingValue; } - return bottom - v2; + return Short.compare(bottom, v2); } @Override @@ -552,7 +509,7 @@ public abstract class FieldComparator { if (docsWithField != null && docValue == 0 && !docsWithField.get(doc)) { docValue = missingValue; } - return docValue - value; + return Short.compare(docValue, value); } } @@ -572,27 +529,11 @@ public abstract class FieldComparator { @Override public int compare(int slot1, int slot2) { - // TODO: there are sneaky non-branch ways to compute - // -1/+1/0 sign - // Cannot return values[slot1] - values[slot2] because that - // may overflow - final int v1 = values[slot1]; - final int v2 = values[slot2]; - if (v1 > v2) { - return 1; - } else if (v1 < v2) { - return -1; - } else { - return 0; - } + return Integer.compare(values[slot1], values[slot2]); } @Override public int compareBottom(int doc) { - // TODO: there are sneaky non-branch ways to compute - // -1/+1/0 sign - // Cannot return bottom - values[slot2] because that - // may overflow int v2 = currentReaderValues.get(doc); // Test for v2 == 0 to save Bits.get method call for // the common case (doc has value and value is non-zero): @@ -600,13 +541,7 @@ public abstract class FieldComparator { v2 = missingValue; } - if (bottom > v2) { - return 1; - } else if (bottom < v2) { - return -1; - } else { - return 0; - } + return Integer.compare(bottom, v2); } @Override @@ -648,13 +583,7 @@ public abstract class FieldComparator { if (docsWithField != null && docValue == 0 && !docsWithField.get(doc)) { docValue = missingValue; } - if (docValue < value) { - return -1; - } else if (docValue > value) { - return 1; - } else { - return 0; - } + return Integer.compare(docValue, value); } } @@ -674,17 +603,7 @@ public abstract class FieldComparator { @Override public int compare(int slot1, int slot2) { - // TODO: there are sneaky non-branch ways to compute - // -1/+1/0 sign - final long v1 = values[slot1]; - final long v2 = values[slot2]; - if (v1 > v2) { - return 1; - } else if (v1 < v2) { - return -1; - } else { - return 0; - } + return Long.compare(values[slot1], values[slot2]); } @Override @@ -698,13 +617,7 @@ public abstract class FieldComparator { v2 = missingValue; } - if (bottom > v2) { - return 1; - } else if (bottom < v2) { - return -1; - } else { - return 0; - } + return Long.compare(bottom, v2); } @Override @@ -746,13 +659,7 @@ public abstract class FieldComparator { if (docsWithField != null && docValue == 0 && !docsWithField.get(doc)) { docValue = missingValue; } - if (docValue < value) { - return -1; - } else if (docValue > value) { - return 1; - } else { - return 0; - } + return Long.compare(docValue, value); } } @@ -773,16 +680,14 @@ public abstract class FieldComparator { @Override public int compare(int slot1, int slot2) { - final float score1 = scores[slot1]; - final float score2 = scores[slot2]; - return score1 > score2 ? -1 : (score1 < score2 ? 1 : 0); + return Float.compare(scores[slot2], scores[slot1]); } @Override public int compareBottom(int doc) throws IOException { float score = scorer.score(); assert !Float.isNaN(score); - return bottom > score ? -1 : (bottom < score ? 1 : 0); + return Float.compare(score, bottom); } @Override @@ -831,15 +736,7 @@ public abstract class FieldComparator { final float value = valueObj.floatValue(); float docValue = scorer.score(); assert !Float.isNaN(docValue); - if (docValue < value) { - // reverse of FloatComparator - return 1; - } else if (docValue > value) { - // reverse of FloatComparator - return -1; - } else { - return 0; - } + return Float.compare(value, docValue); } } @@ -893,13 +790,7 @@ public abstract class FieldComparator { public int compareDocToValue(int doc, Integer valueObj) { final int value = valueObj.intValue(); int docValue = docBase + doc; - if (docValue < value) { - return -1; - } else if (docValue > value) { - return 1; - } else { - return 0; - } + return Integer.compare(docValue, value); } } diff --git a/lucene/core/src/java/org/apache/lucene/search/SearcherLifetimeManager.java b/lucene/core/src/java/org/apache/lucene/search/SearcherLifetimeManager.java index deb0ce70a7c..a06ca965753 100644 --- a/lucene/core/src/java/org/apache/lucene/search/SearcherLifetimeManager.java +++ b/lucene/core/src/java/org/apache/lucene/search/SearcherLifetimeManager.java @@ -119,16 +119,7 @@ public class SearcherLifetimeManager implements Closeable { // Newer searchers are sort before older ones: @Override public int compareTo(SearcherTracker other) { - // Be defensive: cannot subtract since it could - // technically overflow long, though, we'd never hit - // that in practice: - if (recordTimeSec < other.recordTimeSec) { - return 1; - } else if (other.recordTimeSec < recordTimeSec) { - return -1; - } else { - return 0; - } + return Double.compare(other.recordTimeSec, recordTimeSec); } @Override diff --git a/lucene/core/src/test/org/apache/lucene/util/TestSorterTemplate.java b/lucene/core/src/test/org/apache/lucene/util/TestSorterTemplate.java index 00e63fb057c..ffc546fbab2 100644 --- a/lucene/core/src/test/org/apache/lucene/util/TestSorterTemplate.java +++ b/lucene/core/src/test/org/apache/lucene/util/TestSorterTemplate.java @@ -44,7 +44,7 @@ public class TestSorterTemplate extends LuceneTestCase { // only compare the last 32 bits final long a = i & 0xFFFFFFFFL; final long b = j & 0xFFFFFFFFL; - return a < b ? -1 : a == b ? 0 : 1; + return Long.compare(a, b); } @Override diff --git a/lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/Passage.java b/lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/Passage.java index 9df80484712..db66ffc4e1e 100644 --- a/lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/Passage.java +++ b/lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/Passage.java @@ -76,8 +76,7 @@ public final class Passage { @Override protected int compare(int i, int j) { - // TODO: java7 use Integer.compare(starts[i], starts[j]) - return Long.signum(((long)starts[i]) - starts[j]); + return Integer.compare(starts[i], starts[j]); } @Override @@ -87,8 +86,7 @@ public final class Passage { @Override protected int comparePivot(int j) { - // TODO: java7 use Integer.compare(pivot, starts[j]) - return Long.signum(((long)pivot) - starts[j]); + return Integer.compare(pivot, starts[j]); } int pivot; diff --git a/lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/PostingsHighlighter.java b/lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/PostingsHighlighter.java index a0907589259..3c0432b7f4c 100644 --- a/lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/PostingsHighlighter.java +++ b/lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/PostingsHighlighter.java @@ -464,7 +464,7 @@ public final class PostingsHighlighter { if (off == otherOff) { return id - other.id; } else { - return Long.signum(((long)off) - otherOff); + return Integer.compare(off, otherOff); } } catch (IOException e) { throw new RuntimeException(e); diff --git a/lucene/misc/src/java/org/apache/lucene/misc/HighFreqTerms.java b/lucene/misc/src/java/org/apache/lucene/misc/HighFreqTerms.java index 0d5b21184f7..ab53aaf9f47 100644 --- a/lucene/misc/src/java/org/apache/lucene/misc/HighFreqTerms.java +++ b/lucene/misc/src/java/org/apache/lucene/misc/HighFreqTerms.java @@ -211,13 +211,7 @@ final class TotalTermFreqComparatorSortDescending implements Comparator b.totalTermFreq) { - return -1; - } else { - return 0; - } + return Long.compare(b.totalTermFreq, a.totalTermFreq); } } diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/ValueSource.java b/lucene/queries/src/java/org/apache/lucene/queries/function/ValueSource.java index 2e40b842fe7..d1dd994454a 100644 --- a/lucene/queries/src/java/org/apache/lucene/queries/function/ValueSource.java +++ b/lucene/queries/src/java/org/apache/lucene/queries/function/ValueSource.java @@ -139,28 +139,12 @@ public abstract class ValueSource { @Override public int compare(int slot1, int slot2) { - final double v1 = values[slot1]; - final double v2 = values[slot2]; - if (v1 > v2) { - return 1; - } else if (v1 < v2) { - return -1; - } else { - return 0; - } - + return Double.compare(values[slot1], values[slot2]); } @Override public int compareBottom(int doc) { - final double v2 = docVals.doubleVal(doc); - if (bottom > v2) { - return 1; - } else if (bottom < v2) { - return -1; - } else { - return 0; - } + return Double.compare(bottom, docVals.doubleVal(doc)); } @Override @@ -188,13 +172,7 @@ public abstract class ValueSource { public int compareDocToValue(int doc, Double valueObj) { final double value = valueObj; final double docValue = docVals.doubleVal(doc); - if (docValue < value) { - return -1; - } else if (docValue > value) { - return 1; - } else { - return 0; - } + return Double.compare(docValue, value); } } } diff --git a/lucene/suggest/src/java/org/apache/lucene/search/suggest/SortedTermFreqIteratorWrapper.java b/lucene/suggest/src/java/org/apache/lucene/search/suggest/SortedTermFreqIteratorWrapper.java index f48305e6fc6..409f4ff34c8 100644 --- a/lucene/suggest/src/java/org/apache/lucene/search/suggest/SortedTermFreqIteratorWrapper.java +++ b/lucene/suggest/src/java/org/apache/lucene/search/suggest/SortedTermFreqIteratorWrapper.java @@ -120,13 +120,7 @@ public class SortedTermFreqIteratorWrapper implements TermFreqIterator { if (cmp != 0) { return cmp; } - if (leftCost < rightCost) { - return -1; - } else if (rightCost < leftCost) { - return 1; - } else { - return 0; - } + return Long.compare(leftCost, rightCost); } };