| 1 | package com.github.dakusui.actionunit.actions.cmd; | |
| 2 | ||
| 3 | import java.util.*; | |
| 4 | import java.util.function.BiFunction; | |
| 5 | import java.util.function.BiPredicate; | |
| 6 | import java.util.function.Function; | |
| 7 | ||
| 8 | import static java.util.Collections.unmodifiableList; | |
| 9 | ||
| 10 | /** | |
| 11 | * A trivial interface to alias {@code BiFunction<String, String, String>} for readability. | |
| 12 | * | |
| 13 | * Sometimes, a program (`echo`, `ls`, etc.) may behave because of its implementation on a certain host. | |
| 14 | * To achieve intended behavior on the host, sometimes we need to specify a full-path of a command. | |
| 15 | * For instance, `echo` is a built-in of `bash` and to use `-E` option of `echo` command (not built-in!), | |
| 16 | * we must specify `/bin/echo`, instead of just `echo`. | |
| 17 | * | |
| 18 | * `CommanderConfig` has a method `programNameResolver`, which returns {@code BiFunction<String, String, String>}. | |
| 19 | * This bi-function is used to figure out an actual program name to be run on a certain host. | |
| 20 | * | |
| 21 | * That is, a pair of string values, which are host name and a command name are given to the bi-function, which is supposed to return an actual program name executed on the host as the given command name. | |
| 22 | */ | |
| 23 | public interface ProgramNameResolver extends BiFunction<String, String, String> { | |
| 24 | /** | |
| 25 | * A builder to create a {@link ProgramNameResolver} instance easily. | |
| 26 | * The instance built by this class behaves as follows, when a pair of hostname and command name: | |
| 27 | * | |
| 28 | * Check a list of entries ({@link ProgramNameResolver.Builder.Entry}) one by one if its `matcher` returns `true` for the pair. | |
| 29 | * If it finds an entry whose `matcher` gives true, applies a command name to its `resolver` and returns the value as the result of the program name resolver. | |
| 30 | * If no entry matches with the host name and the command name, an {@link NoSuchElementException} will be thrown. | |
| 31 | */ | |
| 32 | class Builder { | |
| 33 | /** | |
| 34 | * A conversion rule entry. | |
| 35 | */ | |
| 36 | interface Entry { | |
| 37 | /** | |
| 38 | * A bi-predicate that checks if a given pair of a host name and a command name matches this entry. | |
| 39 | * The first parameter corresponds to the host name and the second one corresponds to the command name. | |
| 40 | * | |
| 41 | * @return matcher bi-predicate. | |
| 42 | */ | |
| 43 | BiPredicate<String, String> matcher(); | |
| 44 | ||
| 45 | /** | |
| 46 | * Returns a function that maps a command name to an actual program name, executed on the host, checked by the bi-predicate retuened by `matcher()` method. | |
| 47 | * | |
| 48 | * @return A function that maps a command name to an actual program name | |
| 49 | */ | |
| 50 | Function<String, String> resolver(); | |
| 51 | ||
| 52 | /** | |
| 53 | * Createsn an instance of {@link Entry}. | |
| 54 | * | |
| 55 | * @param matcher A matcher bi-predicate. | |
| 56 | * @param resolver A resolver function. | |
| 57 | * @return A new {@link Entry} objeect. | |
| 58 | * @see this#matcher() | |
| 59 | * @see this#resolver() | |
| 60 | */ | |
| 61 | static Entry create(BiPredicate<String, String> matcher, Function<String, String> resolver) { | |
| 62 |
1
1. create : replaced return value with null for com/github/dakusui/actionunit/actions/cmd/ProgramNameResolver$Builder$Entry::create → KILLED |
return new Entry() { |
| 63 | @Override | |
| 64 | public BiPredicate<String, String> matcher() { | |
| 65 |
1
1. matcher : replaced return value with null for com/github/dakusui/actionunit/actions/cmd/ProgramNameResolver$Builder$Entry$1::matcher → KILLED |
return matcher; |
| 66 | } | |
| 67 | ||
| 68 | @Override | |
| 69 | public Function<String, String> resolver() { | |
| 70 |
1
1. resolver : replaced return value with null for com/github/dakusui/actionunit/actions/cmd/ProgramNameResolver$Builder$Entry$1::resolver → KILLED |
return resolver; |
| 71 | } | |
| 72 | }; | |
| 73 | } | |
| 74 | } | |
| 75 | ||
| 76 | final List<Entry> entries = new LinkedList<>(); | |
| 77 | ||
| 78 | /** | |
| 79 | * Constructs a new {@link ProgramNameResolver.Builder} object. | |
| 80 | */ | |
| 81 | public Builder() { | |
| 82 | } | |
| 83 | ||
| 84 | /** | |
| 85 | * Registers a transforming rule for program names. | |
| 86 | * | |
| 87 | * Note that an entry registered later is more prioritized than ones registered earlier. | |
| 88 | * | |
| 89 | * @param matcher A predicate to decide if program name resolution by {@code programNameResolver} should be applied. | |
| 90 | * @param programNameResolver A function that converts a program name for a host. | |
| 91 | * @return This object. | |
| 92 | */ | |
| 93 | public Builder register(BiPredicate<String, String> matcher, Function<String, String> programNameResolver) { | |
| 94 |
1
1. register : removed call to java/util/List::add → KILLED |
this.entries.add(0, Entry.create(matcher, programNameResolver)); |
| 95 |
1
1. register : replaced return value with null for com/github/dakusui/actionunit/actions/cmd/ProgramNameResolver$Builder::register → KILLED |
return this; |
| 96 | } | |
| 97 | ||
| 98 | /** | |
| 99 | * Builds a new {@link ProgramNameResolver} object. | |
| 100 | * | |
| 101 | * @return A new {@link ProgramNameResolver} object | |
| 102 | */ | |
| 103 | public ProgramNameResolver build() { | |
| 104 |
1
1. build : replaced return value with null for com/github/dakusui/actionunit/actions/cmd/ProgramNameResolver$Builder::build → KILLED |
return new ProgramNameResolver() { |
| 105 | final List<Entry> entries = unmodifiableList(new ArrayList<>(Builder.this.entries)); | |
| 106 | ||
| 107 | @Override | |
| 108 | public String apply(String host, String programName) { | |
| 109 |
1
1. apply : replaced return value with "" for com/github/dakusui/actionunit/actions/cmd/ProgramNameResolver$Builder$1::apply → KILLED |
return this.entries.stream() |
| 110 |
2
1. lambda$apply$0 : replaced boolean return with false for com/github/dakusui/actionunit/actions/cmd/ProgramNameResolver$Builder$1::lambda$apply$0 → KILLED 2. lambda$apply$0 : replaced boolean return with true for com/github/dakusui/actionunit/actions/cmd/ProgramNameResolver$Builder$1::lambda$apply$0 → KILLED |
.filter(each -> each.matcher().test(host, programName)) |
| 111 | .findFirst() | |
| 112 |
1
1. lambda$apply$1 : replaced return value with "" for com/github/dakusui/actionunit/actions/cmd/ProgramNameResolver$Builder$1::lambda$apply$1 → KILLED |
.map(v -> v.resolver().apply(programName)) |
| 113 | .orElseThrow(NoSuchElementException::new); | |
| 114 | } | |
| 115 | }; | |
| 116 | } | |
| 117 | } | |
| 118 | ||
| 119 | /** | |
| 120 | * Creates a {@link ProgramNameResolver} instance with a handy default for UNIX environment. | |
| 121 | * | |
| 122 | * @return An instance for UNIX environment. | |
| 123 | */ | |
| 124 | static ProgramNameResolver createForUnix() { | |
| 125 |
1
1. createForUnix : replaced return value with null for com/github/dakusui/actionunit/actions/cmd/ProgramNameResolver::createForUnix → KILLED |
return new Builder() |
| 126 |
2
1. lambda$createForUnix$0 : replaced boolean return with false for com/github/dakusui/actionunit/actions/cmd/ProgramNameResolver::lambda$createForUnix$0 → KILLED 2. lambda$createForUnix$1 : replaced return value with "" for com/github/dakusui/actionunit/actions/cmd/ProgramNameResolver::lambda$createForUnix$1 → KILLED |
.register((h, c) -> true, c -> c) |
| 127 |
3
1. lambda$createForUnix$2 : replaced boolean return with false for com/github/dakusui/actionunit/actions/cmd/ProgramNameResolver::lambda$createForUnix$2 → KILLED 2. lambda$createForUnix$2 : replaced boolean return with true for com/github/dakusui/actionunit/actions/cmd/ProgramNameResolver::lambda$createForUnix$2 → KILLED 3. lambda$createForUnix$3 : replaced return value with "" for com/github/dakusui/actionunit/actions/cmd/ProgramNameResolver::lambda$createForUnix$3 → KILLED |
.register((h, c) -> "echo".equals(c), c -> "/bin/echo") |
| 128 | .build(); | |
| 129 | } | |
| 130 | } | |
Mutations | ||
| 62 |
1.1 |
|
| 65 |
1.1 |
|
| 70 |
1.1 |
|
| 94 |
1.1 |
|
| 95 |
1.1 |
|
| 104 |
1.1 |
|
| 109 |
1.1 |
|
| 110 |
1.1 2.2 |
|
| 112 |
1.1 |
|
| 125 |
1.1 |
|
| 126 |
1.1 2.2 |
|
| 127 |
1.1 2.2 3.3 |