Skip to content

Commit

Permalink
20240524
Browse files Browse the repository at this point in the history
  • Loading branch information
thevpc committed May 22, 2024
1 parent 46b5ec4 commit e0765b6
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ private DefaultNRepositoryDB(NSession session) {
reg("google", "maven@https://maven.google.com");
reg("spring", "maven@https://repo.spring.io/release", "spring-framework");
reg("maven-thevpc-git", "maven@https://raw.githubusercontent.com/thevpc/vpc-public-maven/master", "vpc-public-maven");
reg("nuts-public", "maven@dotfilefs:https://raw.githubusercontent.com/thevpc/nuts-public/master", "vpc-public-nuts","nuts-thevpc-git");
reg("nuts-preview", "maven@dotfilefs://raw.githubusercontent.com/thevpc/nuts-preview/master", "preview");
reg("nuts-public", "maven@dotfilefs:https://raw.githubusercontent.com/thevpc/nuts-public/master", "vpc-public-nuts","nuts-thevpc-git");
reg("nuts-preview", "maven@dotfilefs:https://raw.githubusercontent.com/thevpc/nuts-preview/master", "preview");
reg("thevpc", "maven@htmlfs:https://thevpc.net/maven", "dev");
reg("thevpc-goodies", "nuts@htmlfs:https://thevpc.net/maven-goodies", "goodies");
reg("local", "nuts@local");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public class DefaultNSettingsInternalExecutable extends DefaultInternalNExecutableCommand {

public DefaultNSettingsInternalExecutable(String[] args, NExecCmd execCommand) {
super("fetch", args, execCommand);
super("settings", args, execCommand);
}

private List<NSettingsSubCommand> subCommands;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
public class NReservedOptionalValidCallable<T> extends NReservedOptionalValid<T> implements Cloneable {

private final NCallable<T> value;
private T result;
private boolean evaluated;

public NReservedOptionalValidCallable(NCallable<T> value) {
NAssert.requireNonNull(value, "callable");
Expand Down Expand Up @@ -40,6 +42,10 @@ public <V> NOptional<V> then(Function<T, V> mapper) {

@Override
public T get(NSession session) {
return value.call(session);
if(!evaluated){
result=value.call(session);
evaluated=true;
}
return result;
}
}
123 changes: 123 additions & 0 deletions core/nuts/src/main/java/net/thevpc/nuts/util/CallableSupport.java
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();
}
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()));
}
}
5 changes: 5 additions & 0 deletions toolbox/noapi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@
<artifactId>nlib-md</artifactId>
<version>0.8.4.0</version>
</dependency>
<dependency>
<groupId>org.jruby</groupId>
<artifactId>jruby-complete</artifactId>
<version>1.7.26</version>
</dependency>
<dependency>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctorj</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public static String toCode(TypeInfo o, boolean includeDesc, String indent, AppM
type += ("(" + o.getMinLength().trim() + "..." + o.getMaxLength().trim() + ")");
} else if (!NBlankable.isBlank(o.getMinLength())) {
type += (">=" + o.getMinLength().trim());
} else if (!NBlankable.isBlank(o.getMaxLength())) {
} else if (!NBlankable.isBlank(o.getMinLength())) {
type += ("<=" + o.getMinLength().trim());
}
if (o.getEnumValues() != null && o.getEnumValues().size() > 0) {
Expand All @@ -187,7 +187,7 @@ public static String toCode(TypeInfo o, boolean includeDesc, String indent, AppM
type += ("(" + o.getMinLength().trim() + "..." + o.getMaxLength().trim() + ")");
} else if (!NBlankable.isBlank(o.getMinLength())) {
type += (">=" + o.getMinLength().trim());
} else if (!NBlankable.isBlank(o.getMaxLength())) {
} else if (!NBlankable.isBlank(o.getMinLength())) {
type += ("<=" + o.getMinLength().trim());
}
if (o.getEnumValues() != null && o.getEnumValues().size() > 0) {
Expand Down

0 comments on commit e0765b6

Please sign in to comment.