|
| 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.parser; |
| 25 | + |
| 26 | +import java.util.concurrent.CompletableFuture; |
| 27 | +import org.apiguardian.api.API; |
| 28 | +import org.checkerframework.checker.nullness.qual.NonNull; |
| 29 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 30 | +import org.incendo.cloud.context.CommandContext; |
| 31 | +import org.incendo.cloud.context.CommandInput; |
| 32 | +import org.incendo.cloud.suggestion.SuggestionProvider; |
| 33 | + |
| 34 | +import static java.util.Objects.requireNonNull; |
| 35 | + |
| 36 | +@API(status = API.Status.INTERNAL) |
| 37 | +public final class MappedArgumentParserImpl<C, I, O> implements MappedArgumentParser<C, I, O>, |
| 38 | + ArgumentParser.FutureArgumentParser<C, O> { |
| 39 | + |
| 40 | + private final ArgumentParser<C, I> base; |
| 41 | + private final Mapper<C, I, O> mapper; |
| 42 | + |
| 43 | + MappedArgumentParserImpl( |
| 44 | + final ArgumentParser<C, I> base, |
| 45 | + final Mapper<C, I, O> mapper |
| 46 | + ) { |
| 47 | + this.base = base; |
| 48 | + this.mapper = mapper; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public @NonNull ArgumentParser<C, I> baseParser() { |
| 53 | + return this.base; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public @NonNull CompletableFuture<@NonNull ArgumentParseResult<O>> parseFuture( |
| 58 | + final @NonNull CommandContext<@NonNull C> commandContext, |
| 59 | + final @NonNull CommandInput commandInput |
| 60 | + ) { |
| 61 | + return this.base.parseFuture(commandContext, commandInput) |
| 62 | + .thenCompose(result -> this.mapper.map(commandContext, result)); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public @NonNull SuggestionProvider<C> suggestionProvider() { |
| 67 | + return this.base.suggestionProvider(); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public <O1> ArgumentParser.@NonNull FutureArgumentParser<C, O1> flatMap(final Mapper<C, O, O1> mapper) { |
| 72 | + requireNonNull(mapper, "mapper"); |
| 73 | + return new MappedArgumentParserImpl<>( |
| 74 | + this.base, |
| 75 | + (ctx, orig) -> this.mapper.map(ctx, orig) |
| 76 | + .thenCompose(mapped -> mapper.map(ctx, mapped)) |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public int hashCode() { |
| 82 | + return 31 + this.base.hashCode() |
| 83 | + + 7 * this.mapper.hashCode(); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public boolean equals(final @Nullable Object other) { |
| 88 | + if (!(other instanceof MappedArgumentParserImpl<?, ?, ?>)) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + final MappedArgumentParserImpl<?, ?, ?> that = (MappedArgumentParserImpl<?, ?, ?>) other; |
| 93 | + return this.base.equals(that.base) |
| 94 | + && this.mapper.equals(that.mapper); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public String toString() { |
| 99 | + return "MappedArgumentParserImpl{" |
| 100 | + + "base=" + this.base + ',' |
| 101 | + + "mapper=" + this.mapper + '}'; |
| 102 | + } |
| 103 | +} |
0 commit comments