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

io.featurehub.strategies.matchers.NumberArrayMatcher Maven / Gradle / Ivy

There is a newer version: 3.3
Show newest version
package io.featurehub.strategies.matchers;

import io.featurehub.sse.model.FeatureRolloutStrategyAttribute;
import org.jetbrains.annotations.Nullable;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.Objects;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public class NumberArrayMatcher implements StrategyMatcher {
  private BigDecimal supplied = null;

  @Override
  public boolean match(String suppliedValue, FeatureRolloutStrategyAttribute attr) {
    try {
      Supplier bd = () -> {
        if (supplied == null) {
          supplied = new BigDecimal(suppliedValue);
        }
        return supplied;
      };

      Supplier> vals = () -> attr.getValues().stream()
        .map(NumberArrayMatcher::toBigDecimal).filter(Objects::nonNull).collect(Collectors.toList());

      switch (attr.getConditional()) {
        case EQUALS:
        case INCLUDES:
          return vals.get().stream().anyMatch(v -> v.equals(bd.get()));
        case ENDS_WITH:
          return attr.getValues().stream().anyMatch(v -> suppliedValue.endsWith(v.toString()));
        case STARTS_WITH:
          return attr.getValues().stream().anyMatch(v -> suppliedValue.startsWith(v.toString()));
        case GREATER:
          return vals.get().stream().anyMatch(v -> bd.get().compareTo(v) > 0 );
        case GREATER_EQUALS:
          return vals.get().stream().anyMatch(v -> bd.get().compareTo(v) >= 0 );
        case LESS:
          return vals.get().stream().anyMatch(v -> bd.get().compareTo(v) < 0 );
        case LESS_EQUALS:
          return vals.get().stream().anyMatch(v -> bd.get().compareTo(v) <= 0 );
        case NOT_EQUALS:
        case EXCLUDES:
          return vals.get().stream().noneMatch(v -> v.equals(bd.get()));
        case REGEX:
          return attr.getValues().stream().anyMatch(v -> v.toString().matches(suppliedValue));
      }
    } catch (Exception ignored) {

    }

    return false;
  }

  @Nullable
  private static BigDecimal toBigDecimal(Object v) {
    if (v instanceof BigDecimal) {
      return (BigDecimal) v;
    }
    if (v instanceof Integer) {
      return new BigDecimal((Integer) v);
    }
    if (v instanceof BigInteger) {
      return new BigDecimal((BigInteger) v);
    }
    if (v instanceof Double) {
      return BigDecimal.valueOf((Double) v);
    }
    return null;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy