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
015import static org.apache.juneau.common.internal.StringUtils.*;
016
017import org.apache.juneau.*;
018
019/**
020 * Represents an OSGi-style version range like <js>"1.2"</js> or <js>"[1.0,2.0)"</js>.
021 *
022 * <p>
023 * The range can be any of the following formats:
024 * <ul>
025 *    <li><js>"[0,1.0)"</js> = Less than 1.0.  1.0 and 1.0.0 does not match.
026 *    <li><js>"[0,1.0]"</js> = Less than or equal to 1.0.  Note that 1.0.1 will match.
027 *    <li><js>"1.0"</js> = At least 1.0.  1.0 and 2.0 will match.
028 * </ul>
029 *
030 * <h5 class='section'>See Also:</h5><ul>
031 * </ul>
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         int c = range.indexOf(',');
048         if (c > -1 && (c1 == '[' || c1 == '(') && (c2 == ']' || c2 == ')')) {
049            String v1 = range.substring(1, c), v2 = range.substring(c+1, range.length()-1);
050            minVersion = new Version(v1);
051            maxVersion = new Version(v2);
052            minExclusive = c1 == '(';
053            maxExclusive = c2 == ')';
054         } else {
055            minVersion = new Version(range);
056            maxVersion = null;
057            minExclusive = maxExclusive = false;
058         }
059      } else {
060         minVersion = maxVersion = null;
061         minExclusive = maxExclusive = false;
062      }
063   }
064
065   /**
066    * Returns <jk>true</jk> if the specified version string matches this version range.
067    *
068    * @param v The version string (e.g. <js>"1.2.3"</js>)
069    * @return <jk>true</jk> if the specified version string matches this version range.
070    */
071   public boolean matches(String v) {
072      if (isEmpty(v))
073         return (minVersion == null && maxVersion == null);
074      Version ver = new Version(v);
075      if ((minVersion != null && ! ver.isAtLeast(minVersion, minExclusive)) || (maxVersion != null && ! ver.isAtMost(maxVersion, maxExclusive)))
076         return false;
077      return true;
078   }
079
080   @Override /* Object */
081   public String toString() {
082      return (minExclusive ? "(" : "[") + minVersion + ',' + maxVersion + (maxExclusive ? ")" : "]");
083   }
084}