javatools.administrative
Class D

java.lang.Object
  extended by javatools.administrative.D

public class D
extends java.lang.Object

This class is part of the Java Tools (see http://mpii.de/yago-naga/javatools). It is licensed under the Creative Commons Attribution License (see http://creativecommons.org/licenses/by/3.0) by the YAGO-NAGA team (see http://mpii.de/yago-naga). This class provides convenience methods for Input/Output. Allows to do basic I/O with easy procedure calls -- nearly like in normal programming languages. Furthermore, the class provides basic set operations for EnumSets, NULL-safe comparisons and adding to maps.
Example:

 D.p("This is an easy way to write a string");
 // And this is an easy way to read one:
 String s=D.r();

 // Here is a cool way to print something inline
 computeProduct(factor1,(Integer)D.p(factor2));

 // Here are some tricks with enums
 enum T {a,b,c};
 EnumSet<T> i=D.intersection(EnumSet.of(T.a,T.b),EnumSet.of(T.b,T.c));
 EnumSet<T> u=D.union(EnumSet.of(T.a,T.b),EnumSet.of(T.b,T.c));

 // Here is how to compare things, even if they are NULL
 D.compare(object1, object2);

 // Here is how to add something to maps that contain lists
 Map<String,List<String>> string2list=new TreeMap<String,List<String>>();
 D.addKeyValue(string2list,"key","new list element",ArrayList.class); 
 // now, the map contains "key" -> [ "new list element" ]
 D.addKeyValue(string2list,"key","again a new list element",ArrayList.class);
 // now, the map contains "key" -> [ "new list element", "again a new list element" ]  

 // Here is how to add something to maps that contain integers
 Map<String,Integer> string2list=new TreeMap<String,Integer>();
 D.addKeyValue(string2list,"key",7); // map now contains "key" -> 7
 D.addKeyValue(string2list,"key",3); // map now contains "key" -> 10

 


Field Summary
static int indent
          Indentation margin.
 
Constructor Summary
D()
           
 
Method Summary
static
<K,V,C extends java.util.Collection<V>,L extends java.util.Collection>
void
addKeyValue(java.util.Map<K,C> map, K key, V value, java.lang.Class<L> collectionType)
          Given a map that maps to collections, adds a new key/value pair or introduces the key
static
<K> void
addKeyValue(java.util.Map<K,java.lang.Integer> map, K key, int value)
          Given a map that maps to integers, adds a new key/value pair or increases the counter
static
<K> void
addKeyValueDbl(java.util.Map<K,java.lang.Double> map, K key, double value)
          Given a map that maps to doubles, adds a new key/value pair or increases the counter
static
<K> void
addKeyValueFlt(java.util.Map<K,java.lang.Float> map, K key, float value)
          Given a map that maps to floats, adds a new key/value pair or increases the counter
static
<K,V,C extends java.util.Collection<V>,L extends java.util.Collection>
void
addKeyValues(java.util.Map<K,C> map, K key, C values, java.lang.Class<L> collectionType)
          Given a map that maps to collections, adds a new key/value pair or introduces the key
static
<E extends java.lang.Comparable<E>>
int
compare(E s1, E s2)
          Compares two things, including NULL
static int comparePairs(java.lang.Object... o)
          Compares pairs of comparable things (a1,a2,b1,b2,...), including NULL
static
<E extends java.lang.Enum<E>>
boolean
containsOneOf(java.util.EnumSet<E> s1, java.util.EnumSet<E> s2)
          Tells whether the intersection is non-empty
static
<E> boolean
equal(E s1, E s2)
          Returns true if two things are equal, including NULL
static boolean equalPairs(java.lang.Object... o)
          Compares pairs of comparable things (a1,a2,b1,b2,...) for equality, including NULL
static void execute(java.lang.String cmd, java.io.File folder)
          Executes a command
static void exit()
          Exits with error code 0
static
<K,V> V
getOr(java.util.Map<K,V> map, K key, V defValue)
          Returns the element of a map or a default value
static
<K> int
getOrZero(java.util.Map<K,java.lang.Integer> map, K key)
          Returns the element of a map or 0
static
<K> double
getOrZeroDouble(java.util.Map<K,java.lang.Double> map, K key)
          Returns the element of a map or 0
static int indexOf(java.lang.Object o, java.lang.Object... os)
          Returns the index of a thing in an array or -1
static
<E extends java.lang.Enum<E>>
java.util.EnumSet<E>
intersection(java.util.EnumSet<E> s1, java.util.EnumSet<E> s2)
          Returns the intersection of two enumsets
static
<T> int
intersectionSize(java.util.Collection<T> c1, java.util.Collection<T> c2)
          Returns the size of the intersection
static double[] p(double[] a)
          Prints an array of doubles
static int[] p(int[] a)
          Prints an array of integers
static java.lang.Object p(java.lang.Object... a)
          Prints some Objects, returns them
static
<T> T
pick(java.util.Collection<T> set)
          Picks one element from a set or NULL
static void pl(java.lang.Object... a)
          Prints some Objects on one line
static java.lang.Object println(java.lang.Object... a)
          Prints some Objects
static java.lang.String r()
          Reads a line from the keyboard
static java.lang.String read()
          Reads a line from the keyboard
static java.lang.String read(java.lang.String question)
          Reads a long from the keyboard
static boolean readBoolean(java.lang.String question)
          Reads a long from the keyboard
static double readDouble(java.lang.String question)
          Reads a double from the keyboard
static long readLong(java.lang.String question)
          Reads a long from the keyboard
static
<K,V extends java.lang.Comparable<V>>
void
setKeyValueIfGreaterThanCurrent(java.util.Map<K,V> map, K key, V value)
          Given a map that maps to comparable objects, sets a key to a given value iff the current value is null or smaller than the given value
static void silentWriteln(java.io.Writer out, java.lang.Object s)
          Writes a line silently to a writer.
static
<C extends java.lang.Enum<C>>
boolean
smaller(java.lang.Enum<C> e1, java.lang.Enum<C> e2)
          TRUE if the first enum is before the second
static
<T> java.util.List<T>
sorted(java.util.Map<T,java.lang.Integer> map)
          Returns a sorted list of the items
static
<T> java.util.List<T>
sortedDouble(java.util.Map<T,java.lang.Double> map)
          Returns a sorted list of the items
static java.lang.String toString(java.lang.Object... o)
          Returns a reasonable String representation of a sequence of things.
static
<E extends java.lang.Enum<E>>
java.util.EnumSet<E>
union(java.util.EnumSet<E> s1, java.util.EnumSet<E> s2)
          Returns the union of two enumsets
static void waitMS(long milliseconds)
          Waits for a number of milliseconds
static void writeln(java.io.OutputStream out, java.lang.Object s)
          Writes a line to a writer.
static void writeln(java.io.Writer out, java.lang.Object s)
          Writes a line to a writer.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

indent

public static int indent
Indentation margin. All methods indent their output by indent spaces

Constructor Detail

D

public D()
Method Detail

p

public static java.lang.Object p(java.lang.Object... a)
Prints some Objects, returns them


println

public static java.lang.Object println(java.lang.Object... a)
Prints some Objects


pl

public static void pl(java.lang.Object... a)
Prints some Objects on one line


p

public static int[] p(int[] a)
Prints an array of integers


p

public static double[] p(double[] a)
Prints an array of doubles


r

public static java.lang.String r()
Reads a line from the keyboard


read

public static java.lang.String read()
Reads a line from the keyboard


read

public static java.lang.String read(java.lang.String question)
Reads a long from the keyboard


readBoolean

public static boolean readBoolean(java.lang.String question)
Reads a long from the keyboard


readLong

public static long readLong(java.lang.String question)
Reads a long from the keyboard


readDouble

public static double readDouble(java.lang.String question)
Reads a double from the keyboard


waitMS

public static void waitMS(long milliseconds)
Waits for a number of milliseconds


intersection

public static <E extends java.lang.Enum<E>> java.util.EnumSet<E> intersection(java.util.EnumSet<E> s1,
                                                                              java.util.EnumSet<E> s2)
Returns the intersection of two enumsets


union

public static <E extends java.lang.Enum<E>> java.util.EnumSet<E> union(java.util.EnumSet<E> s1,
                                                                       java.util.EnumSet<E> s2)
Returns the union of two enumsets


containsOneOf

public static <E extends java.lang.Enum<E>> boolean containsOneOf(java.util.EnumSet<E> s1,
                                                                  java.util.EnumSet<E> s2)
Tells whether the intersection is non-empty


exit

public static void exit()
Exits with error code 0


writeln

public static void writeln(java.io.Writer out,
                           java.lang.Object s)
                    throws java.io.IOException
Writes a line to a writer. Yes, this is possible

Throws:
java.io.IOException

writeln

public static void writeln(java.io.OutputStream out,
                           java.lang.Object s)
                    throws java.io.IOException
Writes a line to a writer. Yes, this is possible

Throws:
java.io.IOException

silentWriteln

public static void silentWriteln(java.io.Writer out,
                                 java.lang.Object s)
Writes a line silently to a writer.


execute

public static void execute(java.lang.String cmd,
                           java.io.File folder)
                    throws java.lang.Exception
Executes a command

Throws:
java.lang.Exception

addKeyValue

public static <K,V,C extends java.util.Collection<V>,L extends java.util.Collection> void addKeyValue(java.util.Map<K,C> map,
                                                                                                      K key,
                                                                                                      V value,
                                                                                                      java.lang.Class<L> collectionType)
Given a map that maps to collections, adds a new key/value pair or introduces the key


addKeyValues

public static <K,V,C extends java.util.Collection<V>,L extends java.util.Collection> void addKeyValues(java.util.Map<K,C> map,
                                                                                                       K key,
                                                                                                       C values,
                                                                                                       java.lang.Class<L> collectionType)
Given a map that maps to collections, adds a new key/value pair or introduces the key


addKeyValue

public static <K> void addKeyValue(java.util.Map<K,java.lang.Integer> map,
                                   K key,
                                   int value)
Given a map that maps to integers, adds a new key/value pair or increases the counter


addKeyValueFlt

public static <K> void addKeyValueFlt(java.util.Map<K,java.lang.Float> map,
                                      K key,
                                      float value)
Given a map that maps to floats, adds a new key/value pair or increases the counter


addKeyValueDbl

public static <K> void addKeyValueDbl(java.util.Map<K,java.lang.Double> map,
                                      K key,
                                      double value)
Given a map that maps to doubles, adds a new key/value pair or increases the counter


setKeyValueIfGreaterThanCurrent

public static <K,V extends java.lang.Comparable<V>> void setKeyValueIfGreaterThanCurrent(java.util.Map<K,V> map,
                                                                                         K key,
                                                                                         V value)
Given a map that maps to comparable objects, sets a key to a given value iff the current value is null or smaller than the given value


getOrZero

public static <K> int getOrZero(java.util.Map<K,java.lang.Integer> map,
                                K key)
Returns the element of a map or 0


getOrZeroDouble

public static <K> double getOrZeroDouble(java.util.Map<K,java.lang.Double> map,
                                         K key)
Returns the element of a map or 0


getOr

public static <K,V> V getOr(java.util.Map<K,V> map,
                            K key,
                            V defValue)
Returns the element of a map or a default value


sorted

public static <T> java.util.List<T> sorted(java.util.Map<T,java.lang.Integer> map)
Returns a sorted list of the items


sortedDouble

public static <T> java.util.List<T> sortedDouble(java.util.Map<T,java.lang.Double> map)
Returns a sorted list of the items


equal

public static <E> boolean equal(E s1,
                                E s2)
Returns true if two things are equal, including NULL


compare

public static <E extends java.lang.Comparable<E>> int compare(E s1,
                                                              E s2)
Compares two things, including NULL


comparePairs

public static int comparePairs(java.lang.Object... o)
Compares pairs of comparable things (a1,a2,b1,b2,...), including NULL


equalPairs

public static boolean equalPairs(java.lang.Object... o)
Compares pairs of comparable things (a1,a2,b1,b2,...) for equality, including NULL


indexOf

public static int indexOf(java.lang.Object o,
                          java.lang.Object... os)
Returns the index of a thing in an array or -1


smaller

public static <C extends java.lang.Enum<C>> boolean smaller(java.lang.Enum<C> e1,
                                                            java.lang.Enum<C> e2)
TRUE if the first enum is before the second


toString

public static java.lang.String toString(java.lang.Object... o)
Returns a reasonable String representation of a sequence of things. Handles arrays, deep arrays and NULL.


pick

public static <T> T pick(java.util.Collection<T> set)
Picks one element from a set or NULL


intersectionSize

public static <T> int intersectionSize(java.util.Collection<T> c1,
                                       java.util.Collection<T> c2)
Returns the size of the intersection