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

com.day.jcr.vault.packaging.VersionRange Maven / Gradle / Ivy

/*************************************************************************
 *
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2011 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 *
 **************************************************************************/

package com.day.jcr.vault.packaging;

/**
 * Implements a version range
 *
 * @since 2.0
 * @deprecated since 2.5.0. Use the org.apache.jackrabbit.vault API instead.
 */
@Deprecated
public class VersionRange {

    /**
     * Infinite (covers all) range.
     */
    public static final VersionRange INFINITE = new VersionRange(null, true, null, true);

    /**
     * lower bound
     */
    private final Version low;

    /**
     * specifies if lower bound is inclusive
     */
    private final boolean lowIncl;

    /**
     * upper bound
     */
    private final Version high;

    /**
     * specifies if upper bound is inclusive
     */
    private final boolean highIncl;

    /**
     * internal string representation
     */
    private final String str;

    /**
     * Creates a new version range.
     * @param low lower bound or null
     * @param lowIncl specifies if lower bound is inclusive
     * @param high upper bound or null
     * @param highIncl specifies if upper bound is inclusive
     * @throws IllegalArgumentException if bounds are not valid
     */
    public VersionRange(Version low, boolean lowIncl, Version high, boolean highIncl) {
        // check if range is valid
        if (low != null && high != null) {
            int comp = low.compareTo(high);
            if (comp > 0) {
                throw new IllegalArgumentException("lower bound must be less or equal to upper bound.");
            } else if (comp == 0) {
                if (!lowIncl || !highIncl) {
                    throw new IllegalArgumentException("invalid empty range. upper and lower bound must be inclusive.");
                }
            }
        }
        this.low = low;
        this.lowIncl = lowIncl;
        this.high = high;
        this.highIncl = highIncl;
        StringBuilder b = new StringBuilder();
        if (low == null && high == null) {
            // infinite range, empty string
        } else if (high == null) {
            // no high bound,
            if (lowIncl) {
                // special case, just use version
                b.append(low);
            } else {
                b.append('(');
                b.append(low);
                b.append(",)");
            }
        } else if (low == null) {
            b.append("[,");
            b.append(high);
            b.append(highIncl ? ']' : ')');
        } else {
            b.append(lowIncl ? '[' : '(');
            b.append(low);
            b.append(',');
            b.append(high);
            b.append(highIncl ? ']' : ')');
        }
        this.str = b.toString();
    }

    /**
     * Creates a new version range that exactly includes the given version.
     * @param v the version.
     */
    public VersionRange(Version v) {
        this(v, true, v, true);
    }

    /**
     * Returns the lower bound
     * @return the lower bound or null
     */
    public Version getLow() {
        return low;
    }

    /**
     * Returns true if the lower bound is inclusive
     * @return true if the lower bound is inclusive
     */
    public boolean isLowInclusive() {
        return lowIncl;
    }

    /**
     * Returns the upper bound
     * @return the upper bound or null
     */
    public Version getHigh() {
        return high;
    }

    /**
     * Returns true if the upper bound is inclusive
     * @return true if the upper bound is inclusive
     */
    public boolean isHighInclusive() {
        return highIncl;
    }

    @Override
    public int hashCode() {
        return str.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        return this == obj ||
                obj instanceof VersionRange && str.equals(obj.toString());
    }

    @Override
    public String toString() {
        return str;
    }

    /**
     * Checks if the given version is in this range.
     * @param v the version to check
     * @return true if the given version is in this range.
     */
    public boolean isInRange(Version v) {
        if (low != null) {
            int comp = v.osgiCompareTo(low);
            if (comp < 0 || comp == 0 && !lowIncl) {
                return false;
            }
        }
        if (high != null) {
            int comp = v.osgiCompareTo(high);
            if (comp > 0 || comp == 0 && !highIncl) {
                return false;
            }
        }
        return true;
    }

    /**
     * Creates a range from a string
     * @param str string
     * @return the version range
     */
    public static VersionRange fromString(String str) {
        int idx = str.indexOf(',');
        if (idx >= 0) {
            boolean linc = false;
            int lm = str.indexOf('(');
            if (lm < 0) {
                lm = str.indexOf('[');
                if (lm < 0) {
                    throw new IllegalArgumentException("Range must start with '[' or '('");
                }
                linc = true;
            }
            boolean hinc = false;
            int hm = str.indexOf(')');
            if (hm < 0) {
                hm = str.indexOf(']');
                if (hm < 0) {
                    throw new IllegalArgumentException("Range must end with ']' or ')'");
                }
                hinc = true;
            }
            String low = str.substring(lm + 1, idx).trim();
            String high = str.substring(idx+1, hm).trim();
            Version vLow = low.length() == 0 ? null : Version.create(low);
            Version vHigh = high.length() == 0 ? null : Version.create(high);
            return new VersionRange(vLow, linc, vHigh, hinc);
        } else if (str.length() == 0) {
            // infinite range
            return new VersionRange(null, false, null, false);
        } else {
            // simple range where given version is minimum
            return new VersionRange(Version.create(str), true, null, false);
        }
    }

    public org.apache.jackrabbit.vault.packaging.VersionRange toJrVltVersionRange() {
        return org.apache.jackrabbit.vault.packaging.VersionRange.fromString(str);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy