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

com.launchdarkly.client.SegmentRule Maven / Gradle / Ivy

There is a newer version: 4.61
Show newest version
package com.launchdarkly.client;

import java.util.List;

public class SegmentRule {
  private final List clauses;
  private final Integer weight;
  private final String bucketBy;
  
  public SegmentRule(List clauses, Integer weight, String bucketBy) {
    this.clauses = clauses;
    this.weight = weight;
    this.bucketBy = bucketBy;
  }

  public boolean matchUser(LDUser user, String segmentKey, String salt) {
    for (Clause c: clauses) {
      if (!c.matchesUserNoSegments(user)) {
        return false;
      }
    }
    
    // If the Weight is absent, this rule matches
    if (weight == null) {
      return true;
    }
    
    // All of the clauses are met. See if the user buckets in
    String by = (bucketBy == null) ? "key" : bucketBy;
    double bucket = VariationOrRollout.bucketUser(user, segmentKey, by, salt);
    double weight = (double)this.weight / 100000.0;
    return bucket < weight;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy