Skip to content
This repository was archived by the owner on Mar 8, 2024. It is now read-only.

Commit 5674fb8

Browse files
authored
Merge pull request #10 from LSafer-Agile/master
Updated the 'wrapper.set(...)' and 'wrapper.get(...)' utility functions
2 parents d11db8d + 92d6179 commit 5674fb8

4 files changed

Lines changed: 69 additions & 18 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
}

src/main/java/org/cufy/http/wrapper/RequestContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* @since 0.3.0 ~2021.12.12
3131
*/
3232
public interface RequestContext<E extends Endpoint, R extends ResponseContext<E, Self, R>, Self extends RequestContext<E, R, Self>>
33-
extends RequestExtension<Self>, EndpointWrapper<E, Self>, ExtrasExtension<Self> {
33+
extends RequestExtension<Self>, EndpointContext<E, Self>, ExtrasExtension<Self> {
3434
/**
3535
* Return the response wrapper instance of this.
3636
* <br>

src/main/java/org/cufy/http/wrapper/ResponseContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* @since 0.3.0 ~2021.12.12
3131
*/
3232
public interface ResponseContext<E extends Endpoint, R extends RequestContext<E, Self, R>, Self extends ResponseContext<E, R, Self>>
33-
extends ResponseExtension<Self>, EndpointWrapper<E, Self>, ExtrasExtension<Self> {
33+
extends ResponseExtension<Self>, EndpointContext<E, Self>, ExtrasExtension<Self> {
3434
/**
3535
* Return the request wrapper instance of this.
3636
* <br>

src/main/java/org/cufy/http/wrapper/extensions.kt

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -217,19 +217,3 @@ operator fun <E : Endpoint, R : RequestContext<E, T, R>, T : ResponseContext<E,
217217
*/
218218
operator fun <E : Endpoint, R : RequestContext<E, T, R>, T : ResponseContext<E, R, T>> T.component2(): T =
219219
this
220-
221-
/**
222-
* Invoke the given [setter] with this as the receiver and the endpoint as the parameter.
223-
*/
224-
fun <E : Endpoint, T : EndpointWrapper<E, T>> T.set(setter: T.(E) -> Unit): T {
225-
setter(this, this.endpoint)
226-
return this
227-
}
228-
229-
/**
230-
* Invoke the given [getter] with this as the receiver and the endpoint as the parameter
231-
* and return the invocation result.
232-
*/
233-
fun <E : Endpoint, T : EndpointWrapper<E, T>, V> T.get(getter: T.(E) -> V): V {
234-
return getter(this, this.endpoint)
235-
}

0 commit comments

Comments
 (0)