// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition.document;

import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;

/**
 * Encapsulates values required for predicate fields.
 *
 * @author lesters
 */
public final class BooleanIndexDefinition {

    public static final int DEFAULT_ARITY = 8;
    public static final long DEFAULT_UPPER_BOUND = Long.MAX_VALUE;
    public static final long DEFAULT_LOWER_BOUND = Long.MIN_VALUE;
    public static final double DEFAULT_DENSE_POSTING_LIST_THRESHOLD = 0.4;

    private final OptionalInt arity;   // mandatory field value
    private final OptionalLong lowerBound;
    private final OptionalLong upperBound;
    private final OptionalDouble densePostingListThreshold;

    public BooleanIndexDefinition(OptionalInt arity, OptionalLong lowerBound,
                                  OptionalLong upperBound, OptionalDouble densePostingListThreshold) {
        this.arity = arity;
        this.lowerBound = lowerBound;
        this.upperBound = upperBound;
        this.densePostingListThreshold = densePostingListThreshold;
    }

    public int getArity() {
        return arity.getAsInt();
    }

    public boolean hasArity() {
        return arity.isPresent();
    }

    public long getLowerBound() {
        return lowerBound.orElse(DEFAULT_LOWER_BOUND);
    }

    public boolean hasLowerBound() {
        return lowerBound.isPresent();
    }

    public long getUpperBound() {
        return upperBound.orElse(DEFAULT_UPPER_BOUND);
    }

    public boolean hasUpperBound() {
        return upperBound.isPresent();
    }

    public double getDensePostingListThreshold() {
        return densePostingListThreshold.orElse(DEFAULT_DENSE_POSTING_LIST_THRESHOLD);
    }

    public boolean hasDensePostingListThreshold() {
        return densePostingListThreshold.isPresent();
    }

    @Override
    public String toString() {
        return "BooleanIndexDefinition [arity=" + arity + ", lowerBound="
                + lowerBound + ", upperBound=" + upperBound + ", densePostingListThreshold="
                + densePostingListThreshold + "]";
    }

}

@@ -144,16 +144,43 @@
 ument;%0A%0A
+import java.util.Optional;%0A
 import j
@@ -863,16 +863,755 @@
 shold;%0A%0A
+    public BooleanIndexDefinition(Optional%3CInteger%3E arity,%0A                                  Optional%3CLong%3E lowerBound,%0A                                  Optional%3CLong%3E upperBound,%0A                                  Optional%3CDouble%3E densePLT)%0A    %7B%0A        this.arity                     = arity.isPresent() ? OptionalInt.of(arity.get()) : OptionalInt.empty();%0A        this.lowerBound                = lowerBound.isPresent() ? OptionalLong.of(lowerBound.get()) : OptionalLong.empty();%0A        this.upperBound                = upperBound.isPresent() ? OptionalLong.of(upperBound.get()) : OptionalLong.empty();%0A        this.densePostingListThreshold = densePLT.isPresent() ? OptionalDouble.of(densePLT.get()) : OptionalDouble.empty();%0A    %7D%0A%0A
     publ

allow constructing from Optional objects