All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.hyperfoil.core.builders.CharSequenceCondition Maven / Gradle / Ivy

There is a newer version: 0.27.1
Show newest version
package io.hyperfoil.core.builders;

import io.hyperfoil.api.config.BenchmarkDefinitionException;
import io.hyperfoil.api.config.InitFromParam;
import io.hyperfoil.api.config.Locator;
import io.hyperfoil.api.session.Access;
import io.hyperfoil.api.session.Session;
import io.hyperfoil.core.session.SessionFactory;
import io.hyperfoil.core.util.Util;
import io.hyperfoil.function.SerializableBiPredicate;

public interface CharSequenceCondition extends SerializableBiPredicate {
   class Builder

implements InitFromParam> { private final P parent; private CharSequence value; private boolean caseSensitive = true; private String matchVar; private CompareMode compareMode; public Builder(P parent) { this.parent = parent; } public CharSequenceCondition build() { if (value == null && matchVar == null) { throw new BenchmarkDefinitionException("Must set one of: 'value', 'startsWith', 'endsWith' or 'matchVar'!"); } if (caseSensitive) { if (value != null) { CharSequence myValue = value; return (s, string) -> { int offset = 0, length = myValue.length(); switch (compareMode) { case FULL: length = Math.max(string.length(), length); break; case PREFIX: break; case SUFFIX: offset = string.length() - length; break; default: throw new IllegalStateException("Unexpected value: " + compareMode); } return Util.regionMatches(myValue, 0, string, offset, length); }; } else { Access access = SessionFactory.access(matchVar); return (s, string) -> { Object value = access.getObject(s); if (value instanceof CharSequence) { CharSequence v = (CharSequence) value; return Util.regionMatches(v, 0, string, 0, Math.max(v.length(), string.length())); } return false; }; } } else { if (value != null) { String myValue = value.toString(); return (s, string) -> { int offset = 0, length = myValue.length(); switch (compareMode) { case FULL: length = Math.max(string.length(), length); break; case PREFIX: break; case SUFFIX: offset = string.length() - length; break; default: throw new IllegalStateException("Unexpected value: " + compareMode); } return Util.regionMatchesIgnoreCase(myValue, 0, string, offset, length); }; } else { Access access = SessionFactory.access(matchVar); return (s, string) -> { Object value = access.getObject(s); if (value instanceof CharSequence) { CharSequence v = (CharSequence) value; return Util.regionMatchesIgnoreCase(v, 0, string, 0, Math.max(v.length(), string.length())); } return false; }; } } } // While this method does not require the locator, adding it prevents docs generator from recognizing // this as regular builder fluent setter method. public Builder

copy(Locator locator) { return new Builder<>(parent).value(value).caseSensitive(caseSensitive).matchVar(matchVar); } /** * @param param String that should be matched. * @return Self. */ @Override public Builder

init(String param) { this.value = param; return this; } /** * True if the case must match, false if the check is case-insensitive. * * @param caseSensitive Boolean value. * @return Self. */ public Builder

caseSensitive(boolean caseSensitive) { this.caseSensitive = caseSensitive; return this; } private void ensureNotSet() { if (value != null || matchVar != null) { throw new BenchmarkDefinitionException("Must set only one of: 'value', 'startsWith', 'endsWith' or 'matchVar'!"); } } /** * Literal value the condition should match. * * @param value String. * @return Self. */ public Builder

value(CharSequence value) { ensureNotSet(); this.value = value; this.compareMode = CompareMode.FULL; return this; } /** * Prefix for the string. * * @param value String. * @return Self. */ public Builder

startsWith(CharSequence value) { this.value = value; this.compareMode = CompareMode.PREFIX; return this; } /** * Suffix for the string. * * @param value String. * @return Self. */ public Builder

endsWith(CharSequence value) { this.value = value; this.compareMode = CompareMode.SUFFIX; return this; } /** * Fetch the value from a variable. * * @param var Variable name. * @return Self. */ public Builder

matchVar(String var) { this.matchVar = var; return this; } public P end() { return parent; } } enum CompareMode { FULL, PREFIX, SUFFIX } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy