Skip to content

Commit c3c3f23

Browse files
committed
feat(core): Reuse a single instance for SuggestionProvider#noSuggestions
1 parent b9c37a4 commit c3c3f23

2 files changed

Lines changed: 56 additions & 3 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//
2+
// MIT License
3+
//
4+
// Copyright (c) 2024 Incendo
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in all
14+
// copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// SOFTWARE.
23+
//
24+
package org.incendo.cloud.suggestion;
25+
26+
import java.util.Collections;
27+
import java.util.concurrent.CompletableFuture;
28+
import org.checkerframework.checker.nullness.qual.NonNull;
29+
import org.incendo.cloud.context.CommandContext;
30+
import org.incendo.cloud.context.CommandInput;
31+
32+
final class NoSuggestions implements SuggestionProvider<Object> {
33+
34+
private static final SuggestionProvider<?> INSTANCE = new NoSuggestions();
35+
36+
private final CompletableFuture<? extends @NonNull Iterable<? extends @NonNull Suggestion>> result =
37+
CompletableFuture.completedFuture(Collections.emptyList());
38+
39+
private NoSuggestions() {
40+
}
41+
42+
@Override
43+
public @NonNull CompletableFuture<? extends @NonNull Iterable<? extends @NonNull Suggestion>> suggestionsFuture(
44+
final @NonNull CommandContext<Object> context,
45+
final @NonNull CommandInput input
46+
) {
47+
return this.result;
48+
}
49+
50+
@SuppressWarnings("unchecked")
51+
static <C> @NonNull SuggestionProvider<C> instance() {
52+
return (SuggestionProvider<C>) INSTANCE;
53+
}
54+
}

cloud-core/src/main/java/org/incendo/cloud/suggestion/SuggestionProvider.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
package org.incendo.cloud.suggestion;
2525

2626
import java.util.Arrays;
27-
import java.util.Collections;
2827
import java.util.concurrent.CompletableFuture;
2928
import org.apiguardian.api.API;
3029
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -66,8 +65,8 @@ public interface SuggestionProvider<C> {
6665
* @param <C> command sender type
6766
* @return suggestion provider
6867
*/
69-
static <C> SuggestionProvider<C> noSuggestions() {
70-
return (ctx, in) -> CompletableFuture.completedFuture(Collections.emptyList());
68+
static <C> @NonNull SuggestionProvider<C> noSuggestions() {
69+
return NoSuggestions.instance();
7170
}
7271

7372
/**

0 commit comments

Comments
 (0)