@@ -1200,6 +1200,42 @@ static ClusterState innerPutTemplate(
12001200 return ClusterState .builder (currentState ).metadata (builder ).build ();
12011201 }
12021202
1203+ /**
1204+ * A private, local alternative to elements.stream().anyMatch(predicate) for micro-optimization reasons.
1205+ */
1206+ private static <T > boolean anyMatch (final List <T > elements , final Predicate <T > predicate ) {
1207+ for (T e : elements ) {
1208+ if (predicate .test (e )) {
1209+ return true ;
1210+ }
1211+ }
1212+ return false ;
1213+ }
1214+
1215+ /**
1216+ * A private, local alternative to elements.stream().noneMatch(predicate) for micro-optimization reasons.
1217+ */
1218+ private static <T > boolean noneMatch (final List <T > elements , final Predicate <T > predicate ) {
1219+ for (T e : elements ) {
1220+ if (predicate .test (e )) {
1221+ return false ;
1222+ }
1223+ }
1224+ return true ;
1225+ }
1226+
1227+ /**
1228+ * A private, local alternative to elements.stream().filter(predicate).findFirst() for micro-optimization reasons.
1229+ */
1230+ private static <T > Optional <T > findFirst (final List <T > elements , final Predicate <T > predicate ) {
1231+ for (T e : elements ) {
1232+ if (predicate .test (e )) {
1233+ return Optional .of (e );
1234+ }
1235+ }
1236+ return Optional .empty ();
1237+ }
1238+
12031239 /**
12041240 * Finds index templates whose index pattern matched with the given index name. In the case of
12051241 * hidden indices, a template with a match all pattern or global template will not be returned.
@@ -1219,15 +1255,14 @@ public static List<IndexTemplateMetadata> findV1Templates(Metadata metadata, Str
12191255 final List <IndexTemplateMetadata > matchedTemplates = new ArrayList <>();
12201256 for (IndexTemplateMetadata template : metadata .templates ().values ()) {
12211257 if (isHidden == null || isHidden == Boolean .FALSE ) {
1222- final boolean matched = template .patterns ().stream ().anyMatch (patternMatchPredicate );
1223- if (matched ) {
1258+ if (anyMatch (template .patterns (), patternMatchPredicate )) {
12241259 matchedTemplates .add (template );
12251260 }
12261261 } else {
12271262 assert isHidden == Boolean .TRUE ;
1228- final boolean isNotMatchAllTemplate = template .patterns (). stream (). noneMatch ( Regex ::isMatchAllPattern );
1263+ final boolean isNotMatchAllTemplate = noneMatch ( template .patterns (), Regex ::isMatchAllPattern );
12291264 if (isNotMatchAllTemplate ) {
1230- if (template .patterns (). stream (). anyMatch ( patternMatchPredicate )) {
1265+ if (anyMatch ( template .patterns (), patternMatchPredicate )) {
12311266 matchedTemplates .add (template );
12321267 }
12331268 }
@@ -1238,19 +1273,21 @@ public static List<IndexTemplateMetadata> findV1Templates(Metadata metadata, Str
12381273 // this is complex but if the index is not hidden in the create request but is hidden as the result of template application,
12391274 // then we need to exclude global templates
12401275 if (isHidden == null ) {
1241- final Optional <IndexTemplateMetadata > templateWithHiddenSetting = matchedTemplates .stream ()
1242- .filter (template -> IndexMetadata .INDEX_HIDDEN_SETTING .exists (template .settings ()))
1243- .findFirst ();
1276+ final Optional <IndexTemplateMetadata > templateWithHiddenSetting = findFirst (
1277+ matchedTemplates ,
1278+ template -> IndexMetadata .INDEX_HIDDEN_SETTING .exists (template .settings ())
1279+ );
12441280 if (templateWithHiddenSetting .isPresent ()) {
12451281 final boolean templatedIsHidden = IndexMetadata .INDEX_HIDDEN_SETTING .get (templateWithHiddenSetting .get ().settings ());
12461282 if (templatedIsHidden ) {
12471283 // remove the global templates
1248- matchedTemplates .removeIf (current -> current .patterns (). stream (). anyMatch ( Regex ::isMatchAllPattern ));
1284+ matchedTemplates .removeIf (current -> anyMatch ( current .patterns (), Regex ::isMatchAllPattern ));
12491285 }
12501286 // validate that hidden didn't change
1251- final Optional <IndexTemplateMetadata > templateWithHiddenSettingPostRemoval = matchedTemplates .stream ()
1252- .filter (template -> IndexMetadata .INDEX_HIDDEN_SETTING .exists (template .settings ()))
1253- .findFirst ();
1287+ final Optional <IndexTemplateMetadata > templateWithHiddenSettingPostRemoval = findFirst (
1288+ matchedTemplates ,
1289+ template -> IndexMetadata .INDEX_HIDDEN_SETTING .exists (template .settings ())
1290+ );
12541291 if (templateWithHiddenSettingPostRemoval .isEmpty ()
12551292 || templateWithHiddenSetting .get () != templateWithHiddenSettingPostRemoval .get ()) {
12561293 throw new IllegalStateException (
@@ -1313,14 +1350,13 @@ static List<Tuple<String, ComposableIndexTemplate>> findV2CandidateTemplates(Met
13131350 * built with a template that none of its indices match.
13141351 */
13151352 if (isHidden == false || template .getDataStreamTemplate () != null ) {
1316- final boolean matched = template .indexPatterns ().stream ().anyMatch (patternMatchPredicate );
1317- if (matched ) {
1353+ if (anyMatch (template .indexPatterns (), patternMatchPredicate )) {
13181354 candidates .add (Tuple .tuple (name , template ));
13191355 }
13201356 } else {
1321- final boolean isNotMatchAllTemplate = template .indexPatterns (). stream (). noneMatch ( Regex ::isMatchAllPattern );
1357+ final boolean isNotMatchAllTemplate = noneMatch ( template .indexPatterns (), Regex ::isMatchAllPattern );
13221358 if (isNotMatchAllTemplate ) {
1323- if (template .indexPatterns (). stream (). anyMatch ( patternMatchPredicate )) {
1359+ if (anyMatch ( template .indexPatterns (), patternMatchPredicate )) {
13241360 candidates .add (Tuple .tuple (name , template ));
13251361 }
13261362 }
@@ -1334,7 +1370,7 @@ static List<Tuple<String, ComposableIndexTemplate>> findV2CandidateTemplates(Met
13341370 // Checks if a global template specifies the `index.hidden` setting. This check is important because a global
13351371 // template shouldn't specify the `index.hidden` setting, we leave it up to the caller to handle this situation.
13361372 private static boolean isGlobalAndHasIndexHiddenSetting (Metadata metadata , ComposableIndexTemplate template , String templateName ) {
1337- return template .indexPatterns (). stream (). anyMatch ( Regex ::isMatchAllPattern )
1373+ return anyMatch ( template .indexPatterns (), Regex ::isMatchAllPattern )
13381374 && IndexMetadata .INDEX_HIDDEN_SETTING .exists (resolveSettings (metadata , templateName ));
13391375 }
13401376
0 commit comments