1 /*
   2  * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.test.lib;
  27 
  28 import java.util.Arrays;
  29 import java.io.BufferedReader;
  30 import java.io.FileReader;
  31 import java.util.regex.Pattern;
  32 import java.util.stream.Collectors;
  33 import java.security.AccessController;
  34 import java.security.PrivilegedActionException;
  35 import java.security.PrivilegedExceptionAction;
  36 
  37 public final class OSVersion implements Comparable<OSVersion> {
  38     public static final OSVersion WINDOWS_95 = new OSVersion(4, 0);
  39     public static final OSVersion WINDOWS_98 = new OSVersion(4, 10);
  40     public static final OSVersion WINDOWS_ME = new OSVersion(4, 90);
  41     public static final OSVersion WINDOWS_2000 = new OSVersion(5, 0);
  42     public static final OSVersion WINDOWS_XP = new OSVersion(5, 1);
  43     public static final OSVersion WINDOWS_2003 = new OSVersion(5, 2);
  44     public static final OSVersion WINDOWS_VISTA = new OSVersion(6, 0);
  45 
  46     private final int[] versionTokens;
  47 
  48     public static OSVersion current() {
  49         return new OSVersion(Platform.getOsVersion());
  50     }
  51 
  52     public OSVersion(int major, int minor) {
  53         versionTokens = new int[] {major, minor};
  54     }
  55 
  56     public OSVersion(String version) {
  57         Pattern onlyDigits = Pattern.compile("^\\d+$");
  58         this.versionTokens = Arrays.stream(version.split("-")[0].split("\\."))
  59                                    .filter(onlyDigits.asPredicate())
  60                                    .mapToInt(Integer::parseInt)
  61                                    .toArray();
  62     }
  63 
  64     @Override
  65     public int compareTo(OSVersion o) {
  66         return Arrays.compare(this.versionTokens, o.versionTokens);
  67     }
  68 
  69     @Override
  70     public int hashCode() {
  71         return Arrays.hashCode(versionTokens);
  72     }
  73 
  74     @Override
  75     public boolean equals(Object o) {
  76         if (this == o) return true;
  77         if (o == null || getClass() != o.getClass()) return false;
  78         OSVersion osVersion = (OSVersion) o;
  79         return Arrays.equals(versionTokens, osVersion.versionTokens);
  80     }
  81 
  82     @Override
  83     public String toString() {
  84         return Arrays.stream(versionTokens)
  85                      .mapToObj(String::valueOf)
  86                      .collect(Collectors.joining("."));
  87     }
  88 }
  89