X Tutup
package javaxt.utils; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.text.DecimalFormat; import java.lang.reflect.Array; import java.util.HashMap; //****************************************************************************** //** Console //****************************************************************************** /** * Various command line utilities used to print debug messages and collect * user inputs. * ******************************************************************************/ public class Console { private static final String indent = " "; // 25 spaces private static final DecimalFormat df = new DecimalFormat("#.##"); /** Static instance of the this class that can be called directly via a * static import. Example:
    import static javaxt.utils.Console.console;
    public class Test {
        public Test(){
            console.log("Hello!");
        }
    }
   
*/ public static Console console = new Console(); //************************************************************************** //** log //************************************************************************** /** Prints a message to the standard output stream, along with the class * name and line number where the log() function is called. This function * is intended to help debug applications and is inspired by the JavaScript * console.log() function. * @param any Accepts any Java object (e.g. string, number, null, classes, * arrays, etc). You can pass in multiple objects separated by a comma. * Example:
       console.log("Hello");
       console.log("Date: ", new Date());
       console.log(1>0, 20/5, new int[]{1,2,3});
   
*/ public void log(Object... any){ //Get source String source = getSource(); source = "[" + source + "]"; if (source.length()0) sb.append(","); sb.append(s); } sb.append("]"); str = sb.toString(); } else{ str = format(obj); } } if (n>0) out.append(" "); out.append(str); n++; } } else{ out.append(any); } System.out.println(out); } //************************************************************************** //** format //************************************************************************** private String format(Object obj){ String str = null; if (obj!=null){ if (obj instanceof Double){ df.setMaximumFractionDigits(8); str = df.format((Double) obj); } else{ str = obj.toString(); } } return str; } //************************************************************************** //** getSource //************************************************************************** /** Returns the class name and line number that was used to call the log() * method. */ private String getSource(){ //Create an exception and get the stack trace Exception e = new Exception(); StackTraceElement[] stackTrace = e.getStackTrace(); //Find first element in the stack trace that is not an instance of this class StackTraceElement target = null; boolean foundConsole = false; for (int i=1; i0) className = className.substring(idx+1); idx = className.indexOf("$"); if (idx>0) className = className.substring(0, idx); return className + ":" + target.getLineNumber(); } //************************************************************************** //** getInput //************************************************************************** /** Used to prompt a user for an input */ public static String getInput(String prompt){ String input = null; System.out.print(prompt); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { input = br.readLine(); } catch (IOException e) { System.out.println("Failed to read input"); //System.exit(1); } return input; } //************************************************************************** //** getUserName //************************************************************************** /** Used to prompt a user for a username */ public static String getUserName(String prompt){ return getInput(prompt); } //************************************************************************** //** getPassword //************************************************************************** /** Used to prompt a user for a password. The password is hidden as is it * entered. */ public static String getPassword(String prompt) { return new String(System.console().readPassword(prompt)); } //************************************************************************** //** parseArgs //************************************************************************** /** Converts command line inputs into key/value pairs. Assumes keys start * with a "-" character (e.g. "-version" or "--version") followed by a * value (or nothing at all). */ public static HashMap parseArgs(String[] args){ HashMap map = new HashMap<>(); for (int i=0; i args, String ...keys){ for (String key : keys){ if (args.containsKey(key)){ return new Value(args.get(key)); } else{ //Fuzzy match (user passed one hyphen instead of two) if (key.startsWith("-")){ String k = key.substring(1); if (k.startsWith("-") && args.containsKey(k)){ return new Value(args.get(k)); } } } } return new Value(null); } //************************************************************************** //** main //************************************************************************** /** Used to print the JavaXT version number when this jar is called from the * command line. Example:
    java -jar javaxt-core.jar
    JavaXT: 1.11.3
   
* Under the hood, this simple command line application uses the * getVersion() method in the javaxt.io.Jar class. */ public static void main(String[] args) { javaxt.io.Jar jar = new javaxt.io.Jar(javaxt.io.Jar.class); System.out.println("JavaXT: " + jar.getVersion()); } }
X Tutup