forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce Predicate Utilities for always true/false use-cases (elasti…
…c#105881) Just a suggetion. I think this would save us a bit of memory here and there. We have loads of places where the always true lambdas are used with `Predicate.or/and`. Found this initially when looking into field caps performance where we used to heavily compose these but many spots in security and index name resolution gain from these predicates. The better toString also helps in some cases at least when debugging.
- Loading branch information
1 parent
9c1a079
commit fc8e2b7
Showing
55 changed files
with
246 additions
and
93 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
libs/core/src/main/java/org/elasticsearch/core/Predicates.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.core; | ||
|
||
import java.util.function.Predicate; | ||
|
||
/** | ||
* Utilities around predicates. | ||
*/ | ||
public enum Predicates { | ||
; | ||
|
||
@SuppressWarnings("rawtypes") | ||
private static final Predicate NEVER = new Predicate() { | ||
@Override | ||
public boolean test(Object o) { | ||
return false; | ||
} | ||
|
||
@Override | ||
public Predicate and(Predicate other) { | ||
return this; | ||
} | ||
|
||
@Override | ||
public Predicate negate() { | ||
return ALWAYS; | ||
} | ||
|
||
@Override | ||
public Predicate or(Predicate other) { | ||
return other; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Predicate[NEVER]"; | ||
} | ||
}; | ||
|
||
@SuppressWarnings("rawtypes") | ||
private static final Predicate ALWAYS = new Predicate() { | ||
@Override | ||
public boolean test(Object o) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Predicate and(Predicate other) { | ||
return other; | ||
} | ||
|
||
@Override | ||
public Predicate negate() { | ||
return NEVER; | ||
} | ||
|
||
@Override | ||
public Predicate or(Predicate other) { | ||
return this; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Predicate[ALWAYS]"; | ||
} | ||
}; | ||
|
||
/** | ||
* @return a predicate that accepts all input values | ||
* @param <T> type of the predicate | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public static <T> Predicate<T> always() { | ||
return (Predicate<T>) ALWAYS; | ||
} | ||
|
||
/** | ||
* @return a predicate that rejects all input values | ||
* @param <T> type of the predicate | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public static <T> Predicate<T> never() { | ||
return (Predicate<T>) NEVER; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.