
org.jbasics.command.CommandCall Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jbasics Show documentation
Show all versions of jbasics Show documentation
jBasics is a collection of useful utility classes for Java. This includes helper for XML, mathematic functions,
restful web services helper, pattern oriented programming interfaces and more. Currently Java7 and up is
supported. Version 1.0 will required at leaset Java8.
/*
* Copyright (c) 2009-2015
* IT-Consulting Stephan Schloepke (http://www.schloepke.de/)
* klemm software consulting Mirko Klemm (http://www.klemm-scs.com/)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jbasics.command;
import org.jbasics.checker.ContractCheck;
import org.jbasics.pattern.factory.ParameterFactory;
import org.jbasics.types.transpose.MapTransposer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
public class CommandCall {
public final static MapTransposer COMMAND_PARAMETER_TRANSPOSER = new MapTransposer(
new ParameterFactory() {
public String create(final CommandParameter param) {
return param == null ? null : param.getParameterName();
}
}, true);
private final String name;
private final Map parameters;
public CommandCall(final String name) {
this.name = ContractCheck.mustNotBeNullOrTrimmedEmpty(name, "name"); //$NON-NLS-1$
this.parameters = Collections.emptyMap();
}
public CommandCall(final String name, final CommandParameter... parameters) {
this(name, Arrays.asList(parameters));
}
public CommandCall(final String name, final Collection parameters) {
this.name = ContractCheck.mustNotBeNullOrTrimmedEmpty(name, "name"); //$NON-NLS-1$
this.parameters = parameters != null && parameters.size() > 0 ? Collections.unmodifiableMap(CommandCall.COMMAND_PARAMETER_TRANSPOSER
.transpose(parameters)) : Collections.emptyMap();
}
public static CommandCall create(final String[] args) {
String commandName = ContractCheck.mustNotBeNullOrEmpty(args, "args")[0]; //$NON-NLS-1$
if (args.length > 1) {
ArrayList parameter = new ArrayList(args.length - 1);
for (int i = 1; i < args.length; i++) {
parameter.add(CommandParameter.parse(args[i]));
}
return new CommandCall(commandName, parameter);
} else {
return new CommandCall(commandName);
}
}
public String getName() {
return this.name;
}
public Map getParameters() {
return this.parameters;
}
@Override
@SuppressWarnings("nls")
public String toString() {
StringBuilder t = new StringBuilder();
t.append(this.name);
if (!this.parameters.isEmpty()) {
for (CommandParameter p : this.parameters.values()) {
t.append(" \"").append(p).append("\"");
}
}
return t.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy