OperatingSystemFamily.java

  1. /*******************************************************************************
  2.  * Copyright 2012 André Rouél
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  ******************************************************************************/
  16. package net.sf.uadetector;

  17. import java.util.regex.Matcher;
  18. import java.util.regex.Pattern;

  19. import javax.annotation.Nonnull;

  20. import net.sf.qualitycheck.Check;

  21. /**
  22.  * This enum represents the more commonly used operating system families. It will never be complete, but can assist in
  23.  * identifying the version of an operating system.
  24.  *
  25.  * @author André Rouél
  26.  */
  27. public enum OperatingSystemFamily {

  28.     /**
  29.      * AIX (Advanced Interactive eXecutive) is a Unix operating system from IBM.
  30.      */
  31.     AIX("AIX", Pattern.compile("AIX")),

  32.     /**
  33.      * AROS is a free operating system aiming at being compatible with AmigaOS at the API level.
  34.      */
  35.     AROS("AROS", Pattern.compile("AROS")),

  36.     /**
  37.      * AmigaOS is the native operating system for the Commodore Amiga, consisting of the components of Workbench,
  38.      * AmigaDOS with the command line interpreter CLI (later renamed to shell) and for many Amiga models in the ROM
  39.      * included kernel <i>kickstart</i>.
  40.      */
  41.     AMIGA_OS("Amiga OS", Pattern.compile("Amiga OS")),

  42.     /**
  43.      * Android is both an operating system and a software platform for mobile devices like smart phones, mobile phones,
  44.      * netbooks and tablets, which is developed by the Open Handset Alliance.
  45.      */
  46.     ANDROID("Android", Pattern.compile("Android")),

  47.     /**
  48.      * The Berkeley Software Distribution (BSD) is a version of the Unix operating system, which was created at the
  49.      * University of California at Berkeley in 1977.
  50.      */
  51.     BSD("BSD", Pattern.compile("BSD")),

  52.     /**
  53.      * Bada is a service-oriented operating system that is developed by Samsung Electronics and is designed for use in
  54.      * smartphones.
  55.      */
  56.     BADA("Bada", Pattern.compile("Bada")),

  57.     /**
  58.      * Be Operating System (BeOS) was an operating system of the company <i>Be Incorporated</i> and was called in later
  59.      * versions Be. Due to its multimedia capabilities it is also commonly called "Media OS".
  60.      */
  61.     BEOS("BeOS", Pattern.compile("BeOS")),

  62.     /**
  63.      * Danger OS is a smartphone operating system. It is used on Sidekick devices, which are sold in Germany by
  64.      * T-Mobile.
  65.      */
  66.     DANGEROS("DangerOS", Pattern.compile("DangerOS")),

  67.     /**
  68.      * Firefox OS is an open source operating system for smartphones and tablet computers being developed by Mozilla.
  69.      */
  70.     FIREFOX_OS("Firefox OS", Pattern.compile("Firefox OS")),

  71.     /**
  72.      * HP-UX (Hewlett Packard UniX) is a commercial Unix operating system from Hewlett-Packard and is based on UNIX
  73.      * System V.
  74.      */
  75.     HPUX("HP-UX", Pattern.compile("HP-UX")),

  76.     /**
  77.      * Haiku (formerly OpenBeOS) is an open-source project with the aim, to reprogram and expand that in 2001 abandoned
  78.      * operating system BeOS.
  79.      */
  80.     HAIKU("Haiku OS", Pattern.compile("Haiku OS")),

  81.     /**
  82.      * IRIX is a commercial Unix operating system of the company Silicon Graphics (SGI).
  83.      */
  84.     IRIX("IRIX", Pattern.compile("IRIX")),

  85.     /**
  86.      * Inferno is a distributed computer operating system that comes from Bell Laboratories.
  87.      */
  88.     INFERNO_OS("Inferno OS", Pattern.compile("Inferno OS")),

  89.     /**
  90.      * The Java Virtual Machine (abbreviated Java VM or JVM) is the part of the Java Runtime Environment (JRE) for Java
  91.      * programs, which is responsible for the execution of Java bytecode.<br>
  92.      * <br>
  93.      * This value is not an operating system family.
  94.      */
  95.     JVM("JVM", Pattern.compile("JVM")),

  96.     /**
  97.      * Linux or GNU/Linux are usually called free, unix-like multi-user operating systems running based on the Linux
  98.      * kernel and other GNU software.
  99.      */
  100.     LINUX("Linux", Pattern.compile("Linux")),

  101.     /**
  102.      * Mac OS is the name of the classic operating system (1984-2001) by Apple for Macintosh computers.
  103.      */
  104.     MAC_OS("Mac OS", Pattern.compile("Mac OS")),

  105.     /**
  106.      * MeeGo was a Linux kernel-based free mobile operating system project resulting from the fusion of Intel's Moblin
  107.      * and Nokia's Maemo operating systems.
  108.      */
  109.     MEEGO("MeeGo", Pattern.compile("MeeGo")),

  110.     /**
  111.      * Minix is a free unixoides operating system that was developed by Andrew S. Tanenbaum at the Free University of
  112.      * Amsterdam as a teaching tool.
  113.      */
  114.     MINIX("MINIX", Pattern.compile("MINIX")),

  115.     /**
  116.      * OpenVMS (Open Virtual Memory System), previously known as VAX-11/VMS, VAX/VMS or (informally) VMS, is a computer
  117.      * server operating system that runs on VAX, Alpha and Itanium-based families of computers.
  118.      */
  119.     OPENVMS("OpenVMS", Pattern.compile("OpenVMS")),

  120.     /**
  121.      * OS X, formerly Mac OS X, is a Unix-based operating systems developed by Apple. It is a proprietary distribution
  122.      * of the free Darwin operating system from Apple.
  123.      */
  124.     OS_X("OS X", Pattern.compile("(Mac OS X|OS X)")),

  125.     /**
  126.      * MorphOS is an Amiga-compatible computer operating system. It is a mixed proprietary and open source OS produced
  127.      * for the Pegasos PowerPC processor based computer.
  128.      */
  129.     MORPHOS("MorphOS", Pattern.compile("MorphOS")),

  130.     /**
  131.      * This value indicates the operating systems from Nintendo, which they developed for their devices.<br>
  132.      * <br>
  133.      * This value is not an operating system family.
  134.      */
  135.     NINTENDO("Nintendo", Pattern.compile("Nintendo")),

  136.     /**
  137.      * OS/2 is a multitasking operating system for PCs. It was originally developed by IBM and Microsoft together with
  138.      * the aim to replace DOS.
  139.      */
  140.     OS_2("OS/2", Pattern.compile("OS/2")),

  141.     /**
  142.      * Palm OS was the operating system for organizer of the Palm series (known as PDAs) and smartphones.
  143.      */
  144.     PALM_OS("Palm OS", Pattern.compile("Palm OS")),

  145.     /**
  146.      * The PlayStation Vita system software is the official, updatable firmware and operating system for the PlayStation
  147.      * Vita.
  148.      */
  149.     PLAYSTATION_VITA("LiveArea", Pattern.compile("LiveArea")),

  150.     /**
  151.      * QNX is a POSIX-compatible proprietary Unix-like real-time operating system that focused primarily at the embedded
  152.      * market.
  153.      */
  154.     QNX("QNX", Pattern.compile("QNX")),

  155.     /**
  156.      * RISC OS is a computer operating system originally designed by Acorn Computers Ltd. It was specifically designed
  157.      * to run on the ARM chipset, which Acorn had designed concurrently for use in its new line of Archimedes personal
  158.      * computers.
  159.      */
  160.     RISC_OS("RISC OS", Pattern.compile("RISC OS|RISK OS")),

  161.     /**
  162.      * Binary Runtime Environment for Wireless (Brew MP, Brew, or BREW) is an application development platform created
  163.      * by Qualcomm.
  164.      */
  165.     BREW("Brew", Pattern.compile("Brew")),

  166.     /**
  167.      * BlackBerry OS (up to the fifth edition known as the <i>BlackBerry Device Software</i>, also known as <i>Research
  168.      * In Motion OS</i>) is a proprietary mobile operating system developed by BlackBerry Ltd for its BlackBerry line of
  169.      * smartphone handheld devices.
  170.      */
  171.     BLACKBERRY_OS("BlackBerry OS", Pattern.compile("(RIM OS|BlackBerry OS)")),

  172.     /**
  173.      * Sailfish is a Linux-based mobile operating system developed by Jolla in cooperation with the Mer project and
  174.      * supported by the Sailfish Alliance.
  175.      */
  176.     SAILFISH_OS("Sailfish", Pattern.compile("Sailfish")),

  177.     /**
  178.      * Solaris is the name of an operating system distribution based on SunOS and is a Unix operating system. Since the
  179.      * takeover of Sun Microsystems in 2010 Solaris is part of Oracle.
  180.      */
  181.     SOLARIS("Solaris", Pattern.compile("Solaris")),

  182.     /**
  183.      * Syllable is a slim and fast desktop Unix-like operating system for x86 processors.
  184.      */
  185.     SYLLABLE("Syllable", Pattern.compile("Syllable")),

  186.     /**
  187.      * The Symbian platform, simply called Symbian, is an operating system for smartphones and PDAs. The Symbian
  188.      * platform is the successor to Symbian OS
  189.      */
  190.     SYMBIAN("Symbian OS", Pattern.compile("Symbian OS")),

  191.     /**
  192.      * Tizen is a free operating system based on Linux respectively Debian and was launched by the Linux Foundation and
  193.      * LiMo Foundation.
  194.      */
  195.     TIZEN("Tizen", Pattern.compile("Tizen")),

  196.     /**
  197.      * The Wii Operating System is based on Nintendo’s proprietary software and runs on the Wii video game console.
  198.      */
  199.     WII_OS("Wii OS", Pattern.compile("Nintendo Wii|Wii OS")),

  200.     /**
  201.      * Microsoft Windows is a trademark for operating systems of the Microsoft Corporation. Microsoft Windows was
  202.      * originally a graphical extension of the operating system MS-DOS.
  203.      */
  204.     WINDOWS("Windows", Pattern.compile("Windows")),

  205.     /**
  206.      * XrossMediaBar (XMB) is the name of the graphical user interface, which are used on PlayStation 3, PlayStation
  207.      * Portable, Sony Blu-Ray players and Sony Bravia TVs. Also some special versions of the PlayStation 2, PSX, already
  208.      * using the XMB.
  209.      */
  210.     XROSSMEDIABAR("XrossMediaBar (XMB)", Pattern.compile("XrossMediaBar (XMB)")),

  211.     /**
  212.      * iOS (until June 2010 iPhone OS) is the standard operating system of Apple products like iPhone, iPod touch, iPad,
  213.      * and the second generation of Apple TV. iOS is based on Mac OS X.
  214.      */
  215.     IOS("iOS", Pattern.compile("iOS|iPhone OS")),

  216.     /**
  217.      * webOS is a smartphone and tablet operating system from Hewlett-Packard (formerly HP Palm). It represents the
  218.      * follower of Palm OS.
  219.      */
  220.     WEBOS("webOS", Pattern.compile("webOS")),

  221.     /**
  222.      * Unknown operating system family<br>
  223.      * <br>
  224.      * This value will be returned if the operating system family cannot be determined.
  225.      */
  226.     UNKNOWN("", Pattern.compile("^$"));

  227.     /**
  228.      * This method try to find by the given family name a matching enum value. The family name must match against an
  229.      * operating system entry in UAS data file.
  230.      *
  231.      * @param family
  232.      *            name of an operating system family
  233.      * @return the matching enum value or {@code OperatingSystemFamily#UNKNOWN}
  234.      * @throws net.sf.qualitycheck.exception.IllegalNullArgumentException
  235.      *             if the given argument is {@code null}
  236.      */
  237.     public static OperatingSystemFamily evaluate(@Nonnull final String family) {
  238.         Check.notNull(family, "family");

  239.         OperatingSystemFamily result = UNKNOWN;

  240.         // search by name
  241.         result = evaluateByName(family);

  242.         // search by pattern
  243.         if (result == UNKNOWN) {
  244.             result = evaluateByPattern(family);
  245.         }

  246.         return result;
  247.     }

  248.     /**
  249.      * This method try to find by the given family name a matching enum value. The family name will be evaluated against
  250.      * the stored name of an operating system entry.
  251.      *
  252.      * @param family
  253.      *            name of an operating system family
  254.      * @return the matching enum value or {@code OperatingSystemFamily#UNKNOWN}
  255.      * @throws net.sf.qualitycheck.exception.IllegalNullArgumentException
  256.      *             if the given argument is {@code null}
  257.      */
  258.     protected static OperatingSystemFamily evaluateByName(@Nonnull final String family) {
  259.         Check.notNull(family, "family");

  260.         OperatingSystemFamily result = UNKNOWN;
  261.         for (final OperatingSystemFamily value : values()) {
  262.             if (value.getName().equals(family)) {
  263.                 result = value;
  264.                 break;
  265.             }
  266.         }

  267.         return result;
  268.     }

  269.     /**
  270.      * This method try to find by the given family name a matching enum value. The family name will be evaluated against
  271.      * the stored regular expression of an operating system entry.
  272.      *
  273.      * @param family
  274.      *            name of an operating system family
  275.      * @return the matching enum value or {@code OperatingSystemFamily#UNKNOWN}
  276.      * @throws net.sf.qualitycheck.exception.IllegalNullArgumentException
  277.      *             if the given argument is {@code null}
  278.      */
  279.     protected static OperatingSystemFamily evaluateByPattern(@Nonnull final String family) {
  280.         Check.notNull(family, "family");

  281.         OperatingSystemFamily result = UNKNOWN;
  282.         for (final OperatingSystemFamily value : values()) {
  283.             final Matcher m = value.getPattern().matcher(family);
  284.             if (m.matches()) {
  285.                 result = value;
  286.                 break;
  287.             }
  288.         }

  289.         return result;
  290.     }

  291.     /**
  292.      * The internal family name in the UAS database.
  293.      */
  294.     @Nonnull
  295.     private final String name;

  296.     /**
  297.      * The regular expression which a family name must be match.
  298.      */
  299.     @Nonnull
  300.     private final Pattern pattern;

  301.     private OperatingSystemFamily(@Nonnull final String name, @Nonnull final Pattern pattern) {
  302.         this.name = name;
  303.         this.pattern = pattern;
  304.     }

  305.     /**
  306.      * Gets the internal family name in the UAS database.
  307.      *
  308.      * @return the internal family name
  309.      */
  310.     @Nonnull
  311.     public String getName() {
  312.         return this.name;
  313.     }

  314.     /**
  315.      * Gets the regular expression which a family name must be match with.
  316.      *
  317.      * @return regular expression
  318.      */
  319.     @Nonnull
  320.     public Pattern getPattern() {
  321.         return pattern;
  322.     }

  323. }