From d203640d5296bde151596388aa6df332393d0a1c Mon Sep 17 00:00:00 2001 From: Roman / Linnea Gräf Date: Fri, 9 Sep 2022 15:52:20 +0200 Subject: Fix item resolution query crashing on some enchant books (#262) --- .../notenoughupdates/util/IteratorUtils.java | 36 ++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/main/java/io/github/moulberry/notenoughupdates/util/IteratorUtils.java (limited to 'src/main/java/io/github/moulberry/notenoughupdates/util/IteratorUtils.java') diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/IteratorUtils.java b/src/main/java/io/github/moulberry/notenoughupdates/util/IteratorUtils.java new file mode 100644 index 00000000..32cd5fad --- /dev/null +++ b/src/main/java/io/github/moulberry/notenoughupdates/util/IteratorUtils.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2022 NotEnoughUpdates contributors + * + * This file is part of NotEnoughUpdates. + * + * NotEnoughUpdates is free software: you can redistribute it + * and/or modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation, either + * version 3 of the License, or (at your option) any later version. + * + * NotEnoughUpdates is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with NotEnoughUpdates. If not, see . + */ + +package io.github.moulberry.notenoughupdates.util; + +import java.util.Iterator; + +public class IteratorUtils { + + public static T getOnlyElement(Iterator it, T defaultValue) { + if (!it.hasNext()) return defaultValue; + T ret = it.next(); + if (it.hasNext()) return defaultValue; + return ret; + } + + public static T getOnlyElement(Iterable it, T defaultValue) { + return getOnlyElement(it.iterator(), defaultValue); + } +} -- cgit