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

com.microsoft.azure.documentdb.internal.routing.Range Maven / Gradle / Ivy

package com.microsoft.azure.documentdb.internal.routing;

import java.util.Comparator;

import org.json.JSONObject;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.microsoft.azure.documentdb.JsonSerializable;

@JsonIgnoreProperties({"empty", "singleValue", "hashMap"})
public final class Range> extends JsonSerializable {
    private static final String MIN_PROPERTY = "min";
    private static final String MAX_PROPERTY = "max";
    private static final String IS_MIN_INCLUSIVE_PROPERTY = "isMinInclusive";
    private static final String IS_MAX_INCLUSIVE_PROPERTY = "isMaxInclusive";

    private T minValue;
    private T maxValue;

    public Range() {
        super();
    }

    public Range(String jsonString) {
        super(jsonString);
    }

    public Range(JSONObject jsonObject) {
        super(jsonObject);
    }

    public Range(T min, T max, boolean isMinInclusive, boolean isMaxInclusive) {
        this.setMin(min);
        this.setMax(max);
        this.setMinInclusive(isMinInclusive);
        this.setMaxInclusive(isMaxInclusive);
    }

    public static > Range getPointRange(T value) {
        return new Range(value, value, true, true);
    }

    public static > Range getEmptyRange(T value) {
        return new Range(value, value, true, false);
    }

    public static > boolean checkOverlapping(Range range1, Range range2) {
        if (range1 == null || range2 == null || range1.isEmpty() || range2.isEmpty()) {
            return false;
        }

        int cmp1 = range1.getMin().compareTo(range2.getMax());
        int cmp2 = range2.getMin().compareTo(range1.getMax());

        if (cmp1 <= 0 && cmp2 <= 0) {
            return !((cmp1 == 0 && !(range1.isMinInclusive() && range2.isMaxInclusive()))
                    || (cmp2 == 0 && !(range2.isMinInclusive() && range1.isMaxInclusive())));
        }

        return false;
    }

    @SuppressWarnings("unchecked")
    public T getMin() {
        if (this.minValue == null) {
            this.minValue = (T) super.get(Range.MIN_PROPERTY);
        }
        return this.minValue;
    }

    public void setMin(T min) {
        this.minValue = min;
    }

    @SuppressWarnings("unchecked")
    public T getMax() {
        if (this.maxValue == null) {
            this.maxValue = (T) super.get(Range.MAX_PROPERTY);
        }
        return this.maxValue;
    }

    public void setMax(T max) {
        this.maxValue = max;
    }

    @JsonProperty("isMinInclusive")
    public boolean isMinInclusive() {
        return super.getBoolean(Range.IS_MIN_INCLUSIVE_PROPERTY);
    }

    public void setMinInclusive(boolean isMinInclusive) {
        super.set(Range.IS_MIN_INCLUSIVE_PROPERTY, isMinInclusive);
    }

    @JsonProperty("isMaxInclusive")
    public boolean isMaxInclusive() {
        return super.getBoolean(Range.IS_MAX_INCLUSIVE_PROPERTY);
    }

    public void setMaxInclusive(boolean isMaxInclusive) {
        super.set(Range.IS_MAX_INCLUSIVE_PROPERTY, isMaxInclusive);
    }

    public boolean isSingleValue() {
        return this.isMinInclusive() && this.isMaxInclusive() && this.getMin().compareTo(this.getMax()) == 0;
    }

    public boolean isEmpty() {
        return this.getMin().compareTo(this.getMax()) == 0 && !(this.isMinInclusive() && this.isMaxInclusive());
    }

    public boolean contains(T value) {
        int minToValueRelation = this.getMin().compareTo(value);
        int maxToValueRelation = this.getMax().compareTo(value);

        return ((this.isMinInclusive() && minToValueRelation <= 0)
                || (!this.isMinInclusive() && minToValueRelation < 0))
                && ((this.isMaxInclusive() && maxToValueRelation >= 0)
                        || (!this.isMaxInclusive() && maxToValueRelation > 0));
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Range))
            return false;
        if (obj == this)
            return true;
        @SuppressWarnings("unchecked")
        Range otherRange = (Range) obj;

        return this.getMin().compareTo(otherRange.getMin()) == 0 && this.getMax().compareTo(otherRange.getMax()) == 0
                && this.isMinInclusive() == otherRange.isMinInclusive()
                && this.isMaxInclusive() == otherRange.isMaxInclusive();
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash = (hash * 397) ^ this.getMin().hashCode();
        hash = (hash * 397) ^ this.getMax().hashCode();
        hash = (hash * 397) ^ Boolean.compare(this.isMinInclusive(), false);
        hash = (hash * 397) ^ Boolean.compare(this.isMaxInclusive(), false);
        return hash;
    }

    @Override
    public String toString() {
        return toJson();
    }
    
    @Override
    public String toJson() {
        return new StringBuilder().append(this.isMinInclusive() ? '[' : ')').append(this.getMin()).append(',')
                .append(this.getMax()).append(this.isMaxInclusive() ? ']' : ')').toString();
    }

    public static class MinComparator> implements Comparator> {
        @Override
        public int compare(Range range1, Range range2) {
            int result = range1.getMin().compareTo(range2.getMin());
            if (result != 0 || range1.isMinInclusive() == range2.isMinInclusive()) {
                return result;
            }

            return range1.isMinInclusive() ? -1 : 1;
        }
    }

    public static class MaxComparator> implements Comparator> {
        @Override
        public int compare(Range range1, Range range2) {
            int result = range1.getMax().compareTo(range2.getMax());
            if (result != 0 || range1.isMaxInclusive() == range2.isMaxInclusive()) {
                return result;
            }

            return range1.isMaxInclusive() ? 1 : -1;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy