Skip to content

Commit

Permalink
replaced let of value method with opt method
Browse files Browse the repository at this point in the history
  • Loading branch information
evpl committed Mar 9, 2024
1 parent 37d64a6 commit b4dda2f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 54 deletions.
64 changes: 32 additions & 32 deletions src/main/java/com/plugatar/jkscope/JKScope.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@
* <li>{@link #withResource(AutoCloseable, ThConsumer)}</li>
* <li>{@link #with(Object, Object, ThBiConsumer)}</li>
* <li>{@link #with(Object, Object, Object, ThTriConsumer)}</li>
* <li>{@link #let(Object)}</li>
* <li>{@link #letNonNull(Object)}</li>
* <li>{@link #let(ThSupplier)}</li>
* <li>{@link #letInt(ThIntSupplier)}</li>
* <li>{@link #letLong(ThLongSupplier)}</li>
Expand All @@ -104,6 +102,8 @@
* <li>{@link #letWithResource(AutoCloseable, ThFunction)}</li>
* <li>{@link #letWith(Object, Object, ThBiFunction)}</li>
* <li>{@link #letWith(Object, Object, Object, ThTriFunction)}</li>
* <li>{@link #opt(Object)}</li>
* <li>{@link #optNonNull(Object)}</li>
* <li>{@link #lazy(ThSupplier)}</li>
* <li>{@link #lazy(Object, ThSupplier)}</li>
* <li>{@link #lazy(Lazy.ThreadSafetyMode, ThSupplier)}</li>
Expand All @@ -116,7 +116,7 @@
* it.put("value2", 2);
* };
*
* let("value").takeUnless(it -> it.isEmpty()).takeIf(it -> it.length() < 100).letIt(it -> System.out.println(it));
* opt("value").takeUnless(it -> it.isEmpty()).takeIf(it -> it.length() < 100).letIt(it -> System.out.println(it));
*
* int value = letIntRec(10, (n, func) -> {
* if (n < 2) {
Expand Down Expand Up @@ -373,35 +373,6 @@ static <V1, V2, V3> void with(final V1 value1,
ThTriConsumer.unchecked(block).accept(value1, value2, value3);
}

/**
* Returns {@link Opt} instance of given value, {@code null} is also considered as a value.
* <pre>{@code
* let(value).takeNonNull().takeUnless(it -> it.isEmpty()).takeIf(it -> it.length() < 100).letIt(it -> System.out.println(it));
* }</pre>
*
* @param value the value
* @param <V> the type of the value
* @return {@link Opt} instance of given value
*/
static <V> Opt<V> let(final V value) {
return Opt.of(value);
}

/**
* Returns {@link Opt} instance of given value or empty {@link Opt} instance if given value is null. Equivalent to
* calling a chain of methods: {@code let(value).takeNonNull()}.
* <pre>{@code
* letNonNull(value).takeUnless(it -> it.isEmpty()).takeIf(it -> it.length() < 100).letIt(it -> System.out.println(it));
* }</pre>
*
* @param value the value
* @param <V> the type of the value
* @return {@link Opt} instance of given value or empty {@link Opt} instance if given value is null
*/
static <V> Opt<V> letNonNull(final V value) {
return Opt.nonNullOf(value);
}

/**
* Performs given function block and returns result.
* <pre>{@code
Expand Down Expand Up @@ -814,6 +785,35 @@ static <V1, V2, V3, R> R letWith(final V1 value1,
return ThTriFunction.unchecked(block).apply(value1, value2, value3);
}

/**
* Returns {@link Opt} instance of given value, {@code null} is also considered as a value.
* <pre>{@code
* opt(value).takeNonNull().takeUnless(it -> it.isEmpty()).takeIf(it -> it.length() < 100).letIt(it -> System.out.println(it));
* }</pre>
*
* @param value the value
* @param <V> the type of the value
* @return {@link Opt} instance of given value
*/
static <V> Opt<V> opt(final V value) {
return Opt.of(value);
}

/**
* Returns {@link Opt} instance of given value or empty {@link Opt} instance if given value is null. Equivalent to
* calling a chain of methods: {@code opt(value).takeNonNull()}.
* <pre>{@code
* optNonNull(value).takeUnless(it -> it.isEmpty()).takeIf(it -> it.length() < 100).letIt(it -> System.out.println(it));
* }</pre>
*
* @param value the value
* @param <V> the type of the value
* @return {@link Opt} instance of given value or empty {@link Opt} instance if given value is null
*/
static <V> Opt<V> optNonNull(final V value) {
return Opt.nonNullOf(value);
}

/**
* Returns a new {@link Lazy} instance that uses the specified initialization function and the
* {@link Lazy.ThreadSafetyMode#SYNCHRONIZED} thread-safety mode. The returned instance uses itself to synchronize
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/plugatar/jkscope/function/Utils.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2024 Evgenii Plugatar
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.plugatar.jkscope.function;

/**
Expand Down
44 changes: 22 additions & 22 deletions src/test/java/com/plugatar/jkscope/JKScopeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -809,28 +809,6 @@ void with3ArgsStaticMethodThrowsException() {
.isSameAs(throwable);
}

@Test
void letStaticMethod() {
final Object nonNullValue = new Object();
assertThat(JKScope.let(nonNullValue).get())
.isSameAs(nonNullValue);

final Object nullValue = null;
assertThat(JKScope.let(nullValue).get())
.isNull();
}

@Test
void letNonNullStaticMethod() {
final Object nonNullValue = new Object();
assertThat(JKScope.letNonNull(nonNullValue).get())
.isSameAs(nonNullValue);

final Object nullValue = null;
assertThat(JKScope.letNonNull(nullValue).isEmpty())
.isTrue();
}

@Test
void letSupplierStaticMethod() {
final Object result = new Object();
Expand Down Expand Up @@ -1347,6 +1325,28 @@ void letWith3ArgsStaticMethodThrowsException() {
.isSameAs(throwable);
}

@Test
void optStaticMethod() {
final Object nonNullValue = new Object();
assertThat(JKScope.opt(nonNullValue).get())
.isSameAs(nonNullValue);

final Object nullValue = null;
assertThat(JKScope.opt(nullValue).get())
.isNull();
}

@Test
void optNonNullStaticMethod() {
final Object nonNullValue = new Object();
assertThat(JKScope.optNonNull(nonNullValue).get())
.isSameAs(nonNullValue);

final Object nullValue = null;
assertThat(JKScope.optNonNull(nullValue).isEmpty())
.isTrue();
}

@Test
void lazyInitializerStaticMethod() {
final ThSupplier<Object, Throwable> initializer = () -> new Object();
Expand Down

0 comments on commit b4dda2f

Please sign in to comment.