OS.java 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * JLibs: Common Utilities for Java
  3. * Copyright (C) 2009 Santhosh Kumar T <santhosh.tekuri@gmail.com>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. */
  15. package org.django.acquabooks.utils;
  16. public enum OS {
  17. WINDOWS_NT("Windows NT"),
  18. WINDOWS_95("Windows 95"),
  19. WINDOWS_98("Windows 98"),
  20. WINDOWS_2000("Windows 2000"),
  21. WINDOWS_VISTA("Windows Vista"),
  22. WINDOWS_7("Windows 7"),
  23. WINDOWS_OTHER("Windows"),
  24. SOLARIS("Solaris"),
  25. LINUX("Linux"),
  26. HP_UX("HP-UX"),
  27. IBM_AIX("AIX"),
  28. SGI_IRIX("Irix"),
  29. SUN_OS("SunOS"),
  30. COMPAQ_TRU64_UNIX("Digital UNIX"),
  31. MAC("Mac OS X", "Darwin"),
  32. FREE_BSD("freebsd"),
  33. // add new unix versions here
  34. OS2("OS/2"),
  35. COMPAQ_OPEN_VMS("OpenVMS"),
  36. /**
  37. * Unrecognized OS
  38. */
  39. OTHER("");
  40. private final String names[];
  41. private OS(final String... names) {
  42. this.names = names;
  43. }
  44. /**
  45. * @return true if this OS belongs to windows family
  46. */
  47. public boolean isWindows() {
  48. return ordinal() <= WINDOWS_OTHER.ordinal();
  49. }
  50. /**
  51. * @return true if this OS belongs to *nix family
  52. */
  53. public boolean isUnix() {
  54. return ordinal() > WINDOWS_OTHER.ordinal() && ordinal() < OS2.ordinal();
  55. }
  56. /**
  57. * @param osName name of OS as returned by <code>System.getProperty("os.name")</code>
  58. *
  59. * @return OS for the specified {@code osName}
  60. */
  61. public static OS get(String osName) {
  62. osName = osName.toLowerCase();
  63. for (final OS os : values()) {
  64. for (final String name : os.names) {
  65. if (osName.contains(name.toLowerCase())) {
  66. return os;
  67. }
  68. }
  69. }
  70. throw ContractUtils.unreachable();
  71. }
  72. private static OS current;
  73. /**
  74. * @return OS on which this JVM is running
  75. */
  76. public static OS get() {
  77. if (current == null) {
  78. current = get(System.getProperty("os.name"));
  79. }
  80. return current;
  81. }
  82. }