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