|
Orbital library | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object orbital.util.Utility
public final class Utility
A general Utility class containing static methods for common tasks like assertion checking etc.
MathUtilities
,
Setops
,
UIUtilities
,
ClassicalLogic.Utilities
Field Summary | |
---|---|
static BinaryPredicate |
instanceOf
Predicate for x instanceof y. |
Method Summary | |
---|---|
static java.util.Collection |
asCollection(java.lang.Object o)
Get a collection view of the object o. |
static java.util.Iterator |
asIterator(java.lang.Object a)
Get an iterator view of a generalized iteratable object, if possible. |
static int |
compare(java.lang.Object a,
java.lang.Object b)
Compares its two arguments for order. |
static boolean |
containsIdenticalTo(java.lang.Object[] a,
java.lang.Object x)
Check whether an array contains an object that is identical to x. |
static int[] |
dimensions(java.lang.Object a)
Get the dimensions of a multi-dimensional array. |
static int[] |
dimensions(java.lang.Object[] a)
|
static boolean |
equals(java.lang.Object a,
java.lang.Object b)
Check whether two objects are equal. |
static boolean |
equalsAll(java.lang.Object a,
java.lang.Object b)
Check whether two objects are equal (and their array components, as well). |
static boolean |
flip(java.util.Random r,
double probability)
Flips to true with a given probability. |
static boolean |
forallMembers(java.lang.Object o,
Predicate pred)
Applies a Predicate to all member fields of an object, as long as the predicate returns true. |
static boolean |
forallPublicMembers(java.lang.Object o,
Predicate pred)
Applies a Predicate to all public member fields of an object, as long as the predicate returns true. |
static java.lang.String |
format(java.lang.String delimiter,
java.lang.Object iteratable)
Format a generalized iteratable
inserting delimiters between the individual elements. |
static java.lang.Object |
getPart(java.lang.Object[] a,
int[] partSpecification)
|
static java.lang.Object |
getPart(java.lang.Object a,
int[] partSpecification)
Get the element in the (possibly multi-dimensional) array a specified by the part specification. |
static int |
hashCode(java.lang.Object a)
Get the hashCode of an object. |
static int |
hashCodeAll(java.lang.Object o)
Get the hashCode of an object (considering all its array components, as well). |
static int |
hashCodeArray(java.lang.Object[] a)
Get the hashCode of a one-dimensional array. |
static int |
hashCodeSet(java.lang.Object o)
Get the hashCode of an object (considering all its array components, as sets, as well). |
static boolean |
isIteratable(java.lang.Object a)
Checks whether the given object is generalized iteratable. |
static int |
matchingBrace(java.lang.String text,
int pos)
Utility returning the index of the brace matching the brace at pos in text. |
static java.lang.Object |
newIteratableLike(java.lang.Object a)
Get a new instance of generalized iteratable object of the same type as the one specified. |
static void |
pre(boolean test,
java.lang.String description)
Call to make a precondition assertion. |
static int |
rank(java.lang.Object a)
Get the number of indices required to reach the component type. |
static int |
rank(java.lang.Object[] a)
|
static java.lang.String |
readLine(java.io.InputStream is)
Read a line from an InputStream in human-readable-form as a String. |
static java.lang.String |
readLine(java.io.Reader rd)
Read a line from an InputStream in human-readable-form as a String. |
static void |
setPart(java.lang.Object[] a,
int[] partSpecification,
java.lang.Object value)
|
static void |
setPart(java.lang.Object a,
int[] partSpecification,
java.lang.Object value)
Set the element in the (possibly multi-dimensional) array a specified by the part specification. |
static boolean |
sorted(double[] a,
boolean asc)
Checks whether an array is sorted. |
static boolean |
sorted(java.util.List a,
java.util.Comparator cmp)
Checks whether a list is sorted. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final BinaryPredicate instanceOf
Class.isInstance(Object)
Method Detail |
---|
public static void pre(boolean test, java.lang.String description)
Can be used to check parameter restrictions of a method. It will throw an IllegalArgumentException for invalid arguments.
Utility.pre(0<=x && x<=maximum_size, "numbers in range"); // can be used instead of if (x<0 || x>maximum_size) throw new IllegalArgumentException("number should not exceed range");
Note that per general contract of argument checking in Java this assertion will be performed even if all other assertion checks are set to be skipped.
test
- assertion predicate expression.
If this logical expression evaluates to true
, the assertion passes.
If it evaluates to false
, the assertion fails.description
- specifies additional information for the assertion that will appear in an exception.
java.lang.IllegalArgumentException
- if test evaluates to false
.public static final boolean containsIdenticalTo(java.lang.Object[] a, java.lang.Object x)
public static final boolean equals(java.lang.Object a, java.lang.Object b)
This method is just a shortcut to simplify assertion statements.
hashCode(Object)
public static final boolean equalsAll(java.lang.Object a, java.lang.Object b)
hashCodeAll(Object)
public static final int hashCode(java.lang.Object a)
This method is just a shortcut to simplify assertion statements.
equals(Object, Object)
public static final int compare(java.lang.Object a, java.lang.Object b)
Compares two objects with null
being the smallest object.
Contrary to the specified NullPointerException behaviour f Comparable.compareTo(Object)
!
equals(Object, Object)
public static final int hashCodeAll(java.lang.Object o)
hashCodeSet(Object)
, the order of elements plays a role.
o
- the object whose hashCode to calculate.
equalsAll(Object, Object)
,
Arrays.equals(Object[], Object[])
,
Functionals.foldRight(orbital.logic.functor.BinaryFunction, Object, Object[])
List
, nor, of course, Set
.public static final int hashCodeArray(java.lang.Object[] a)
hashCodeAll(Object)
, does not descend recursively.
a
- the object whose hashCode to calculate.
public static final int hashCodeSet(java.lang.Object o)
hashCodeAll(Object)
, the order of elements plays no role.
o
- the object whose hashCode to calculate.
equalsAll(Object, Object)
,
Arrays.equals(Object[], Object[])
,
Functionals.foldRight(orbital.logic.functor.BinaryFunction, Object, Object[])
Set
, nor, of cours,e List
.public static final boolean sorted(double[] a, boolean asc)
a
- the array to be checked.asc
- whether to check for ascending order (true
),
or descending order (false
).
public static final boolean sorted(java.util.List a, java.util.Comparator cmp)
a
- the list to be checked.cmp
- the comparator used (for ascending order).
If null
, natural order is used (by using Comparable
).
ReverseComparator
public static final int rank(java.lang.Object[] a)
rank(Object)
public static final int rank(java.lang.Object a)
public static final int[] dimensions(java.lang.Object[] a)
dimensions(Object)
public static final int[] dimensions(java.lang.Object a)
public static java.lang.Object getPart(java.lang.Object[] a, int[] partSpecification)
getPart(Object,int[])
public static java.lang.Object getPart(java.lang.Object a, int[] partSpecification)
a
specified by the part specification.
Contrary to getPart(Object[],int[])
, also accepts primitive type arrays.
partSpecification
- the part specification p
(multi-index into the multi-dimensional array a
).
public static void setPart(java.lang.Object[] a, int[] partSpecification, java.lang.Object value)
setPart(Object[],int[],Object)
public static void setPart(java.lang.Object a, int[] partSpecification, java.lang.Object value)
a
specified by the part specification.
Contrary to setPart(Object[],int[],Object)
, also accepts primitive type arrays.
Sets a[p[0]][p[1]]...[p[p.length-1]] := value.
partSpecification
- the part specification p
(multi-index into the multi-dimensional array a
).public static boolean flip(java.util.Random r, double probability)
probability
- the probabilty ranging from 0 to 1 with that flip returns true
.
true
with a given probability (nondeterministic).Random.nextDouble()
public static int matchingBrace(java.lang.String text, int pos)
public static boolean forallPublicMembers(java.lang.Object o, Predicate pred) throws java.lang.IllegalAccessException
o
- the object whose member public fields are desired.pred
- the predicate to be applied for every public member field.
java.lang.IllegalAccessException
public static boolean forallMembers(java.lang.Object o, Predicate pred) throws java.lang.IllegalAccessException
java.lang.reflect.ReflectPermission
of suppressAccessChecks
to work for all superclasses.
o
- the object whose member fields are desired.pred
- the predicate to be applied for every member field.
java.lang.SecurityException
- if the java.lang.reflect.ReflectPermission
of suppressAccessChecks
is not permitted.
java.lang.IllegalAccessException
public static java.util.Collection asCollection(java.lang.Object o)
asIterator(Object)
public static boolean isIteratable(java.lang.Object a)
Generalized iteratable objects are objects that somehow support iteration of their components,
like Iterator
, Collection
, Object[]
,
or Tensor
(including Vector
, and Matrix
),
as well as (even multi-dimensional) primitive type arrays.
Multi-dimensional arrays of primitive or structured component type are iterated
over component-wise. Although the order will usually be row-wise, this is not a
strict requirement.
Unfortunately, these classes do not implement a common interface "Iteratable", or
"Iterable", or "Enumerable" so we must rely on implicit interfaces.
Additionally, some iteratable classes may support ListIterator
s
others Iterator
s, so covariant return-types would be required for
defining such a common interface in a convenient way that avoids casting.
public static java.util.Iterator asIterator(java.lang.Object a) throws NotIteratableException
NotIteratableException
- if a is not generalized iteratable.isIteratable(Object)
,
asCollection(Object)
,
newIteratableLike(Object)
public static java.lang.Object newIteratableLike(java.lang.Object a) throws NotIteratableException
NotIteratableException
- if a is not generalized iteratable.isIteratable(Object)
,
Setops.newCollectionLike(Collection)
public static final java.lang.String format(java.lang.String delimiter, java.lang.Object iteratable)
generalized iteratable
inserting delimiters between the individual elements.
delimiter
- the delimiter string to insert between
successive elements of iteratable.
NotIteratableException
- if a is not generalized iteratable.public static java.lang.String readLine(java.io.InputStream is) throws java.io.IOException
java.io.IOException
readLine(java.io.InputStream)
public static java.lang.String readLine(java.io.Reader rd) throws java.io.IOException
Prefer using
BufferedReader rd = new BufferedReader(inner_reader); // forget about using the inner reader since rd is buffered! inner_reader = null; String l = rd.readLine();whenever possible.
java.io.IOException
BufferedReader.readLine()
|
Orbital library 1.3.0: 11 Apr 2009 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |