|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectbcds.tools.Utils
public class Utils
This class is a collection of utility functions. All of them must be static
Method Summary | ||
---|---|---|
static
|
average(java.util.Collection<T> c)
Returns the average of the numbers in c . |
|
static
|
devStd(T[] A)
Returns A 4-element array of doubles containing the standard deviation of the values in A as well as
complementary information. |
|
static
|
freqTableAverage(FreqTable<T> tab)
Returns the average of a numeric variable organized in a frequency table. |
|
static java.lang.String |
getProp(java.lang.String key,
java.lang.String list)
Returns the value part of a given key in a comma-separated list of key=value pairs. |
|
static void |
newBufferedSystemOut()
Creates a new buffered output around System.out and then sets that as the new System.out. |
|
static
|
newFromString(java.lang.Class<T> cls,
java.lang.String value)
Returns a new instance of class cls , using the
parameter value as the parameter to cls's
constructor. |
|
static
|
newHashMap()
Creates a new HashMap. |
|
static SimplePair<java.lang.Integer,java.lang.Boolean> |
parseIntOrPrc(java.lang.String str)
Parses an integer or an integer percentage from the given string and returns the integer and a boolean to signal if a percentage sign was found. |
|
static
|
printFreqTable(java.io.PrintStream out,
FreqTable<T> tab,
java.util.Set<T> keys,
java.lang.String msg,
boolean acc)
Prints to out a frequency table. |
|
static java.lang.String |
removePath(java.lang.String fname)
Returns the last component of a path, assuming that the component separator is the forward slash (/). |
|
static java.lang.String |
replaceExt(java.lang.String fname,
java.lang.String ext)
Replaces the "extension" of a file name. |
|
static
|
select(java.util.Collection<T> c,
int subgroup_size,
java.util.Random rnd)
Returns a subgroup taken from the elements in the collection c ,
consisting of exactly subgroup_size elements, which are
randomly selected using rnd as the source of randomness. |
|
static
|
silentCast(java.lang.Object b)
Returns the object b cast as an instance of type T ,
suppressing compilation warnings about unchecked casts. |
|
static
|
sortMap(java.util.Map<K,V> h,
java.util.Comparator<K> cmp)
Returns a list with the elements of the map h sorted
according to the criterion established by the comparator cmp . |
|
static
|
sortMapByValues(java.util.Map<K,V> h,
SortOrder order)
Returns a list with the elements of the map h
sorted, that is, the sorted map values. |
|
static java.util.List<java.lang.String> |
split(java.lang.String line,
java.lang.String delim_chars)
Splits the string line using a set of characters given
in delim_chars as component delimiter. |
|
static java.util.List<java.lang.String> |
splitOnWS(java.lang.String line)
Splits the string line using any whitespace as delimiter
and returns the resulting list. |
|
static java.lang.String |
timeStamp()
Returns the current time with the format yyyy-MM-dd HH:mm:ss. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
public static java.lang.String timeStamp()
public static <T extends java.lang.Number> double[] devStd(T[] A)
A
as well as
complementary information. The values returned are the
following, in that order:
public static java.lang.String replaceExt(java.lang.String fname, java.lang.String ext)
replaceExt("some_filename.dat", "txt") => "some_filename.txt" replaceExt("some.filename.dat", "txt") => "some.filename.txt" replaceExt("some.filename", "txt") => "some.filename"
public static java.lang.String removePath(java.lang.String fname)
removePath("some/file/name.dat", "txt") => "name.dat"
public static <T extends java.lang.Number> double average(java.util.Collection<T> c)
c
.
public static <T extends java.lang.Number> double freqTableAverage(FreqTable<T> tab)
public static <K,V extends java.lang.Comparable<? super V>> java.util.List<K> sortMapByValues(java.util.Map<K,V> h, SortOrder order)
h
sorted, that is, the sorted map values. The comparison is based
on V
's compareTo method. If multiple keys exist
with the same value, their relative ordering is undefined
(may vary from call to call).
This method calls sortMap(Map, Comparator)
to
perform the actual sorting.
h
- the map whose values will be sorted.order
- the sort order (ascending or descending).public static <K,V extends java.lang.Comparable<? super V>> java.util.List<K> sortMap(java.util.Map<K,V> h, java.util.Comparator<K> cmp)
h
sorted
according to the criterion established by the comparator cmp
.
This method calls
Collections.sort(List, Comparator)
to perform
the sorting.
h
- the hash whose values will be sorted.cmp
- the comparator to use.public static <T> T newFromString(java.lang.Class<T> cls, java.lang.String value)
cls
, using the
parameter value
as the parameter to cls's
constructor.
java.lang.IllegalArgumentException
- if cls
does not have
a constructor accepting a single string parameter, or
if the instantiation throws any error.public static <K,E> java.util.Map<K,E> newHashMap()
public static java.lang.String getProp(java.lang.String key, java.lang.String list)
For example, given
getProp("s", "a=something,b1=xyq,s=123,b2yy=xxx") => "123" getProp("maxtime", null) => "" getProp("x", "a=abcdx") => ""Case does not matter when searching for Keys. Also: a) there must not be a space between the comma and the following key name. b)the equal sign must follow immediately the key name.
public static SimplePair<java.lang.Integer,java.lang.Boolean> parseIntOrPrc(java.lang.String str)
A percentage is valid if the % sign follows immediately an integer value.
java.lang.NumberFormatException
- if str
cannot be parsed
as an integer, or if it does not represent a
percentage.public static <T> void printFreqTable(java.io.PrintStream out, FreqTable<T> tab, java.util.Set<T> keys, java.lang.String msg, boolean acc)
out
a frequency table. Elements are printed
in the order specified by keys
.
The output of the table is preceeded by msg
.
Elements are printed one per line, with the following format:
# e: count = prc"where e represents the element, count the frequency and prc is frequency(e) / total * 100, with 2 decimal digits. If
acc
is true, an additional column is added, which
is the accumulated percentage.
Note that it is assumed that keys
contains all the keys
in the table. If not, the percentages will be wrong.
public static void newBufferedSystemOut()
By default, System.out seems to use "autoflush". If a program sends several hundred thousand lines of output to System.out, making this change can greatly reduce execution time.
public static java.util.List<java.lang.String> splitOnWS(java.lang.String line)
line
using any whitespace as delimiter
and returns the resulting list.
The following are considered as whitespaces: space \t \n \f \r and . This list is taken from java.util.regex.Pattern.
public static java.util.List<java.lang.String> split(java.lang.String line, java.lang.String delim_chars)
line
using a set of characters given
in delim_chars
as component delimiter.
Contiguous delimiters are considered as a one (1) delimiter, that is,
split("abc def", " ") => (abc, def)
Delimiters embedded in a sequence of characters sorrounded by double-quotes (") are not considered as proper delimiters.
Components are trimmed, so that "abc " returns the list (abc).
java.lang.IllegalArgumentException
- if quotes are not properly closed.public static <T> java.util.List<T> select(java.util.Collection<T> c, int subgroup_size, java.util.Random rnd)
c
,
consisting of exactly subgroup_size
elements, which are
randomly selected using rnd
as the source of randomness.
If the elements in the collection c unique (non-repeated), so are the ones included in the returned subgroup. Note, however, that uniqueness is not a requiste.
java.lang.IllegalArgumentException
- if subgroup_size is larger
than the number of elements in the collection c.public static <T> T silentCast(java.lang.Object b)
b
cast as an instance of type T
,
suppressing compilation warnings about unchecked casts. Note that
at runtime this can throw a CastCastException if the
cast is invalid.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |