Skip to content

Commit 64fad9c

Browse files
CopilotCopilot
andcommitted
Add JavaDoc comments to non-private methods in Functional.java
Add comprehensive JavaDoc documentation with Example Usage sections to all non-private methods and the constructor in the Functional utility class. No code logic was modified. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 0bb2a8d commit 64fad9c

1 file changed

Lines changed: 141 additions & 0 deletions

File tree

microsphere-java-core/src/main/java/io/microsphere/util/Functional.java

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,40 @@ public class Functional<V> {
6969

7070
private Boolean matched = null;
7171

72+
/**
73+
* Constructs a new {@link Functional} instance with the given name and value.
74+
*
75+
* <h3>Example Usage</h3>
76+
* <pre>{@code
77+
* // Subclass creating a named Functional wrapper
78+
* Functional<String> func = new Functional<>("greeting", "Hello, World!");
79+
* }</pre>
80+
*
81+
* @param name the descriptive name for this functional instance
82+
* @param value the value to be operated on
83+
* @since 1.0.0
84+
*/
7285
protected Functional(String name, V value) {
7386
this.name = name;
7487
this.value = value;
7588
}
7689

90+
/**
91+
* Tests the current value against the given {@link Predicate}. If the predicate matches,
92+
* subsequent operations ({@link #as(Function)}, {@link #apply(Consumer)}) will be executed;
93+
* otherwise, they will be skipped. If the value is {@code null}, the predicate is not evaluated.
94+
*
95+
* <h3>Example Usage</h3>
96+
* <pre>{@code
97+
* Functional.value("microsphere")
98+
* .on(s -> s.startsWith("micro"))
99+
* .apply(s -> System.out.println("Matched: " + s));
100+
* }</pre>
101+
*
102+
* @param predicate the condition to test the value against
103+
* @return this {@link Functional} instance for fluent chaining
104+
* @since 1.0.0
105+
*/
77106
public Functional<V> on(Predicate<? super V> predicate) {
78107
if (isSkip()) {
79108
return this;
@@ -85,6 +114,23 @@ public Functional<V> on(Predicate<? super V> predicate) {
85114
return this;
86115
}
87116

117+
/**
118+
* Transforms the current value using the given {@link Function} and returns a new {@link Functional}
119+
* wrapping the result. If the previous {@link #on(Predicate)} did not match, the transformation
120+
* is skipped and this instance is returned as-is.
121+
*
122+
* <h3>Example Usage</h3>
123+
* <pre>{@code
124+
* Functional<Integer> length = Functional.value("Hello")
125+
* .on(s -> s.length() > 3)
126+
* .as(String::length);
127+
* }</pre>
128+
*
129+
* @param <R> the type of the transformed result
130+
* @param function the transformation function to apply to the value
131+
* @return a new {@link Functional} wrapping the transformed result, or this instance if skipped
132+
* @since 1.0.0
133+
*/
88134
public <R> Functional<R> as(Function<V, R> function) {
89135
if (isSkip()) {
90136
return (Functional<R>) this;
@@ -93,6 +139,20 @@ public <R> Functional<R> as(Function<V, R> function) {
93139
return new Functional(this.name, result);
94140
}
95141

142+
/**
143+
* Applies the given {@link Consumer} to the current value as a terminal operation.
144+
* If the previous {@link #on(Predicate)} did not match, the consumer is not invoked.
145+
*
146+
* <h3>Example Usage</h3>
147+
* <pre>{@code
148+
* Functional.value(99)
149+
* .on(n -> n > 50)
150+
* .apply(n -> System.out.println("Large number: " + n));
151+
* }</pre>
152+
*
153+
* @param valueConsumer the consumer to apply to the value
154+
* @since 1.0.0
155+
*/
96156
public void apply(Consumer<V> valueConsumer) {
97157
if (isSkip()) {
98158
return;
@@ -104,6 +164,19 @@ private boolean isSkip() {
104164
return FALSE.equals(matched);
105165
}
106166

167+
/**
168+
* Returns a string representation of this {@link Functional} instance, including its name,
169+
* value, and current match state.
170+
*
171+
* <h3>Example Usage</h3>
172+
* <pre>{@code
173+
* String text = Functional.of("config", "debug").toString();
174+
* // Output: Functional{name='config', value=debug, matched=null}
175+
* }</pre>
176+
*
177+
* @return a string describing this instance
178+
* @since 1.0.0
179+
*/
107180
@Override
108181
public String toString() {
109182
return "Functional{" +
@@ -113,18 +186,86 @@ public String toString() {
113186
'}';
114187
}
115188

189+
/**
190+
* Creates an unnamed {@link Functional} instance whose value is obtained from the given
191+
* {@link Supplier}. The supplier is evaluated immediately.
192+
*
193+
* <h3>Example Usage</h3>
194+
* <pre>{@code
195+
* Functional<Long> timestamp = Functional.value(System::currentTimeMillis);
196+
* timestamp.on(t -> t > 0L)
197+
* .apply(t -> System.out.println("Current time: " + t));
198+
* }</pre>
199+
*
200+
* @param <V> the type of the value
201+
* @param valueSupplier the supplier providing the value
202+
* @return a new unnamed {@link Functional} instance wrapping the supplied value
203+
* @since 1.0.0
204+
*/
116205
public static <V> Functional<V> value(Supplier<V> valueSupplier) {
117206
return value(valueSupplier.get());
118207
}
119208

209+
/**
210+
* Creates an unnamed {@link Functional} instance wrapping the given value.
211+
*
212+
* <h3>Example Usage</h3>
213+
* <pre>{@code
214+
* Functional.value("Hello, World!")
215+
* .on(s -> !s.isEmpty())
216+
* .as(String::toUpperCase)
217+
* .apply(s -> System.out.println(s));
218+
* }</pre>
219+
*
220+
* @param <V> the type of the value
221+
* @param value the value to wrap
222+
* @return a new unnamed {@link Functional} instance wrapping the value
223+
* @since 1.0.0
224+
*/
120225
public static <V> Functional<V> value(V value) {
121226
return of(UNNAMED, value);
122227
}
123228

229+
/**
230+
* Creates a named {@link Functional} instance whose value is obtained from the given
231+
* {@link Supplier}. The supplier is evaluated immediately. The name is useful for
232+
* debugging and identification in {@link #toString()}.
233+
*
234+
* <h3>Example Usage</h3>
235+
* <pre>{@code
236+
* Functional<List<String>> items = Functional.of("inventory", () -> fetchItems());
237+
* items.on(list -> !list.isEmpty())
238+
* .apply(list -> System.out.println("Items count: " + list.size()));
239+
* }</pre>
240+
*
241+
* @param <V> the type of the value
242+
* @param name the descriptive name for this functional instance
243+
* @param valueSupplier the supplier providing the value
244+
* @return a new named {@link Functional} instance wrapping the supplied value
245+
* @since 1.0.0
246+
*/
124247
public static <V> Functional<V> of(String name, Supplier<V> valueSupplier) {
125248
return of(name, valueSupplier.get());
126249
}
127250

251+
/**
252+
* Creates a named {@link Functional} instance wrapping the given value. The name is useful
253+
* for debugging and identification in {@link #toString()}.
254+
*
255+
* <h3>Example Usage</h3>
256+
* <pre>{@code
257+
* Functional.of("user-email", user.getEmail())
258+
* .on(email -> email.contains("@"))
259+
* .as(String::toLowerCase)
260+
* .apply(email -> sendNotification(email));
261+
* }</pre>
262+
*
263+
* @param <V> the type of the value
264+
* @param name the descriptive name for this functional instance
265+
* @param value the value to wrap
266+
* @return a new named {@link Functional} instance wrapping the value
267+
* @since 1.0.0
268+
*/
128269
public static <V> Functional<V> of(String name, V value) {
129270
return new Functional<>(name, value);
130271
}

0 commit comments

Comments
 (0)