001package com.hfg.util;
002
003import java.util.Collection;
004import java.util.Collections;
005import java.util.HashMap;
006import java.util.Map;
007
008//==============================================================================
009/**
010 Operating system detection class.
011 <div>
012 @author J. Alex Taylor, hairyfatguy.com
013 </div>
014 */
015//==============================================================================
016// com.hfg XML/HTML Coding Library
017//
018// This library is free software; you can redistribute it and/or
019// modify it under the terms of the GNU Lesser General Public
020// License as published by the Free Software Foundation; either
021// version 2.1 of the License, or (at your option) any later version.
022//
023// This library is distributed in the hope that it will be useful,
024// but WITHOUT ANY WARRANTY; without even the implied warranty of
025// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
026// Lesser General Public License for more details.
027//
028// You should have received a copy of the GNU Lesser General Public
029// License along with this library; if not, write to the Free Software
030// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
031//
032// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
033// jataylor@hairyfatguy.com
034//==============================================================================
035
036public class OS implements Comparable<OS>
037{
038   private String mName;
039
040
041   private static Map<String, OS> mValueMap = new HashMap<>();
042   private static OS mCurrentOS;
043
044   public static OS Windows = new OS("Windows");
045   public static OS Mac     = new OS("Mac OS X");
046   public static OS UNIX    = new OS("UNIX");
047   public static OS Solaris = new OS("Solaris");
048   public static OS Unknown = new OS("Unknown");
049
050   //---------------------------------------------------------------------------
051   private OS(String inName)
052   {
053      mName = inName;
054      mValueMap.put(mName, this);
055   }
056
057   //---------------------------------------------------------------------------
058   @Override
059   public String toString()
060   {
061      return name();
062   }
063
064   //---------------------------------------------------------------------------
065   public String name()
066   {
067      return mName;
068   }
069
070
071   //---------------------------------------------------------------------------
072   @Override
073   public int hashCode()
074   {
075      return name().hashCode();
076   }
077
078   //---------------------------------------------------------------------------
079   @Override
080   public boolean equals(Object inObj2)
081   {
082      return (inObj2 != null
083              && inObj2 instanceof OS
084              && 0 == compareTo((OS) inObj2));
085   }
086
087   //---------------------------------------------------------------------------
088   public int compareTo(OS inObj2)
089   {
090      return CompareUtil.compare(name(), inObj2.name());
091   }
092
093   //---------------------------------------------------------------------------
094   public static Collection<OS> values()
095   {
096      return Collections.unmodifiableCollection(mValueMap.values());
097   }
098
099   //---------------------------------------------------------------------------
100   public static OS valueOf(String inString)
101   {
102      return mValueMap.get(inString);
103   }
104
105   //---------------------------------------------------------------------------
106   public static synchronized OS value()
107   {
108      if (null == mCurrentOS)
109      {
110         String sysProperty = System.getProperty("os.name").toLowerCase();
111         if (sysProperty.contains("mac"))
112         {
113            mCurrentOS = OS.Mac;
114         }
115         else if (sysProperty.contains("win"))
116         {
117            mCurrentOS = OS.Windows;
118         }
119         else if (sysProperty.contains("sunos"))
120         {
121            mCurrentOS = OS.Solaris;
122         }
123         else if (sysProperty.contains("ix")
124                  || (sysProperty.contains("ux")))
125         {
126            mCurrentOS = OS.UNIX;
127         }
128         else
129         {
130            mCurrentOS = OS.Unknown;
131         }
132      }
133
134      return mCurrentOS;
135   }
136}