Skip to content

Commit

Permalink
Remove most getParameterNames impl in favor or one default impl
Browse files Browse the repository at this point in the history
Thx @teosarca, for the hint
Also simplify the CtxNames.parse method

Lookup validation rule shall support @CtxName/Default@ annotation
metasfresh/metasfresh-webui-api-legacy#585
  • Loading branch information
metas-ts committed Sep 14, 2017
1 parent 0281015 commit f3f3bf2
Show file tree
Hide file tree
Showing 15 changed files with 43 additions and 116 deletions.
Expand Up @@ -93,12 +93,6 @@ public String getExpressionString()
{
return expressionString;
}

@Override
public Set<String> getParameterNames()
{
return ImmutableSet.of();
}

@Override
public Set<CtxName> getParameters()
Expand Down
Expand Up @@ -41,11 +41,15 @@ default String getFormatedExpressionString()

/**
* Gets the list of parameter names.<br>
* In most implementations this should be the result of {@link #getParameters()} that was "dumbed down" via {@link CtxNames#toNames(java.util.Collection)}.
* In most implementations this can be the result of a {@link #getParameters()} that was "dumbed down" via {@link CtxNames#toNames(java.util.Collection)}.<br>
* However, if your implementation wraps around and delegates to an internal {@link IExpression} instance, then I recommend to explicitly delegate also this method.
*
* @return list of parameter names or empty list; never return {@code null}
*/
Set<String> getParameterNames();
default Set<String> getParameterNames()
{
return CtxNames.toNames(getParameters());
}

/**
* Return the parameters as {@link CtxName}s.<br>
Expand Down
Expand Up @@ -23,7 +23,6 @@
*/

import java.util.List;
import java.util.Set;

import org.adempiere.ad.expression.api.IExpressionEvaluator.OnVariableNotFound;
import org.adempiere.ad.expression.api.impl.LogicExpressionBuilder;
Expand Down Expand Up @@ -65,9 +64,6 @@ default Class<Boolean> getValueClass()
@Override
String getFormatedExpressionString();

@Override
Set<String> getParameterNames();

@Override
default Boolean evaluate(final Evaluatee ctx, final boolean ignoreUnparsable)
{
Expand Down
Expand Up @@ -38,12 +38,6 @@ public String getFormatedExpressionString()
return "";
}

@Override
public Set<String> getParameterNames()
{
return ImmutableSet.of();
}

@Override
public Set<CtxName> getParameters()
{
Expand Down
Expand Up @@ -209,12 +209,6 @@ public String getFormatedExpressionString()
return expressionBaseLang.getFormatedExpressionString();
}

@Override
public Set<String> getParameterNames()
{
return CtxNames.toNames(parameters);
}

@Override
public Set<CtxName> getParameters()
{
Expand Down
Expand Up @@ -86,12 +86,10 @@ public static final Builder builder()
private transient String _expressionStr;
private transient String _formatedExpressionString;

private transient Set<String> _parameters;
private transient Set<CtxName> _parametersAsCtxNames;

private Integer _hashCode;


private CompositeStringExpression(final Collection<IStringExpression> expressions)
{
this.expressions = ImmutableList.copyOf(expressions);
Expand Down Expand Up @@ -209,19 +207,6 @@ public IStringExpression resolvePartial(final Evaluatee ctx) throws ExpressionEv
}
}

@Override
public Set<String> getParameterNames()
{
if (_parameters == null)
{
_parameters = expressions.stream()
.flatMap(expression -> expression.getParameterNames().stream())
.collect(GuavaCollectors.toImmutableSet());
}
return _parameters;
}


@Override
public Set<CtxName> getParameters()
{if (_parametersAsCtxNames == null)
Expand Down
Expand Up @@ -117,12 +117,6 @@ public String getFormatedExpressionString()
{
return expressionStr;
}

@Override
public Set<String> getParameterNames()
{
return ImmutableSet.of();
}

@Override
public Set<CtxName> getParameters()
Expand Down
Expand Up @@ -9,7 +9,6 @@
import org.adempiere.ad.expression.json.JsonLogicExpressionSerializer;
import org.adempiere.util.Check;
import org.compiere.util.CtxName;
import org.compiere.util.CtxNames;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.google.common.collect.ImmutableSet;
Expand Down Expand Up @@ -209,12 +208,6 @@ else if (LOGIC_OPERATOR_OR.equals(operator))
}
}

@Override
public Set<String> getParameterNames()
{
return CtxNames.toNames(getParameters());
}

@Override
public Set<CtxName> getParameters()
{
Expand Down
Expand Up @@ -171,12 +171,6 @@ public String getExpressionString()
return expressionStr;
}

@Override
public Set<String> getParameterNames()
{
return CtxNames.toNames(getParameters());
}

@Override
public Set<CtxName> getParameters()
{
Expand Down
Expand Up @@ -48,7 +48,6 @@
private final String expressionStr;
private final CtxName parameter;

private final Set<String> parameters;
private final Set<CtxName> parametersAsCtxName;

/* package */ SingleParameterStringExpression(final String expressionStr, final CtxName parameter)
Expand All @@ -61,7 +60,6 @@
Check.assumeNotNull(parameter, "Parameter parameter is not null");
this.parameter = parameter;

parameters = ImmutableSet.of(parameter.getName()); // NOTE: we need only the parameter name (and not all modifiers)
parametersAsCtxName = ImmutableSet.of(parameter);
}

Expand Down Expand Up @@ -107,13 +105,6 @@ public String getFormatedExpressionString()
{
return parameter.toStringWithMarkers();
}

@Override
public Set<String> getParameterNames()
{
return parameters;
}


@Override
public Set<CtxName> getParameters()
Expand Down
Expand Up @@ -13,7 +13,6 @@
import org.adempiere.ad.expression.exceptions.ExpressionEvaluationException;
import org.adempiere.ad.expression.json.JsonStringExpressionSerializer;
import org.compiere.util.CtxName;
import org.compiere.util.CtxNames;
import org.compiere.util.Evaluatee;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
Expand Down Expand Up @@ -126,12 +125,6 @@ private static final String buildExpressionStringFromChunks(final List<Object> e
return sb.toString();
}

@Override
public Set<String> getParameterNames()
{
return CtxNames.toNames(parametersAsCtxName);
}

@Override
public Set<CtxName> getParameters()
{
Expand Down
Expand Up @@ -170,12 +170,6 @@ public String getFormatedExpressionString()
return "";
}

@Override
public Set<String> getParameterNames()
{
return ImmutableSet.of();
}

@Override
public Set<CtxName> getParameters()
{
Expand Down Expand Up @@ -260,12 +254,6 @@ public String getFormatedExpressionString()
return expressionStr;
}

@Override
public Set<String> getParameterNames()
{
return ImmutableSet.of();
}

@Override
public Set<CtxName> getParameters()
{
Expand Down Expand Up @@ -350,12 +338,6 @@ public String getFormatedExpressionString()
return expressionStr; // expressionStr is good enough in this case
}

@Override
public Set<String> getParameterNames()
{
return CtxNames.toNames(_parametersList);
}

@Override
public Set<CtxName> getParameters()
{
Expand Down
Expand Up @@ -70,12 +70,6 @@ public String getFormatedExpressionString()
return EXPRESSION_STRING;
}

@Override
public Set<String> getParameterNames()
{
return ImmutableSet.of();
}

@Override
public Set<CtxName> getParameters()
{
Expand Down
Expand Up @@ -157,12 +157,6 @@ public String getExpressionString()
return sqlExpression.getExpressionString();
}

@Override
public Set<String> getParameterNames()
{
return parametersAsCtxNames.stream().map(CtxName::getName).collect(ImmutableSet.toImmutableSet());
}

@Override
public Set<CtxName> getParameters()
{
Expand Down
Expand Up @@ -5,6 +5,8 @@
import java.util.List;
import java.util.Set;

import javax.annotation.Nullable;

import org.adempiere.util.Check;

import com.google.common.base.Splitter;
Expand All @@ -24,12 +26,12 @@
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
Expand All @@ -38,7 +40,7 @@

/**
* This class contains the static methods and constants around {@link CtxName}.
*
*
* @author metas-dev <dev@metasfresh.com>
*
*/
Expand All @@ -61,7 +63,7 @@ public class CtxNames

/**
* Returns an immutable set of {@link CtxName}s that contains the results of {@link CtxNames#parse(String)}, applied to the strings of the given {@code stringsWithoutMarkers}.
*
*
* @param stringsWithoutMarkers may not be {@code null}.
* @return
*/
Expand All @@ -74,12 +76,16 @@ public Set<CtxName> parseAll(@NonNull final Collection<String> stringsWithoutMar

/**
* Returns an immutable set of strings which contains the {@link CtxName#getName()}s of the give {@code ctxNames}.
*
* @param ctxNames may not be {@code null}.
*
* @param ctxNames may be {@code null}.
* @return
*/
public Set<String> toNames(@NonNull final Collection<CtxName> ctxNames)
public Set<String> toNames(@Nullable final Collection<CtxName> ctxNames)
{
if (ctxNames == null)
{
return null;
}
return ctxNames.stream()
.map(CtxName::getName)
.collect(ImmutableSet.toImmutableSet());
Expand All @@ -92,11 +98,27 @@ public static CtxName parse(final String contextWithoutMarkers)
return null;
}

String name = null;

final List<String> modifiers = new ArrayList<>();
final String name = extractNameAndModifiers(contextWithoutMarkers, modifiers);

final String defaultValue = extractDefaultValue(modifiers);

return new CtxName(name, modifiers, defaultValue);
}

/**
*
* @param contextWithoutMarkers
* @param modifiers found modifiers are added to this list
* @return
*/
private static String extractNameAndModifiers(
@NonNull final String contextWithoutMarkers,
@NonNull final List<String> modifiers)
{
String name = null;
boolean firstToken = true;
for (String token : SEPARATOR_SPLITTER.splitToList(contextWithoutMarkers))
for (final String token : SEPARATOR_SPLITTER.splitToList(contextWithoutMarkers))
{
if (firstToken)
{
Expand All @@ -109,7 +131,11 @@ public static CtxName parse(final String contextWithoutMarkers)

firstToken = false;
}
return name;
}

static String extractDefaultValue(final List<String> modifiers)
{
final String defaultValue;
if (!modifiers.isEmpty() && !isModifier(modifiers.get(modifiers.size() - 1)))
{
Expand All @@ -119,8 +145,7 @@ public static CtxName parse(final String contextWithoutMarkers)
{
defaultValue = VALUE_NULL;
}

return new CtxName(name, modifiers, defaultValue);
return defaultValue;
}

/** Parse a given name, surrounded by {@value #NAME_Marker} */
Expand Down

0 comments on commit f3f3bf2

Please sign in to comment.