/******************************************************************************* * Copyright (c) 2005, 2009 committers of openArchitectureWare and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * committers of openArchitectureWare - initial API and implementation *******************************************************************************/ package org.eclipse.xtend; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Version implements Comparable<Version> { private static final int MAJOR_VERSION = 1; private static final int MINOR_VERSION = 2; private static final Pattern versionPattern = Pattern.compile("^(\\d+)\\.(\\d+).*"); private int majorVersion; private int minorVersion; public Version(final String versionString) { if (versionString == null || !isVersionString(versionString)) throw new IllegalArgumentException(); final Matcher m = versionPattern.matcher(versionString); m.find(); majorVersion = Integer.parseInt(m.group(MAJOR_VERSION)); minorVersion = Integer.parseInt(m.group(MINOR_VERSION)); } public static boolean isVersionString(String versionString) { if (versionString == null) return false; final Matcher m = versionPattern.matcher(versionString); return m.find(); } public int compareTo(Version other) { if (this == other) return 0; int res = 0; if (getMajorVersion() < other.getMajorVersion()) { res = -1; } else if (getMajorVersion() > other.getMajorVersion()) { res = 1; } else { if (getMinorVersion() < other.getMinorVersion()) { res = -1; } else if (getMinorVersion() > other.getMinorVersion()) { res = 1; } } return res; } public int getMajorVersion() { return majorVersion; } public int getMinorVersion() { return minorVersion; } }