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

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

/*
 * The MIT License (MIT)
 * Copyright (c) 2018 Microsoft Corporation
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

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

import java.util.Comparator;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.microsoft.azure.cosmosdb.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(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;
        super.set(Range.MIN_PROPERTY, 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;
        super.set(Range.MAX_PROPERTY, 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;
    }

    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 - 2025 Weber Informatics LLC | Privacy Policy