Package: RestResponse
RestResponse
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
RestResponse(Optional, String) |
|
|
|
|
|
||||||||||||||||||||
empty() |
|
|
|
|
|
||||||||||||||||||||
flatMap(Function) |
|
|
|
|
|
||||||||||||||||||||
get() |
|
|
|
|
|
||||||||||||||||||||
getResponseStatus() |
|
|
|
|
|
||||||||||||||||||||
ifPresent(Consumer) |
|
|
|
|
|
||||||||||||||||||||
isPresent() |
|
|
|
|
|
||||||||||||||||||||
map(Function) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
Coverage
1: /*
2: * *********************************************************************************************************************
3: *
4: * blueMarine II: Semantic Media Centre
5: * http://tidalwave.it/projects/bluemarine2
6: *
7: * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/bluemarine2-src
23: * git clone https://github.com/tidalwave-it/bluemarine2-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.bluemarine2.rest;
28:
29: import javax.annotation.Nonnull;
30: import java.util.NoSuchElementException;
31: import java.util.Optional;
32: import java.util.function.Consumer;
33: import java.util.function.Function;
34: import lombok.AllArgsConstructor;
35: import lombok.Getter;
36: import lombok.extern.slf4j.Slf4j;
37: import static lombok.AccessLevel.PROTECTED;
38:
39: /***********************************************************************************************************************
40: *
41: * @author Fabrizio Giudici
42: *
43: **********************************************************************************************************************/
44: @AllArgsConstructor(access = PROTECTED) @Slf4j
45: public class RestResponse<T>
46: {
47: @Nonnull
48: private final Optional<T> datum;
49:
50: @Getter @Nonnull
51: private final String responseStatus;
52:
53: @Nonnull
54: public static <X> RestResponse<X> empty()
55: {
56: return new RestResponse<>(Optional.empty(), "");
57: }
58:
59: /*******************************************************************************************************************
60: *
61: * Returns the datum, if available.
62: *
63: * @return the datum
64: * @throws NoSuchElementException if the datum is not available
65: *
66: ******************************************************************************************************************/
67: @Nonnull
68: public T get()
69: throws NoSuchElementException
70: {
71: return datum.get();
72: }
73:
74: /*******************************************************************************************************************
75: *
76: * Returns <code>true</code> if the datum is available.
77: *
78: * @return <code>true</code> if the datum is available
79: *
80: ******************************************************************************************************************/
81: public boolean isPresent()
82: {
83: return datum.isPresent();
84: }
85:
86: /*******************************************************************************************************************
87: *
88: * Maps the result, if present, to a new value using a mapping function.
89: *
90: * @param <U> the type of the result
91: * @param mapper a mapping function to apply to the value, if present
92: * @return an {@code Optional} result
93: *
94: ******************************************************************************************************************/
95: @Nonnull
96: public <U> Optional<U> map (@Nonnull final Function<? super T, ? extends U> mapper)
97: {
98: return datum.map(mapper);
99: }
100:
101: /*******************************************************************************************************************
102: *
103: * Maps the result, if present, to a new value using a mapping function, avoiding wrapping with multiple
104: * {@code Optional}s.
105: *
106: * @param <U> the type of the result
107: * @param mapper a mapping function to apply to the value, if present
108: * @return an {@code Optional} result
109: *
110: ******************************************************************************************************************/
111: @Nonnull
112: public <U> Optional<U> flatMap (@Nonnull final Function<? super T, Optional<U>> mapper)
113: {
114: return datum.flatMap(mapper);
115: }
116:
117: /*******************************************************************************************************************
118: *
119: * If a value is present, invoke the specified consumer with the value, otherwise do nothing.
120: *
121: * @param consumer code to be executed if a value is present
122: *
123: ******************************************************************************************************************/
124: public void ifPresent (@Nonnull final Consumer<? super T> consumer)
125: {
126: datum.ifPresent(consumer);
127: }
128: }