001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.internal; 018 019import org.apache.juneau.*; 020import org.apache.juneau.common.utils.*; 021 022/** 023 * Represents an OSGi-style version range like <js>"1.2"</js> or <js>"[1.0,2.0)"</js>. 024 * 025 * <p> 026 * The range can be any of the following formats: 027 * <ul> 028 * <li><js>"[0,1.0)"</js> = Less than 1.0. 1.0 and 1.0.0 does not match. 029 * <li><js>"[0,1.0]"</js> = Less than or equal to 1.0. Note that 1.0.1 will match. 030 * <li><js>"1.0"</js> = At least 1.0. 1.0 and 2.0 will match. 031 * </ul> 032 * 033 * <h5 class='section'>See Also:</h5><ul> 034 * </ul> 035 */ 036public class VersionRange { 037 038 private final Version minVersion, maxVersion; 039 private final boolean minExclusive, maxExclusive; 040 041 /** 042 * Constructor. 043 * 044 * @param range The range string to parse. 045 */ 046 public VersionRange(String range) { 047 range = range.trim(); 048 if (! range.isEmpty()) { 049 char c1 = range.charAt(0), c2 = range.charAt(range.length()-1); 050 int c = range.indexOf(','); 051 if (c > -1 && (c1 == '[' || c1 == '(') && (c2 == ']' || c2 == ')')) { 052 String v1 = range.substring(1, c), v2 = range.substring(c+1, range.length()-1); 053 minVersion = new Version(v1); 054 maxVersion = new Version(v2); 055 minExclusive = c1 == '('; 056 maxExclusive = c2 == ')'; 057 } else { 058 minVersion = new Version(range); 059 maxVersion = null; 060 minExclusive = maxExclusive = false; 061 } 062 } else { 063 minVersion = maxVersion = null; 064 minExclusive = maxExclusive = false; 065 } 066 } 067 068 /** 069 * Returns <jk>true</jk> if the specified version string matches this version range. 070 * 071 * @param v The version string (e.g. <js>"1.2.3"</js>) 072 * @return <jk>true</jk> if the specified version string matches this version range. 073 */ 074 public boolean matches(String v) { 075 if (Utils.isEmpty(v)) 076 return (minVersion == null && maxVersion == null); 077 Version ver = new Version(v); 078 if ((minVersion != null && ! ver.isAtLeast(minVersion, minExclusive)) || (maxVersion != null && ! ver.isAtMost(maxVersion, maxExclusive))) 079 return false; 080 return true; 081 } 082 083 @Override /* Object */ 084 public String toString() { 085 return (minExclusive ? "(" : "[") + minVersion + ',' + maxVersion + (maxExclusive ? ")" : "]"); 086 } 087}