Skip to content

Commit

Permalink
Clean up public API and parsing logic
Browse files Browse the repository at this point in the history
Clean up the public API by removing the deprecated methods that were
only intended to be called during the parsing of the arguments.

At the same time, migrate all the parsing logic into a dedicated class,
rather than performing this in the constructor of the objects.
  • Loading branch information
swarren12 committed Nov 19, 2021
1 parent 6b8197d commit eee1959
Show file tree
Hide file tree
Showing 22 changed files with 1,452 additions and 1,340 deletions.
24 changes: 5 additions & 19 deletions src/main/java/com/lmax/simpledsl/api/DslArg.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.lmax.simpledsl.api;

import java.util.function.Function;

/**
* The base class for all arg types.
*/
Expand All @@ -38,14 +36,13 @@ public interface DslArg
boolean isRequired();

/**
* Determine if a value is optional for this argument.
* Get a default value for this argument.
*
* If the argument is required, this method will throw an {@link IllegalArgumentException}.
*
* @return {@literal true} if and only if this argument is optional.
* @return the default value for the argument
*/
default boolean isOptional()
{
return !isRequired();
}
String getDefaultValue();

/**
* Check whether this argument can take multiple values.
Expand All @@ -67,15 +64,4 @@ default boolean isOptional()
* @return the values allowed by this argument, or {@literal null} if all values are allowed
*/
String[] getAllowedValues();

/**
* Transform this {@link DslArg} by applying an appropriate {@link Function}.
*
* @param ifRequired the {@link Function} to apply if this {@link DslArg} is compatible with a {@link RequiredArg}.
* @param ifOptional the {@link Function} to apply if this {@link DslArg} is compatible with an {@link OptionalArg}.
* @param ifGroup the {@link Function} to apply if this {@link DslArg} is compatible with a {@link RepeatingArgGroup}.
* @param <T> the return type.
* @return the transformed object.
*/
<T> T fold(Function<RequiredArg, T> ifRequired, Function<OptionalArg, T> ifOptional, Function<RepeatingArgGroup, T> ifGroup);
}
43 changes: 17 additions & 26 deletions src/main/java/com/lmax/simpledsl/api/DslParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.lmax.simpledsl.api;

import com.lmax.simpledsl.internal.DslParamsImpl;
import com.lmax.simpledsl.internal.DslParamsParser;

/**
* The main entry point for defining the DSL language. Create a DslParams instance with the supplied arguments and the supported params.
Expand Down Expand Up @@ -53,40 +53,31 @@ public interface DslParams extends DslValues
RepeatingGroup[] valuesAsGroup(String groupName);

/**
* A shorthand way to create a {@link DslParams} instance that accepts a single required parameter and return the
* value that was supplied for that parameter.
* Create new {@link DslParams}.
*
* @param args the arguments supplied by the test.
* @param requiredParamName the name of the required parameter.
* @return the value supplied for the parameter.
* @param args the values
* @param arguments the {@link DslArg args}
* @return the new {@link DslParams}
*/
static String getSingleRequiredParamValue(final String[] args, final String requiredParamName)
static DslParams create(String[] args, DslArg... arguments)
{
return create(args, new RequiredArg(requiredParamName)).value(requiredParamName);
return new DslParamsParser().parse(args, arguments);
}

/**
* Utility method for defining a DSL method that doesn't accept any arguments.
*
* This is an alternative to removing the {@code String... args} parameter entirely when a consistent public API is desired.
*
* @param args the parameters provided.
*/
static void checkEmpty(final String[] args)
{
new DslParamsImpl(args);
}

/**
* Create a new DslParams to define the supported dsl language.
* A shorthand way to create a {@link DslParams} instance that accepts a single required parameter and return the
* value that was supplied for that parameter.
*
* @param args the arguments supplied by the test.
* @param params the supported parameters.
* @return the parsed {@link DslParams}
* @throws IllegalArgumentException if an invalid parameter is specified
* @param args the arguments supplied by the test.
* @param requiredParamName the name of the required parameter.
* @return the value supplied for the parameter.
*/
static DslParams create(final String[] args, final DslArg... params)
static String getSingleRequiredParamValue(final String[] args, final String requiredParamName)
{
return new DslParamsImpl(args, params);
return new DslParamsParser()
.parse(args, new RequiredArg(requiredParamName))
.value(requiredParamName);
}

}
7 changes: 7 additions & 0 deletions src/main/java/com/lmax/simpledsl/api/DslValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,11 @@ default double[] valuesAsDoubles(final String name)
}
return parsedValues;
}

/**
* Get the supported parameters. Supplied values will have been parsed into the parameters.
*
* @return the array of {@link DslArg DslArgs} supplied to the constructor.
*/
DslArg[] getParams();
}
37 changes: 1 addition & 36 deletions src/main/java/com/lmax/simpledsl/api/OptionalArg.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.lmax.simpledsl.api;

import java.util.function.Function;

/**
* An optional argument.
* <p>
Expand Down Expand Up @@ -45,43 +43,10 @@
*
* @see DslValues#valueAsOptional(String)
*/
public class OptionalArg extends SimpleDslArg<OptionalArg>
public class OptionalArg extends SimpleDslArg
{
private String defaultValue;

public OptionalArg(final String name)
{
super(name, false);
}

/**
* Get a default value for this argument.
*
* @return the default value for the argument
*/
public String getDefaultValue()
{
return defaultValue;
}

/**
* Set a default value for this argument.
* <p>
* If a default is provided, the argument will be considered to always have a value, and will return the default if
* no other value is provided by the caller.
*
* @param defaultValue the default value for the argument.
* @return this argument
*/
public OptionalArg setDefault(final String defaultValue)
{
this.defaultValue = defaultValue;
return this;
}

@Override
public <T> T fold(final Function<RequiredArg, T> ifRequired, final Function<OptionalArg, T> ifOptional, final Function<RepeatingArgGroup, T> ifGroup)
{
return ifOptional.apply(this);
}
}
20 changes: 9 additions & 11 deletions src/main/java/com/lmax/simpledsl/api/RepeatingArgGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.lmax.simpledsl.api;

import java.util.function.Function;

/**
* Define a group of arguments that can be repeated 0 or more times.
* <p>
Expand Down Expand Up @@ -45,9 +43,9 @@
public class RepeatingArgGroup implements DslArg
{
private final RequiredArg identity;
private final DslArg[] otherArgs;
private final SimpleDslArg[] otherArgs;

public RepeatingArgGroup(final RequiredArg firstArg, final SimpleDslArg<?>... otherArgs)
public RepeatingArgGroup(final RequiredArg firstArg, final SimpleDslArg... otherArgs)
{
this.identity = firstArg;
this.otherArgs = otherArgs;
Expand All @@ -65,6 +63,12 @@ public boolean isRequired()
return false;
}

@Override
public String getDefaultValue()
{
throw new IllegalArgumentException("A repeating group can not have a default value");
}

@Override
public boolean isAllowMultipleValues()
{
Expand Down Expand Up @@ -99,14 +103,8 @@ public RequiredArg getIdentity()
*
* @return the {@link DslArg DslArgs}.
*/
public DslArg[] getOtherArgs()
public SimpleDslArg[] getOtherArgs()
{
return otherArgs;
}

@Override
public <T> T fold(final Function<RequiredArg, T> ifRequired, final Function<OptionalArg, T> ifOptional, final Function<RepeatingArgGroup, T> ifGroup)
{
return ifGroup.apply(this);
}
}
29 changes: 24 additions & 5 deletions src/main/java/com/lmax/simpledsl/api/RequiredArg.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.lmax.simpledsl.api;

import java.util.function.Function;

/**
* A required argument.
* <p>
Expand Down Expand Up @@ -50,16 +48,37 @@
* By default, only a single value is allowed.
* Multiple values can be allowed by calling {@link #setAllowMultipleValues()}.
*/
public class RequiredArg extends SimpleDslArg<RequiredArg>
public class RequiredArg extends SimpleDslArg
{
public RequiredArg(final String name)
{
super(name, true);
}

@Override
public <T> T fold(final Function<RequiredArg, T> ifRequired, final Function<OptionalArg, T> ifOptional, final Function<RepeatingArgGroup, T> ifGroup)
public RequiredArg setDefault(final String defaultValue)
{
throw new IllegalArgumentException("A required argument can not have a default value");
}

@Override
public RequiredArg setAllowedValues(final String... allowedValues)
{
super.setAllowedValues(allowedValues);
return this;
}

@Override
public RequiredArg setAllowMultipleValues()
{
super.setAllowMultipleValues();
return this;
}

@Override
public RequiredArg setAllowMultipleValues(final String delimiter)
{
return ifRequired.apply(this);
super.setAllowMultipleValues(delimiter);
return this;
}
}
48 changes: 34 additions & 14 deletions src/main/java/com/lmax/simpledsl/api/SimpleDslArg.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@

/**
* The root type for all simple args.
*
* @param <P> the actual type of simple argument.
*/
public abstract class SimpleDslArg<P extends SimpleDslArg<P>> implements DslArg
public abstract class SimpleDslArg implements DslArg
{
private static final String DEFAULT_DELIMITER = ",";

private final String name;
private final boolean required;

protected String defaultValue;
protected boolean allowMultipleValues;
protected String multipleValueSeparator;
protected String[] allowedValues;
Expand All @@ -50,6 +49,17 @@ public boolean isRequired()
return required;
}

@Override
public String getDefaultValue()
{
if (required)
{
throw new IllegalArgumentException("A required argument can not have a default value");
}

return defaultValue;
}

@Override
public boolean isAllowMultipleValues()
{
Expand All @@ -68,17 +78,33 @@ public String[] getAllowedValues()
return allowedValues;
}

/**
* Set a default value for this argument.
* <p>
* If a default is provided, the argument will be considered to always have a value, and will return the default if
* no other value is provided by the caller.
*
* @param defaultValue the default value for the argument.
* @return this argument
* @throws IllegalArgumentException if the default value cannot be set
*/
public SimpleDslArg setDefault(final String defaultValue)
{
this.defaultValue = defaultValue;
return this;
}

/**
* Restrict the allowed values for this argument to the specified set.
* Specifying a value outside of this set will result in an exception being thrown when parsing the arguments.
*
* @param allowedValues the allowable values for this argument.
* @return this argument
*/
public P setAllowedValues(final String... allowedValues)
public SimpleDslArg setAllowedValues(final String... allowedValues)
{
this.allowedValues = allowedValues;
return me();
return this;
}

/**
Expand All @@ -93,7 +119,7 @@ public P setAllowedValues(final String... allowedValues)
* @return this argument
* @see #setAllowMultipleValues(String)
*/
public P setAllowMultipleValues()
public SimpleDslArg setAllowMultipleValues()
{
return setAllowMultipleValues(DEFAULT_DELIMITER);
}
Expand All @@ -105,16 +131,10 @@ public P setAllowMultipleValues()
* @return this argument
* @see #setAllowMultipleValues()
*/
public P setAllowMultipleValues(final String delimiter)
public SimpleDslArg setAllowMultipleValues(final String delimiter)
{
allowMultipleValues = true;
multipleValueSeparator = delimiter;
return me();
}

@SuppressWarnings("unchecked")
protected P me()
{
return (P) this;
return this;
}
}
Loading

0 comments on commit eee1959

Please sign in to comment.