VersionRange.java

00001 /*
00002  * Copyright (c) 2005-2006, KNOPFLERFISH project
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following
00007  * conditions are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  *
00012  * - Redistributions in binary form must reproduce the above
00013  *   copyright notice, this list of conditions and the following
00014  *   disclaimer in the documentation and/or other materials
00015  *   provided with the distribution.
00016  *
00017  * - Neither the name of the KNOPFLERFISH project nor the names of its
00018  *   contributors may be used to endorse or promote products derived
00019  *   from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00027  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00028  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
00030  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00031  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
00032  * OF THE POSSIBILITY OF SUCH DAMAGE.
00033  */
00034 
00035 package org.knopflerfish.framework;
00036 
00037 import org.osgi.framework.Version;
00038 
00044 public class VersionRange implements Comparable
00045 {
00046   final private Version low;
00047   final private Version high;
00048   final private boolean lowIncluded;
00049   final private boolean highIncluded;
00050 
00054   public static final VersionRange defaultVersionRange = new VersionRange();
00055 
00065   public VersionRange(String vr) throws NumberFormatException {
00066     boolean op = vr.startsWith("(");
00067     boolean ob = vr.startsWith("[");
00068 
00069     if (op || ob) {
00070       boolean cp = vr.endsWith(")");
00071       boolean cb = vr.endsWith("]");
00072       int comma = vr.indexOf(',');
00073 
00074       if (comma > 0 && (cp || cb)) {
00075         low = new Version(vr.substring(1, comma).trim());
00076         high = new Version(vr.substring(comma + 1, vr.length() - 1).trim());
00077         lowIncluded = ob;
00078         highIncluded = cb;
00079       } else  {
00080         throw new NumberFormatException("Illegal version range: " + vr);
00081       }
00082     } else {
00083       low = new Version(vr);
00084       high = null;
00085       lowIncluded = true;
00086       highIncluded = false;
00087     }
00088   }
00089 
00090 
00095   protected VersionRange() {
00096     low = Version.emptyVersion;
00097     high = null;
00098     lowIncluded = true;
00099     highIncluded = false;
00100   }
00101 
00102 
00103   public boolean isSpecified() {
00104     return this != defaultVersionRange;
00105   }
00106 
00107 
00114   public boolean withinRange(Version ver) {
00115     if (this == defaultVersionRange) {
00116       return true;
00117     }
00118     int c = low.compareTo(ver);
00119 
00120     if (c < 0 || (c == 0 && lowIncluded)) {
00121       if (high == null) {
00122         return true;
00123       }
00124       c = high.compareTo(ver);
00125       return c > 0 || (c == 0 && highIncluded);
00126     }
00127     return false;
00128   }
00129 
00130 
00137   public boolean withinRange(VersionRange range) {
00138     if (this == range) {
00139       return true;
00140     }
00141     int c = low.compareTo(range.low);
00142 
00143     if (c < 0 || (c == 0 && lowIncluded == range.lowIncluded)) {
00144       if (high == null) {
00145         return true;
00146       }
00147       c = high.compareTo(range.high);
00148       return c > 0 || (c == 0 && highIncluded == range.highIncluded);
00149     }
00150     return false;
00151   }
00152 
00153 
00163   public int compareTo(Object obj) throws ClassCastException {
00164     VersionRange o = (VersionRange)obj;
00165     return low.compareTo(o.low);
00166   }
00167 
00168 
00175   public String toString() {
00176     if (high != null) {
00177       StringBuffer res = new StringBuffer();
00178       if (lowIncluded) {
00179         res.append('[');
00180       } else {
00181         res.append('(');
00182       }
00183       res.append(low.toString());
00184       res.append(',');
00185       res.append(high.toString());
00186       if (highIncluded) {
00187         res.append(']');
00188       } else {
00189         res.append(')');
00190       }
00191       return res.toString();
00192     } else {
00193       return low.toString();
00194     }
00195   }
00196 
00197 
00204   public boolean equals(Object obj) throws ClassCastException {
00205     VersionRange o = (VersionRange)obj;
00206     if (low.equals(o.low)) {
00207       if (high != null) {
00208         return high.equals(o.high)  &&
00209           lowIncluded == o.lowIncluded &&
00210           highIncluded == o.highIncluded;
00211       } else {
00212         return true;
00213       }
00214     }
00215     return false;
00216   }
00217 
00218 
00224   public int hashCode() {
00225     if (high != null) {
00226       return low.hashCode() + high.hashCode();
00227     } else {
00228       return low.hashCode();
00229     }
00230   }
00231 }

Generated on Mon Jan 11 21:19:17 2010 for OpenMobileIS by  doxygen 1.5.4