-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
202 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
core/nuts/src/main/java/net/thevpc/nuts/util/CallableSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/** | ||
* ==================================================================== | ||
* Nuts : Network Updatable Things Service | ||
* (universal package manager) | ||
* <br> | ||
* is a new Open Source Package Manager to help install packages | ||
* and libraries for runtime execution. Nuts is the ultimate companion for | ||
* maven (and other build managers) as it helps installing all package | ||
* dependencies at runtime. Nuts is not tied to java and is a good choice | ||
* to share shell scripts and other 'things' . Its based on an extensible | ||
* architecture to help supporting a large range of sub managers / repositories. | ||
* | ||
* <br> | ||
* <p> | ||
* Copyright [2020] [thevpc] | ||
* 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. | ||
* <br> | ||
* ==================================================================== | ||
*/ | ||
package net.thevpc.nuts.util; | ||
|
||
import net.thevpc.nuts.NConstants; | ||
|
||
import java.util.Collection; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Stream; | ||
|
||
public interface CallableSupport<T> { | ||
|
||
static <T> CallableSupport<T> resolve(Collection<CallableSupport<T>> source, Supplier<String> emptyMessage) { | ||
if (source == null) { | ||
return invalid(emptyMessage); | ||
} | ||
return resolve(source.stream(), emptyMessage); | ||
} | ||
|
||
static <T> CallableSupport<T> resolve(Stream<CallableSupport<T>> source, Supplier<String> emptyMessage) { | ||
if (source == null) { | ||
return invalid(emptyMessage); | ||
} | ||
return resolveSupplier(source.map(x -> () -> x), emptyMessage); | ||
} | ||
|
||
static <T> CallableSupport<T> resolveSupplier(Collection<Supplier<CallableSupport<T>>> source, Supplier<String> emptyMessage) { | ||
if (source == null) { | ||
return invalid(emptyMessage); | ||
} | ||
return resolveSupplier(source.stream(), emptyMessage); | ||
} | ||
|
||
static <T> CallableSupport<T> resolveSupplier(Stream<Supplier<CallableSupport<T>>> source, Supplier<String> emptyMessage) { | ||
Object[] track = new Object[2]; | ||
if (source != null) { | ||
source.forEach(i -> { | ||
CallableSupport<T> s = i.get(); | ||
NAssert.requireNonNull(s, "NSupported<T>"); | ||
int supportLevel = s.getSupportLevel(); | ||
boolean valid = supportLevel > 0; | ||
if (valid) { | ||
if (track[0] == null) { | ||
track[0] = s; | ||
track[1] = supportLevel; | ||
} else { | ||
int oldSupportLevel = (Integer) track[1]; | ||
if (supportLevel > oldSupportLevel) { | ||
track[0] = s; | ||
track[1] = supportLevel; | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
CallableSupport<T> r = (CallableSupport<T>) track[0]; | ||
if (r == null) { | ||
return invalid(emptyMessage); | ||
} | ||
return (CallableSupport<T>) r; | ||
} | ||
|
||
static <T> CallableSupport<T> of(int supportLevel, T value) { | ||
return of(supportLevel, value, null); | ||
} | ||
|
||
static <T> CallableSupport<T> of(int supportLevel, T value, Supplier<String> emptyMessage) { | ||
return supportLevel <= 0 ? invalid(emptyMessage) : new DefaultCallableSupport<>(() -> value, supportLevel, emptyMessage); | ||
} | ||
|
||
static <T> CallableSupport<T> of(int supportLevel, Supplier<T> supplier) { | ||
return of(supportLevel, supplier, null); | ||
} | ||
|
||
static <T> CallableSupport<T> of(int supportLevel, Supplier<T> supplier, Supplier<String> emptyMessage) { | ||
return (supportLevel <= 0 || supplier == null) ? invalid(emptyMessage) | ||
: new DefaultCallableSupport<>(supplier, supportLevel, emptyMessage) | ||
; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
static <T> CallableSupport<T> invalid(Supplier<String> emptyMessage) { | ||
return new DefaultCallableSupport<>(null, NConstants.Support.NO_SUPPORT, emptyMessage); | ||
} | ||
|
||
static <T> boolean isValid(CallableSupport<T> s) { | ||
return s != null && s.isValid(); | ||
} | ||
|
||
T call(); | ||
|
||
default boolean isValid() { | ||
return getSupportLevel() > 0; | ||
} | ||
|
||
int getSupportLevel(); | ||
|
||
NOptional<T> toOptional(); | ||
} |
62 changes: 62 additions & 0 deletions
62
core/nuts/src/main/java/net/thevpc/nuts/util/DefaultCallableSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* To change this license header, choose License Headers in Project Properties. | ||
* To change this template file, choose Tools | Templates | ||
* and open the template in the editor. | ||
*/ | ||
package net.thevpc.nuts.util; | ||
|
||
import net.thevpc.nuts.NCallableSupport; | ||
import net.thevpc.nuts.NNoSuchElementException; | ||
import net.thevpc.nuts.NSession; | ||
import net.thevpc.nuts.reserved.NApiUtilsRPI; | ||
|
||
import java.util.NoSuchElementException; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Default implementation of NutsSupported | ||
* | ||
* @param <T> value type | ||
* @author thevpc | ||
*/ | ||
public class DefaultCallableSupport<T> implements CallableSupport<T> { | ||
private final Supplier<T> value; | ||
private final int supportLevel; | ||
private final Supplier<String> emptyMessage; | ||
|
||
public DefaultCallableSupport(Supplier<T> value, int supportLevel, Supplier<String> emptyMessage) { | ||
this.value = value; | ||
if (this.value == null && supportLevel > 0) { | ||
throw new IllegalArgumentException("null callable requires invalid support"); | ||
} else if (this.value != null && supportLevel <= 0) { | ||
throw new IllegalArgumentException("non null callable requires valid support"); | ||
} | ||
this.supportLevel = supportLevel; | ||
this.emptyMessage = emptyMessage == null ? () -> "not found" : emptyMessage; | ||
} | ||
|
||
public T call() { | ||
if (isValid()) { | ||
return value.get(); | ||
} else { | ||
String m = emptyMessage.get(); | ||
if(m==null){ | ||
m="not found"; | ||
} | ||
throw new NoSuchElementException(m); | ||
} | ||
} | ||
|
||
public int getSupportLevel() { | ||
return supportLevel; | ||
} | ||
|
||
@Override | ||
public NOptional<T> toOptional() { | ||
if (isValid()) { | ||
return NOptional.ofCallable(s -> value.get()); | ||
} | ||
return NOptional.ofEmpty(s->NMsg.ofPlain(emptyMessage.get())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters