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

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

package io.featurehub.strategies.matchers;

import io.featurehub.sse.model.FeatureRolloutStrategyAttribute;

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import io.featurehub.client.ObjectSupplier;

public class DateTimeArrayMatcher implements StrategyMatcher {
  private OffsetDateTime supplied = null;

  @Override
  public boolean match(String suppliedValue, FeatureRolloutStrategyAttribute attr) {
    DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;

    try {
      ObjectSupplier suppliedDate = () -> {
        if (supplied == null) {
          supplied = OffsetDateTime.from(formatter.parse(suppliedValue));
        }
        return supplied;
      };

      switch (attr.getConditional()) {
        case EQUALS: // all match makes no sense
        case INCLUDES: // same as equals
          return attr.getValues().stream()
            .anyMatch(v -> suppliedDate.get().equals(OffsetDateTime.from(formatter.parse(v.toString()))));
        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 attr.getValues().stream()
            .anyMatch(v -> suppliedDate.get().compareTo(OffsetDateTime.from(formatter.parse(v.toString()))) > 0);
        case GREATER_EQUALS:
          return attr.getValues().stream()
            .anyMatch(v -> suppliedDate.get().compareTo(OffsetDateTime.from(formatter.parse(v.toString()))) >= 0);
        case LESS:
          return attr.getValues().stream()
            .anyMatch(v -> suppliedDate.get().compareTo(OffsetDateTime.from(formatter.parse(v.toString()))) < 0);
        case LESS_EQUALS:
          return attr.getValues().stream()
            .anyMatch(v -> suppliedDate.get().compareTo(OffsetDateTime.from(formatter.parse(v.toString()))) <= 0);
        case NOT_EQUALS:
        case EXCLUDES: // same as not_equals
          return attr.getValues().stream()
            .noneMatch(v -> suppliedDate.get().equals(OffsetDateTime.from(formatter.parse(v.toString()))));
        case REGEX:
          return attr.getValues().stream().anyMatch(v -> suppliedValue.matches(v.toString()));
      }
    } catch (Exception ignored) {
    }

    return false;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy