001/*
002 * Extra Bnd Repository Plugins
003 * Copyright (C) 2019  Michael N. Lipp
004 * 
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Affero General Public License as published by 
007 * the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 * 
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU Affero General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package de.mnl.osgi.bnd.maven;
020
021/**
022 * The common base class of {@link MavenVersion} and {@link MavenVersionRange}.
023 */
024@SuppressWarnings("PMD.AbstractClassWithoutAbstractMethod")
025public abstract class MavenVersionSpecification {
026
027    /**
028     * Checks if is the provided version representation is a range.
029     * If {@code version} is {@code} null it is considered to be
030     * the "all inclusive range" ("[0,)").
031     *
032     * @param version the version
033     * @return true, if is range
034     */
035    public static boolean isRange(String version) {
036        if (version == null) {
037            return true;
038        }
039        version = version.trim();
040        return version.startsWith("[") || version.startsWith("(");
041    }
042
043    /**
044     * Creates a maven version specification from the given arguments.
045     *
046     * @param version the version
047     * @return the maven version specification
048     */
049    public static MavenVersionSpecification from(String version) {
050        if (isRange(version)) {
051            return new MavenVersionRange(version);
052        } else {
053            return new MavenVersion(version);
054        }
055    }
056}