|
| 1 | +/* |
| 2 | + * Copyright 2021-2022 Cufy and ProgSpaceSA |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.cufy.http.wrapper; |
| 17 | + |
| 18 | +import org.cufy.http.Endpoint; |
| 19 | +import org.jetbrains.annotations.Contract; |
| 20 | +import org.jetbrains.annotations.NotNull; |
| 21 | + |
| 22 | +import java.util.Objects; |
| 23 | +import java.util.function.Consumer; |
| 24 | +import java.util.function.Function; |
| 25 | + |
| 26 | +/** |
| 27 | + * An extended version of {@link EndpointWrapper}. |
| 28 | + * |
| 29 | + * @param <E> the type of the endpoint. |
| 30 | + * @param <Self> the type of this wrapper. |
| 31 | + * @author LSafer |
| 32 | + * @version 1.0.0 |
| 33 | + * @since 1.0.0 ~2022.01.06 |
| 34 | + */ |
| 35 | +public interface EndpointContext<E extends Endpoint, Self extends EndpointContext<E, Self>> extends EndpointWrapper<E, Self> { |
| 36 | + /** |
| 37 | + * Invoke the given {@code getter} with this as the parameter and return the result of |
| 38 | + * the invocation. |
| 39 | + * |
| 40 | + * @param getter the getter to be invoked. |
| 41 | + * @param <T> the type of the result. |
| 42 | + * @return the result of invoking the given {@code getter}. |
| 43 | + * @throws NullPointerException if the given {@code getter} is null. |
| 44 | + * @since 1.0.0 ~2022.01.06 |
| 45 | + */ |
| 46 | + @Contract(pure = true) |
| 47 | + default <T> T get(@NotNull Function<Self, T> getter) { |
| 48 | + Objects.requireNonNull(getter, "getter"); |
| 49 | + return getter.apply((Self) this); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Invoke the given {@code setter} with this as the parameter. |
| 54 | + * |
| 55 | + * @param setter the setter to be invoked. |
| 56 | + * @return this. |
| 57 | + * @throws NullPointerException if the given {@code setter} is null. |
| 58 | + * @since 1.0.0 ~2022.01.06 |
| 59 | + */ |
| 60 | + @NotNull |
| 61 | + @Contract(value = "_->this", mutates = "this") |
| 62 | + default Self set(@NotNull Consumer<Self> setter) { |
| 63 | + Objects.requireNonNull(setter, "setter"); |
| 64 | + setter.accept((Self) this); |
| 65 | + return (Self) this; |
| 66 | + } |
| 67 | +} |
0 commit comments