org.jolokia.docker.maven.config.Arguments Maven / Gradle / Ivy
The newest version!
package org.jolokia.docker.maven.config;
import org.jolokia.docker.maven.util.EnvUtil;
import java.util.*;
public class Arguments {
private String shell;
private List exec;
/**
* Used to distinguish between shorter version
*
*
* <cmd>
* <arg>echo</arg>
* <arg>Hello, world!</arg>
* </cmd>
*
*
* from the full one
*
*
* <cmd>
* <exec>
* <arg>echo</arg>
* <arg>Hello, world!</arg>
* <exec>
* </cmd>
*
*
* and throw a validation error if both specified.
*/
private List execInlined = new ArrayList<>();
public Arguments() {
this(null);
}
public Arguments(String shell) {
this.shell = shell;
}
/**
* Used to support shell specified as a default parameter, e.g.
*
*
* <cmd>java -jar $HOME/server.jar</cmd>
*
*
* Read more on
* this and other useful techniques.
*
*/
public void set(String shell) {
setShell(shell);
}
public void setShell(String shell) {
this.shell = shell;
}
public String getShell() {
return shell;
}
public void setExec(List exec) {
this.exec = exec;
}
/**
* @see Arguments#execInlined
*/
@SuppressWarnings("unused")
public void setArg(String arg) {
this.execInlined.add(arg);
}
public List getExec() {
return exec == null ? execInlined : exec;
}
public void validate() throws IllegalArgumentException {
int valueSources = 0;
if (shell != null) {
valueSources ++;
}
if (exec != null && !exec.isEmpty()) {
valueSources ++;
}
if (!execInlined.isEmpty()) {
valueSources ++;
}
if (valueSources != 1){
throw new IllegalArgumentException("Argument conflict: either shell or args should be specified and only in one form.");
}
}
public List asStrings() {
if (shell != null) {
return Arrays.asList(EnvUtil.splitOnSpaceWithEscape(shell));
}
if (exec != null) {
return Collections.unmodifiableList(exec);
}
return Collections.unmodifiableList(execInlined);
}
public static class Builder {
private String shell;
private List params;
public static Builder get(){
return new Builder();
}
public Builder withShell(String shell){
this.shell = shell;
return this;
}
public Builder withParam(String param){
if (params == null) {
params = new ArrayList<>();
}
this.params.add(param);
return this;
}
public Arguments build(){
Arguments a = new Arguments();
a.setShell(shell);
a.setExec(params);
return a;
}
}
}