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.commons.lang; 018 019import static org.apache.juneau.commons.utils.Utils.*; 020 021/** 022 * Represents an OSGi-style version range like <js>"1.2"</js> or <js>"[1.0,2.0)"</js>. 023 * 024 * <p> 025 * The range can be any of the following formats: 026 * <ul> 027 * <li><js>"[0,1.0)"</js> = Less than 1.0. 1.0 and 1.0.0 does not match. 028 * <li><js>"[0,1.0]"</js> = Less than or equal to 1.0. Note that 1.0.1 will match. 029 * <li><js>"1.0"</js> = At least 1.0. 1.0 and 2.0 will match. 030 * </ul> 031 * 032 */ 033public class VersionRange { 034 035 private final Version minVersion, maxVersion; 036 private final boolean minExclusive, maxExclusive; 037 038 /** 039 * Constructor. 040 * 041 * @param range The range string to parse. 042 */ 043 public VersionRange(String range) { 044 range = range.trim(); 045 if (! range.isEmpty()) { 046 char c1 = range.charAt(0), c2 = range.charAt(range.length() - 1); 047 var c = range.indexOf(','); 048 if (c > -1 && (c1 == '[' || c1 == '(') && (c2 == ']' || c2 == ')')) { 049 var v1 = range.substring(1, c); 050 var v2 = range.substring(c + 1, range.length() - 1); 051 minVersion = new Version(v1); 052 maxVersion = new Version(v2); 053 minExclusive = c1 == '('; 054 maxExclusive = c2 == ')'; 055 } else { 056 minVersion = new Version(range); 057 maxVersion = null; 058 minExclusive = maxExclusive = false; 059 } 060 } else { 061 minVersion = maxVersion = null; 062 minExclusive = maxExclusive = false; 063 } 064 } 065 066 /** 067 * Returns <jk>true</jk> if the specified version string matches this version range. 068 * 069 * @param v The version string (e.g. <js>"1.2.3"</js>) 070 * @return <jk>true</jk> if the specified version string matches this version range. 071 */ 072 public boolean matches(String v) { 073 if (e(v)) 074 return (minVersion == null && maxVersion == null); 075 var ver = new Version(v); 076 if ((nn(minVersion) && ! ver.isAtLeast(minVersion, minExclusive)) || (nn(maxVersion) && ! ver.isAtMost(maxVersion, maxExclusive))) 077 return false; 078 return true; 079 } 080 081 @Override /* Overridden from Object */ 082 public String toString() { 083 return (minExclusive ? "(" : "[") + minVersion + ',' + maxVersion + (maxExclusive ? ")" : "]"); 084 } 085}