⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tools.java

📁 Vyger offers a D & D and Rogue-like environment in a graphical online roleplay game.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Light And Shadow. A Persistent Universe based on Robert Jordan's Wheel of Time Books.
 * Copyright (C) 2001-2002 WOTLAS Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
 
package wotlas.utils;

import java.util.*;
import java.io.*;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.zip.*;
import java.util.jar.*;
import javax.swing.JOptionPane;

/** Various useful tools...
 *
 * @author Aldiss
 */

public class Tools {

 /*------------------------------------------------------------------------------------*/

  /** Waits ms milliseconds with a very low CPU use.
   *
   * @param ms number of milliseconds to wait.
   */
    static public void waitTime( long ms )
    {
      Object o = new Object();
    
       synchronized( o ) {
          try{
               o.wait(ms);
          }
          catch(InterruptedException e) {}
       }
    }

 /*------------------------------------------------------------------------------------*/

  /** Is the Java version higher than the "min_required_version" string ?
   *  If it's not the case we return false and signal an ERROR to the Debug utility.
   *
   * @param required_min_version the minimum version number acceptable for this JVM
   *        ("1.2.2" for example).
   * @return true if the JVM version is higher, false otherwise.
   */
    static public boolean javaVersionHigherThan( String min_required_version )
    {
       String version = System.getProperty("java.version");

       if( version==null ) {
         Debug.signal( Debug.ERROR, null, "Could not obtain JVM version..." );
         return false;
       }

       if( version.compareTo(min_required_version) < 0) {
          Debug.signal( Debug.ERROR, null, "Your Java version is "+version
                       +". The minimum required version is "+min_required_version+" !" );
          return false;
       }

       return true;
    }

 /*------------------------------------------------------------------------------------*/ 

  /** To get a date formated in a lexical way ( year-month-day).
   *  Example: "2001-09-25". Note that we write "09" instead of "9".
   *
   * @return date
   */
   static public String getLexicalDate()
   {
      Calendar rightNow = Calendar.getInstance();
 
      String year = ""+rightNow.get(Calendar.YEAR);
      String month = null;
      String day = null;
      
      if( rightNow.get(Calendar.MONTH) <= 9 )
           month = "0"+(rightNow.get(Calendar.MONTH)+1);
      else
           month = ""+(rightNow.get(Calendar.MONTH)+1);

      if( rightNow.get(Calendar.DAY_OF_MONTH) <= 9 )
           day = "0"+rightNow.get(Calendar.DAY_OF_MONTH);
      else
           day = ""+rightNow.get(Calendar.DAY_OF_MONTH);

      return year+"-"+month+"-"+day;
   }
   
   
  /** To get a date formated in a lexical way ( year-month-day).
   *  Example: "2001-09-25". Note that we write "09" instead of "9".
   *
   * @param currentTime the date to convert
   * @return date
   */
   static public String getLexicalDate(Calendar currentTime)
   {      
 
      String year = ""+currentTime.get(Calendar.YEAR);
      String month = null;
      String day = null;
      
      if( currentTime.get(Calendar.MONTH) <= 9 )
           month = "0"+(currentTime.get(Calendar.MONTH)+1);
      else
           month = ""+(currentTime.get(Calendar.MONTH)+1);

      if( currentTime.get(Calendar.DAY_OF_MONTH) <= 9 )
           day = "0"+currentTime.get(Calendar.DAY_OF_MONTH);
      else
           day = ""+currentTime.get(Calendar.DAY_OF_MONTH);

      return year+"-"+month+"-"+day;
   }
   
   

 /*------------------------------------------------------------------------------------*/ 

  /** To get the time in pre-formated way.
   *  Example: "10h-05m-03s". Note that we write "03" instead of "3".
   *
   * @return time
   */
   static public String getLexicalTime()
   {
      Calendar rightNow = Calendar.getInstance();
 
      String hour = null;
      String min = null;
      String sec = null;
      
      if( rightNow.get(Calendar.HOUR_OF_DAY) <= 9 )
           hour = "0"+rightNow.get(Calendar.HOUR_OF_DAY);
      else
           hour = ""+rightNow.get(Calendar.HOUR_OF_DAY);

      if( rightNow.get(Calendar.MINUTE) <= 9 )
           min = "0"+rightNow.get(Calendar.MINUTE);
      else
           min = ""+rightNow.get(Calendar.MINUTE);

      if( rightNow.get(Calendar.SECOND) <= 9 )
           sec = "0"+rightNow.get(Calendar.SECOND);
      else
           sec = ""+rightNow.get(Calendar.SECOND);

      return hour+"h-"+min+"m-"+sec+"s";
   }

 /*------------------------------------------------------------------------------------*/ 

   /** To get an instance of an object from its class name. We assume that the
    *  object has an empty constructor.
    *
    *  @param className a string representing the class name of the filter
    *  @return an instance of the object, null if we cannot get an instance.
    */
      public static Object getInstance( String className ) {
          try{
               Class myClass = Class.forName(className);
               return myClass.newInstance();
          }catch(Exception ex) {
               Debug.signal( Debug.ERROR, null, "Failed to create new instance of "+className+", "+ex );
               return null;
          }
      }

 /*------------------------------------------------------------------------------------*/ 

   /** To display a GUI Debug Message.
    */
      public static void displayDebugMessage(  String title, String msg ) {
            JOptionPane.showMessageDialog(null,msg,title,JOptionPane.ERROR_MESSAGE);
      }

 /*------------------------------------------------------------------------------------*/ 

   /** To get a System property. If the property is not found we return an empty String.
    *
    *  @param key property key
    *  @return systemp property.
    */
     public static String getSystemProp( String key ) {

         String value = System.getProperty( key );
         
         if( value==null )
             return "";
         
         return value;
     }

 /*------------------------------------------------------------------------------------*/ 

  /** To tell if we are on a Windows or Unix System. This can be used if shell scripts are
   *  needed.
   */
     public static boolean isWindowsOS() {
          String os  = System.getProperty( "os.name" ).toLowerCase();
          
          if( os.indexOf("windows")<0 )
              return false;
          else
              return true;
     }

 /*------------------------------------------------------------------------------------*/ 

  /**
   * Returns a new string where the 'newStr' string has replaced all 'find' patterns.
   * @param in String to edit
   * @param find string to match
   * @param newStr string to substitude for find
   */
    public static String subString(String in, String find, String newStr ) {
       StringBuffer buf = new StringBuffer("");

       int cur = 0, nxt=0;
       
       while( cur<in.length() && ( (nxt = in.indexOf(find,cur) )>=0 ) ) {       	
       	  buf.append( in.substring(cur,nxt) );
       	  buf.append( newStr );
       	  cur = nxt+find.length();
       }

       if(cur<in.length())
          buf.append( in.substring( cur, in.length() ) );

       return buf.toString();
    }

 /*------------------------------------------------------------------------------------*/ 

  /** To create a random key of 'nbChars' chars.
   *  ( I smile because I'm sure there will be someone one day seeking this code
   *    to find the key's logic... well, as you see, they keys are generated very
   *    simply. ).
   * @return a string containing a random key of nbChars
   */
   static public String keyGenerator( int nbChars, int seed ) {
   	StringBuffer buf = new StringBuffer("");
        
        Random r = new Random(System.currentTimeMillis()*seed);
        
        for( int i=0; i<nbChars; i++ ) {
             if(r.nextInt(2)==1)
                buf.append( (char)('A'+r.nextInt(26)) );
             else
                buf.append( (char)('0'+r.nextInt(10)) );
        }
        
        return buf.toString();
   }

 /*------------------------------------------------------------------------------------*/ 

    /**
     *  <p> Search for classes that implement a given interface. You can specify in which
     *  packages to search. This method works EVEN if the classes are nested in a
     *  JAR or ZIP files (as long as the JAR/ZIP file is specified in the classpath)</p>
     *
     *  <p> Note that this does not make sense for all class loaders.  In cases
     *  where it doesn't make sense, the return value will be null. </p>
     *
     *  <p> This can also be a very slow method if the classpath is long. Here is an example
     *  of use :
     *  </p>
     *  <pre>
     *     String packages[] = { "wotlas.server.chat", "wotlas.server.chat.extra" };
     *     Class chatCommands[] = Tools.getImplementorsOf( "wotlas.server.chat.ChatCommand", packages );
     *
     *   will return the chat commands classes found in the two specified packages.
     *
     *   Other example :
     *
     *     Class chatCommands[] = Tools.getImplementorsOf( "wotlas.server.chat.ChatCommand", null );
     *
     *   will search everywhere for the commands (using the classpath).
     *  </pre>
     *
     *  IMPORTANT : we assume the classpath contains a least a "." if you want to search among
     *              the local files. If your project only contains JAR just enter them in your

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -