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

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

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

import io.hyperfoil.api.session.Access;
import io.hyperfoil.api.session.Session;
import io.hyperfoil.core.session.IntVar;
import io.hyperfoil.core.session.ObjectVar;
import io.hyperfoil.core.session.SessionFactory;
import io.hyperfoil.function.SerializableIntPredicate;
import io.hyperfoil.function.SerializablePredicate;

public class IntCondition implements SerializablePredicate {
   private final Access var;
   private final SerializableIntPredicate predicate;

   public IntCondition(String var, SerializableIntPredicate predicate) {
      this.var = SessionFactory.access(var);
      this.predicate = predicate;
   }

   @Override
   public boolean test(Session session) {
      Session.Var var = this.var.getVar(session);
      if (!var.isSet()) {
         return false;
      }
      int value;
      if (var instanceof IntVar) {
         value = var.intValue();
      } else if (var instanceof ObjectVar) {
         value = Integer.parseInt(var.objectValue().toString());
      } else {
         throw new IllegalStateException("Unknown type of var: " + var);
      }
      return predicate.test(value);
   }

   public static class Builder

extends BaseBuilder> implements Condition.Builder { private final P parent; private String var; public Builder(P parent) { this.parent = parent; } public Builder

var(String var) { this.var = var; return this; } public P endCondition() { return parent; } @Override public IntCondition build() { return new IntCondition(var, buildPredicate()); } } public abstract static class BaseBuilder> { protected Integer equalTo; protected Integer notEqualTo; protected Integer greaterThan; protected Integer greaterOrEqualTo; protected Integer lessThan; protected Integer lessOrEqualTo; protected static SerializableIntPredicate and(SerializableIntPredicate p1, SerializableIntPredicate p2) { return p1 == null ? p2 : (p2 == null ? null : x -> p1.test(x) && p2.test(x)); } private B self() { return (B) this; } public B equalTo(int equalTo) { this.equalTo = equalTo; return self(); } public B notEqualTo(int notEqualTo) { this.notEqualTo = notEqualTo; return self(); } public B greaterThan(int greaterThan) { this.greaterThan = greaterThan; return self(); } public B greaterOrEqualTo(int greaterOrEqualTo) { this.greaterOrEqualTo = greaterOrEqualTo; return self(); } public B lessThan(int lessThan) { this.lessThan = lessThan; return self(); } public B lessOrEqualTo(int lessOrEqualTo) { this.lessOrEqualTo = lessOrEqualTo; return self(); } protected SerializableIntPredicate buildPredicate() { SerializableIntPredicate predicate = null; if (equalTo != null) { int val = equalTo; predicate = v -> v == val; } if (notEqualTo != null) { int val = notEqualTo; predicate = and(predicate, v -> v != val); } if (greaterThan != null) { int val = greaterThan; predicate = and(predicate, v -> v > val); } if (greaterOrEqualTo != null) { int val = greaterOrEqualTo; predicate = and(predicate, v -> v >= val); } if (lessThan != null) { int val = lessThan; predicate = and(predicate, v -> v < val); } if (lessOrEqualTo != null) { int val = lessOrEqualTo; predicate = and(predicate, v -> v <= val); } return predicate; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy