Path: blob/master/test/lib/jdk/test/lib/OSVersion.java
42759 views
/*1* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package jdk.test.lib;2627import java.util.Arrays;28import java.io.BufferedReader;29import java.io.FileReader;30import java.util.regex.Pattern;31import java.util.stream.Collectors;32import java.security.AccessController;33import java.security.PrivilegedActionException;34import java.security.PrivilegedExceptionAction;3536public final class OSVersion implements Comparable<OSVersion> {37public static final OSVersion WINDOWS_95 = new OSVersion(4, 0);38public static final OSVersion WINDOWS_98 = new OSVersion(4, 10);39public static final OSVersion WINDOWS_ME = new OSVersion(4, 90);40public static final OSVersion WINDOWS_2000 = new OSVersion(5, 0);41public static final OSVersion WINDOWS_XP = new OSVersion(5, 1);42public static final OSVersion WINDOWS_2003 = new OSVersion(5, 2);43public static final OSVersion WINDOWS_VISTA = new OSVersion(6, 0);4445private final int[] versionTokens;4647public static OSVersion current() {48return new OSVersion(Platform.getOsVersion());49}5051public OSVersion(int major, int minor) {52versionTokens = new int[] {major, minor};53}5455public OSVersion(String version) {56Pattern onlyDigits = Pattern.compile("^\\d+$");57this.versionTokens = Arrays.stream(version.split("-")[0].split("\\."))58.filter(onlyDigits.asPredicate())59.mapToInt(Integer::parseInt)60.toArray();61}6263@Override64public int compareTo(OSVersion o) {65return Arrays.compare(this.versionTokens, o.versionTokens);66}6768@Override69public int hashCode() {70return Arrays.hashCode(versionTokens);71}7273@Override74public boolean equals(Object o) {75if (this == o) return true;76if (o == null || getClass() != o.getClass()) return false;77OSVersion osVersion = (OSVersion) o;78return Arrays.equals(versionTokens, osVersion.versionTokens);79}8081@Override82public String toString() {83return Arrays.stream(versionTokens)84.mapToObj(String::valueOf)85.collect(Collectors.joining("."));86}87}88899091