Skip to content

Commit cc3cc5a

Browse files
committed
Slightly optimize adding and removing ingredients are runtime
1 parent 59739c2 commit cc3cc5a

File tree

1 file changed

+35
-29
lines changed

1 file changed

+35
-29
lines changed

Gui/src/main/java/mezz/jei/gui/ingredients/IngredientFilter.java

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ public void rebuildItemFilter() {
130130
this.elementSearch.addAll(elementInfos);
131131
}
132132

133-
public <V> Optional<IListElement<V>> searchForMatchingElement(
133+
@Nullable
134+
public <V> IListElement<V> searchForMatchingElement(
134135
IIngredientHelper<V> ingredientHelper,
135136
ITypedIngredient<V> typedIngredient
136137
) {
@@ -141,11 +142,14 @@ public <V> Optional<IListElement<V>> searchForMatchingElement(
141142
String lowercaseDisplayName = DisplayNameUtil.getLowercaseDisplayNameForSearch(ingredient, ingredientHelper);
142143

143144
ElementPrefixParser.TokenInfo tokenInfo = new ElementPrefixParser.TokenInfo(lowercaseDisplayName, ElementPrefixParser.NO_PREFIX);
144-
return this.elementSearch.getSearchResults(tokenInfo)
145-
.stream()
146-
.map(elementInfo -> checkForMatch(elementInfo, type, ingredientUid, uidFunction))
147-
.flatMap(Optional::stream)
148-
.findFirst();
145+
Set<IListElement<?>> searchResults = this.elementSearch.getSearchResults(tokenInfo);
146+
for (IListElement<?> element : searchResults) {
147+
IListElement<V> match = checkForMatch(element, type, ingredientUid, uidFunction);
148+
if (match != null) {
149+
return match;
150+
}
151+
}
152+
return null;
149153
}
150154

151155
@Override
@@ -178,13 +182,11 @@ public <V> boolean updateHiddenState(IListElement<V> element) {
178182
public <V> void onIngredientVisibilityChanged(ITypedIngredient<V> ingredient, boolean visible) {
179183
IIngredientType<V> ingredientType = ingredient.getType();
180184
IIngredientHelper<V> ingredientHelper = ingredientManager.getIngredientHelper(ingredientType);
181-
searchForMatchingElement(ingredientHelper, ingredient)
182-
.ifPresent(element -> {
183-
if (element.isVisible() != visible) {
184-
element.setVisible(visible);
185-
notifyListenersOfChange();
186-
}
187-
});
185+
IListElement<V> match = searchForMatchingElement(ingredientHelper, ingredient);
186+
if (match != null && match.isVisible() != visible) {
187+
match.setVisible(visible);
188+
notifyListenersOfChange();
189+
}
188190
}
189191

190192
@Override
@@ -232,31 +234,36 @@ private Stream<ITypedIngredient<?>> getIngredientListUncached(String filterText)
232234
.map(IListElement::getTypedIngredient);
233235
}
234236

235-
private static <T> Optional<IListElement<T>> checkForMatch(IListElement<?> element, IIngredientType<T> ingredientType, Object uid, Function<ITypedIngredient<T>, Object> uidFunction) {
236-
return optionalCast(element, ingredientType)
237-
.filter(cast -> {
238-
ITypedIngredient<T> typedIngredient = cast.getTypedIngredient();
239-
Object elementUid = uidFunction.apply(typedIngredient);
240-
return uid.equals(elementUid);
241-
});
237+
@Nullable
238+
private static <T> IListElement<T> checkForMatch(IListElement<?> element, IIngredientType<T> ingredientType, Object uid, Function<ITypedIngredient<T>, Object> uidFunction) {
239+
IListElement<T> cast = optionalCast(element, ingredientType);
240+
if (cast == null) {
241+
return null;
242+
}
243+
ITypedIngredient<T> typedIngredient = cast.getTypedIngredient();
244+
Object elementUid = uidFunction.apply(typedIngredient);
245+
if (uid.equals(elementUid)) {
246+
return cast;
247+
}
248+
return null;
242249
}
243250

244-
private static <T> Optional<IListElement<T>> optionalCast(IListElement<?> element, IIngredientType<T> ingredientType) {
251+
@Nullable
252+
private static <T> IListElement<T> optionalCast(IListElement<?> element, IIngredientType<T> ingredientType) {
245253
ITypedIngredient<?> typedIngredient = element.getTypedIngredient();
246254
if (typedIngredient.getType() == ingredientType) {
247255
@SuppressWarnings("unchecked")
248256
IListElement<T> cast = (IListElement<T>) element;
249-
return Optional.of(cast);
257+
return cast;
250258
}
251-
return Optional.empty();
259+
return null;
252260
}
253261

254262
@Override
255263
public <V> void onIngredientsAdded(IIngredientHelper<V> ingredientHelper, Collection<ITypedIngredient<V>> ingredients) {
256264
for (ITypedIngredient<V> value : ingredients) {
257-
Optional<IListElement<V>> matchingElementOptional = searchForMatchingElement(ingredientHelper, value);
258-
if (matchingElementOptional.isPresent()) {
259-
IListElement<V> matchingElement = matchingElementOptional.get();
265+
IListElement<V> matchingElement = searchForMatchingElement(ingredientHelper, value);
266+
if (matchingElement != null) {
260267
updateHiddenState(matchingElement);
261268
if (DebugConfig.isDebugModeEnabled()) {
262269
LOGGER.debug("Updated ingredient: {}", ingredientHelper.getErrorInfo(value.getIngredient()));
@@ -277,15 +284,14 @@ public <V> void onIngredientsAdded(IIngredientHelper<V> ingredientHelper, Collec
277284
@Override
278285
public <V> void onIngredientsRemoved(IIngredientHelper<V> ingredientHelper, Collection<ITypedIngredient<V>> ingredients) {
279286
for (ITypedIngredient<V> typedIngredient : ingredients) {
280-
Optional<IListElement<V>> matchingElementOptional = searchForMatchingElement(ingredientHelper, typedIngredient);
281-
if (matchingElementOptional.isEmpty()) {
287+
IListElement<V> matchingElement = searchForMatchingElement(ingredientHelper, typedIngredient);
288+
if (matchingElement == null) {
282289
String errorInfo = ingredientHelper.getErrorInfo(typedIngredient.getIngredient());
283290
LOGGER.error("Could not find a matching ingredient to remove: {}", errorInfo);
284291
} else {
285292
if (DebugConfig.isDebugModeEnabled()) {
286293
LOGGER.debug("Removed ingredient: {}", ingredientHelper.getErrorInfo(typedIngredient.getIngredient()));
287294
}
288-
IListElement<V> matchingElement = matchingElementOptional.get();
289295
matchingElement.setVisible(false);
290296
}
291297
}

0 commit comments

Comments
 (0)