001/* 
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.xbean.osgi.bundle.util;
020
021import org.osgi.framework.Version;
022
023public class VersionRange {
024
025    private Version low = null;
026    private boolean isLowInclusive = false;
027    private Version high = null;
028    private boolean isHighInclusive = false;
029    
030    public static final VersionRange infiniteRange = new VersionRange(Version.emptyVersion, true, null, true);
031
032    public VersionRange(Version low, boolean isLowInclusive, Version high, boolean isHighInclusive) {
033        this.low = low;
034        this.isLowInclusive = isLowInclusive;
035        this.high = high;
036        this.isHighInclusive = isHighInclusive;
037    }
038
039    public Version getLow() {   
040        return low;
041    }
042
043    public boolean isLowInclusive() {   
044        return isLowInclusive;
045    }
046
047    public Version getHigh() {   
048        return high;
049    }
050
051    public boolean isHighInclusive() {   
052        return isHighInclusive;
053    }
054
055    public boolean isInRange(Version version) {   
056        // We might not have an upper end to the range.
057        if (high == null) {       
058            return (version.compareTo(low) >= 0);
059        } else if (isLowInclusive() && isHighInclusive()) {       
060            return (version.compareTo(low) >= 0) && (version.compareTo(high) <= 0);
061        } else if (isHighInclusive()) {
062            return (version.compareTo(low) > 0) && (version.compareTo(high) <= 0);
063        } else if (isLowInclusive()) {
064            return (version.compareTo(low) >= 0) && (version.compareTo(high) < 0);
065        }
066        return (version.compareTo(low) > 0) && (version.compareTo(high) < 0);
067    }
068
069    public static VersionRange parse(String range) {   
070        // Check if the version is an interval. 
071        if (range.indexOf(',') >= 0) {       
072            String s = range.substring(1, range.length() - 1);
073            String vlo = s.substring(0, s.indexOf(',')).trim();
074            String vhi = s.substring(s.indexOf(',') + 1, s.length()).trim();
075            return new VersionRange (
076                new Version(vlo), (range.charAt(0) == '['),
077                new Version(vhi), (range.charAt(range.length() - 1) == ']'));
078        } else {
079            return new VersionRange(new Version(range), true, null, false);
080        }
081    }
082
083    public String toString() {    
084        if (high != null) {
085            StringBuffer sb = new StringBuffer();
086            sb.append(isLowInclusive ? '[' : '(');
087            sb.append(low.toString());
088            sb.append(',');
089            sb.append(high.toString());
090            sb.append(isHighInclusive ? ']' : ')');
091            return sb.toString();
092        } else {
093            return low.toString();
094        }
095    }
096}