Skip to content
Snippets Groups Projects
Commit 9fd3e8df authored by Georg Seibt's avatar Georg Seibt :nerd:
Browse files

concatenation method for array+varargs

parent b1d70360
No related branches found
No related tags found
No related merge requests found
......@@ -2,9 +2,11 @@ package de.uni_passau.fim.seibt.gitwrapper.process;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;
import org.apache.commons.io.IOUtils;
......@@ -42,12 +44,7 @@ public class ProcessExecutor {
return exec(workingDir, redirectError, cmd);
}
String[] cmdArray = new String[parameters.length + 1];
cmdArray[0] = cmd;
System.arraycopy(parameters, 0, cmdArray, 1, parameters.length);
return exec(workingDir, redirectError, cmdArray);
return exec(workingDir, redirectError, merge(false, parameters, cmd));
}
/**
......@@ -92,8 +89,8 @@ public class ProcessExecutor {
ExecRes res = new ExecRes();
Process p = builder.start();
res.output = IOUtils.toString(p.getInputStream());
res.exitCode = p.waitFor();
res.output = IOUtils.toString(p.getInputStream());
LOG.fine(() -> String.format("Execution of '%s' returned exit code %d.", cmd, res.exitCode));
LOG.finest(() -> String.format("Execution of '%s' output:%n%s", cmd, res.output.trim()));
......@@ -104,4 +101,33 @@ public class ProcessExecutor {
return Optional.empty();
}
}
/**
* Returns an array representing the concatenation of <code>arr</code> and <code>varArgs</code> in the order defined
* by <code>av</code>. If one of <code>arr</code> and <code>varArgs</code> is null, the other one is returned.
*
* @param av
* if true returns [arr] + [varArgs] otherwise returns [varArgs] + [arr]
* @param arr
* the array
* @param varArgs
* the variable number of arguments
* @return the concatenation
*/
public static String[] merge(boolean av, String[] arr, String... varArgs) {
if (arr != null && varArgs == null) {
return arr;
} else if (arr == null && varArgs != null) {
return varArgs;
} else if (arr == null && varArgs == null){
throw new IllegalArgumentException("Both arr and varArgs are null!");
}
if (av) {
return Stream.concat(Arrays.stream(arr), Arrays.stream(varArgs)).toArray(String[]::new);
} else {
return Stream.concat(Arrays.stream(varArgs), Arrays.stream(arr)).toArray(String[]::new);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment