Skip to content

Commit

Permalink
Merge pull request #27 from RyanHealey/feature/has-param-method
Browse files Browse the repository at this point in the history
add hasParam api that determines if a parameter has been defined
  • Loading branch information
swarren12 authored Jan 20, 2023
2 parents b576494 + 95481f3 commit e81843d
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/main/java/com/lmax/simpledsl/api/DslValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ public interface DslValues
*/
boolean hasValue(String name);

/**
* Determine if a parameter is defined.
* <p>
* Returns true when the parameter is defined.
*
* @param name the name of the parameter.
* @return true if the parameter is defined, otherwise false.
*/
boolean hasParam(String name);

/**
* Retrieve the value supplied for a parameter.
* <p>
Expand All @@ -71,6 +81,19 @@ public interface DslValues
*/
String[] values(String name);

/**
* Determine if a parameter is defined and has a value.
* <p>
* Returns true when the parameter is defined and supplied with an empty value.
*
* @param name the name of the parameter.
* @return true if the parameter is defined and has a value, otherwise false.
*/
default boolean hasParamAndValue(String name)
{
return hasParam(name) && hasValue(name);
}

/**
* Retrieve the value supplied for a parameter, mapping it using the specified {@link Function function}.
*
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/lmax/simpledsl/internal/DslParamsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ public boolean hasValue(final String name)
.orElse(false);
}

@Override
public boolean hasParam(final String name)
{
return findDslParam(name).isPresent();
}

@Override
public DslArg[] getParams()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.util.List;
import java.util.Map;

import static java.util.Arrays.stream;

class RepeatingParamValues implements RepeatingGroup
{
private final DslArg[] dslArgs;
Expand All @@ -23,6 +25,12 @@ public boolean hasValue(final String name)
return valuesByName.containsKey(name.toLowerCase());
}

@Override
public boolean hasParam(final String name)
{
return stream(dslArgs).anyMatch(arg -> arg.getName().equalsIgnoreCase(name));
}

@Override
public String value(final String name)
{
Expand Down
70 changes: 70 additions & 0 deletions src/test/java/com/lmax/simpledsl/internal/DslParamsImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonList;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -510,6 +511,75 @@ public void shouldReturnOptionalListWhenMultipleParameterValueIsSupplied()
assertEquals(Optional.of(asList("value1", "value2")), params.valuesAsOptional("a"));
}

@Test
public void shouldReturnTrueIfAParamHasBeenDefined()
{
final SimpleDslParam aParam = new SimpleDslParam("a", asList("value1", "value2"));

final DslParams params = new DslParamsImpl(new DslArg[0], Collections.singletonMap("a", aParam));

assertTrue(params.hasParam("a"));
}

@Test
public void shouldReturnFalseIfAParamHasNotBeenDefined()
{
final SimpleDslParam aParam = new SimpleDslParam("a", asList("value1", "value2"));

final DslParams params = new DslParamsImpl(new DslArg[0], Collections.singletonMap("a", aParam));

assertFalse(params.hasParam("b"));
}

@Test
public void shouldReturnIfAParamHasBeenDefinedCaseInsensitively()
{
final SimpleDslParam aParam = new SimpleDslParam("a", asList("value1", "value2"));

final DslParams params = new DslParamsImpl(new DslArg[0], Collections.singletonMap("a", aParam));

assertTrue(params.hasParam("A"));
}

@Test
public void shouldReturnTrueIfAParamHasBeenDefinedAndHasAValue()
{
final SimpleDslParam aParam = new SimpleDslParam("a", asList("value1", "value2"));

final DslParams params = new DslParamsImpl(new DslArg[0], Collections.singletonMap("a", aParam));

assertTrue(params.hasParamAndValue("a"));
}

@Test
public void shouldReturnFalseIfAParamHasBeenDefinedButDoesNotHaveAValue()
{
final SimpleDslParam aParam = new SimpleDslParam("a", emptyList());

final DslParams params = new DslParamsImpl(new DslArg[0], Collections.singletonMap("a", aParam));

assertFalse(params.hasParamAndValue("a"));
}

@Test
public void shouldReturnFalseIfAParamHasNotBeenDefinedButDoesNotHaveAValue()
{

final DslParams params = new DslParamsImpl(new DslArg[0], emptyMap());

assertFalse(params.hasParamAndValue("a"));
}

@Test
public void shouldReturnIfAParamHasBeenDefinedHasAValueCaseInsensitively()
{
final SimpleDslParam aParam = new SimpleDslParam("a", asList("value1", "value2"));

final DslParams params = new DslParamsImpl(new DslArg[0], Collections.singletonMap("a", aParam));

assertTrue(params.hasParamAndValue("A"));
}

private enum TestValues
{
VALUE_1,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

/*
* Copyright 2011 LMAX Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.lmax.simpledsl.internal;

import com.lmax.simpledsl.api.DslArg;
import com.lmax.simpledsl.api.OptionalArg;
import com.lmax.simpledsl.api.RequiredArg;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.util.Arrays.asList;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class RepeatingParamValuesTest
{
@Test
public void shouldReturnIfAParamHasBeenDefined()
{
final RequiredArg requiredArg = new RequiredArg("foo");
final OptionalArg otherArg = new OptionalArg("bar");
final Map<String, List<String>> values = new HashMap<>();
values.put("foo", Collections.singletonList("abc"));
values.put("bar", Collections.singletonList("123"));
final RepeatingParamValues params = new RepeatingParamValues(asList(requiredArg, otherArg).toArray(new DslArg[0]), values);

assertTrue(params.hasParam("foo"));
assertTrue(params.hasParam("bar"));
assertFalse(params.hasParam("boo"));
assertFalse(params.hasParam("far"));
}

@Test
public void shouldReturnIfAParamHasBeenDefinedCaseInsensitively()
{
final RequiredArg requiredArg = new RequiredArg("foo");
final OptionalArg otherArg = new OptionalArg("bar");
final Map<String, List<String>> values = new HashMap<>();
values.put("foo", Collections.singletonList("abc"));
values.put("bar", Collections.singletonList("123"));
final RepeatingParamValues params = new RepeatingParamValues(asList(requiredArg, otherArg).toArray(new DslArg[0]), values);

assertTrue(params.hasParam("FOO"));
assertTrue(params.hasParam("BaR"));
}
}

0 comments on commit e81843d

Please sign in to comment.