Package one.microstream
Class X
java.lang.Object
one.microstream.X
public final class X extends Object
Central class for general utility methods regarding collections, arrays and some basic general functionality that is
missing in Java like
This class uses the following sound extension of the java naming conventions:
Static methods that resemble a constructor, begin with an upper case letter. This is consistent with existing naming rules: method names begin with a lower case letter EXCEPT for constructor methods. This extension does nothing more than applying the same exception to constructur-like static methods. Resembling a constructor means: 1.) Indicating by name that a new instance is created. 2.) Always returning a new instance, without exception. No caching, no casting. For example:
notNull(Object) or ints(int...).This class uses the following sound extension of the java naming conventions:
Static methods that resemble a constructor, begin with an upper case letter. This is consistent with existing naming rules: method names begin with a lower case letter EXCEPT for constructor methods. This extension does nothing more than applying the same exception to constructur-like static methods. Resembling a constructor means: 1.) Indicating by name that a new instance is created. 2.) Always returning a new instance, without exception. No caching, no casting. For example:
empty() or asX(List) are NOT constructor-like methods
because they do not (always) create new instances.-
Method Summary
Modifier and Type Method Description static _longKeyValue_longKeyValue(long key, long value)static <T extends Throwable>
TaddSuppressed(T throwable, Throwable suppressed)As usual, the JDK developers fail to create smoothly usable methods, so one has to clean up after them.
Usage:
throw addSuppressed(new SomethingIsWrongException(), e);static <T extends Throwable>
TaddSuppressed(T throwable, Throwable... suppresseds)static <T> T[]array(T... elements)static <E> E[]Array(int length, E element)static <E> E[]Array(E element)static <E> E[]Array(Class<E> componentType, int length)static <E> E[]Array(Class<E> componentType, int length, Supplier<E> supplier)static <E> E[]Array(Class<E> componentType, int length, _intIndexedSupplier<E> supplier)static <E> E[]Array(Class<E> componentType, E element)static <E> E[]Array(Class<E> componentType, E element, int length)static <T, E extends T>
T[]Array(Class<T> type, XGettingCollection<E> collection)Static workaround for the Java typing deficiency that it is not possible to definepublic <T super E> T[] toArray(Class<T> type).static <T> T[]ArrayForElementType(T sampleInstance, int length)static <E> E[]ArrayOfSameType(E[] sampleArray)static <E> E[]ArrayOfSameType(E[] sampleArray, int length)static <E> ArrayView<E>ArrayView(E... elements)static VarStringassembleString(VarString vs, XGettingCollection<?> collection)RuntimeExceptionasUnchecked(Exception e)static <E> XList<E>asX(List<E> oldList)static <K, V> XMap<K,V>asX(Map<K,V> oldMap)static <E> XSet<E>asX(Set<E> oldSet)static boolean[]booleans(boolean... elements)static Boolean[]box(boolean... values)static Byte[]box(byte... values)static Character[]box(char... values)static Double[]box(double... values)static Float[]box(float... values)static Integer[]box(int... values)static Long[]box(long... values)static Short[]box(short... values)static ThrowBreakBREAK()static byte[]bytes(byte... elements)static char[]chars(char... elements)static voidcheck(BooleanTerm condition)Forces the passed condition to evaluate to true by throwing anErrorotherwise.static voidcheck(BooleanTerm condition, String message)Forces the passed condition to evaluate to true by throwing anErrorotherwise.static voidcheck(BooleanTerm condition, String message, int stackLevels)Forces the passed condition to evaluate to true by throwing anErrorotherwise.static intcheckArrayRange(long capacity)Central validation point for Java's current technical limitation of max int as max array capacity.static <T> Tcoalesce(T... elements)static <T> Tcoalesce(T firstElement, T secondElement)static <T> WeakReference<T>[]consolidateWeakReferences(WeakReference<T>[] array)Removes allnullentries and entries withnull-referents.static <E> Constant<E>Constant(E element)static <E> ConstHashEnum<E>ConstEnum(E... elements)static <E> ConstList<E>ConstList(E... elements)static double[]doubles(double... elements)static <T> Empty<T>empty()static <K, V> EmptyTable<K,V>emptyTable()static <E> HashEnum<E>Enum(E... elements)static <E> HashEnum<E>Enum(Iterable<? extends E> elements)static <T> booleanequal(T o1, T o2)static float[]floats(float... elements)static booleanhasNoContent(XGettingCollection<?> collection)static IllegalArgumentExceptionillegalArgument(Object object)static IndexBoundsExceptionIndexBoundsException(long startIndex, long indexBound, long index, String message, int stackTraceCutDepth)static int[]ints(int... elements)static booleanisFalse(Boolean b)Helper method to project ternary values to binary logic.
Useful for checking "really false" (not true and not unknown).static booleanisNotFalse(Boolean b)Helper method to project ternary values to binary logic.
Useful for checking "really not false" (either true or unknown).static booleanisNotNull(Object reference)static booleanisNotTrue(Boolean b)Helper method to project ternary values to binary logic.
Useful for checking "really not true" (either false or unknown).static booleanisNull(Object reference)static booleanisTrue(Boolean b)Helper method to project ternary values to binary logic.
Useful for checking "really true" (not false and not unknown).static <K, V> KeyValue<K,V>KeyValue(K key, V value)static <E> XList<E>List(E... elements)static <E> XList<E>List(Iterable<? extends E> elements)static long[]longs(long... elements)static <T> TmayNull(T object)This method is a complete dummy, simply serving as a semantical counterpart tonotNull(Object).
The use is small, but still there:
- the sourcecode is easier to read if the same structure is used next to anotNull(Object)call instead of missing method calls and comments (like "may be null" or "optional").static <E> E[]notEmpty(E[] array)static <S extends Sized>
SnotEmpty(S sized)static <T> TnotNull(T object)Transiently ensures the passed object to be notnullby either returning it in case it is notnullor throwing aNullPointerExceptionotherwise.static Object[]objects(Object... elements)static <S> Son(S subject, Consumer<? super S> logic)Abbreviation for "execute on".static XList<Integer>range(int firstValue, int lastValue)Utility method to create a list of integers fromfirstValuetolastValue.static <T> XReference<T>Reference(T object)static <P extends _intProcedure>
Prepeat(int startValue, int length, P logic)static <P extends _intProcedure>
Prepeat(int amount, P logic)static short[]shorts(short... elements)static <E> Singleton<E>Singleton(E element)static String[]strings(String... elements)static <E> XCollection<E>synchronize(XCollection<E> collection)Ensures that the returnedXCollectioninstance based on the passed collection is thread safe to use.
This normally means wrapping the passed collection in aSynchCollection, making it effectively synchronized.
If the passed collection already is thread safe (indicated by the marker interfaceThreadSafe), then the collection itself is returned without further actions.static <E> XList<E>synchronize(XList<E> list)Ensures that the returnedXListinstance based on the passed list is thread safe to use.
This normally means wrapping the passed list in aSynchList, making it effectively synchronized.
If the passed list already is thread safe (indicated by the marker interfaceThreadSafe), then the list itself is returned without further actions.static <E> XSet<E>synchronize(XSet<E> set)Ensures that the returnedXSetinstance based on the passed set is thread safe to use.
This normally means wrapping the passed set in aSynchSet, making it effectively synchronized.
If the passed set already is thread safe (indicated by the marker interfaceThreadSafe), then the set itself is returned without further actions.static <K, V> HashTable<K,V>Table(K key, V value)static <K, V> HashTable<K,V>Table(KeyValue<? extends K,? extends V>... elements)static XList<Integer>times(int n)Utility method to create a list of integers from 1 to the passednvalue.static <E> E[]toArray(Iterable<? extends E> iterable, Class<E> type)Converts anIterableinto an array.static byte[]toBytes(int value)static byte[]toBytes(long value)static <T, K, V> KeyValue<K,V>toKeyValue(T instance, Function<? super T,KeyValue<K,V>> mapper)static StringtoString(XGettingCollection<?> collection)static booleanunbox(Boolean d)static booleanunbox(Boolean d, boolean nullSubstitute)static byteunbox(Byte d)static byteunbox(Byte d, byte nullSubstitute)static charunbox(Character d)static charunbox(Character d, char nullSubstitute)static doubleunbox(Double d)static doubleunbox(Double d, double nullSubstitute)static floatunbox(Float d)static floatunbox(Float d, float nullSubstitute)static intunbox(Integer d)static int[]unbox(Integer[] array)static int[]unbox(Integer[] array, int nullReplacement)static intunbox(Integer d, int nullSubstitute)static longunbox(Long d)static long[]unbox(Long[] array)static long[]unbox(Long[] array, long nullReplacement)static longunbox(Long d, long nullSubstitute)static shortunbox(Short d)static shortunbox(Short d, short nullSubstitute)static int[]unbox(XGettingCollection<Integer> ints)static int[]unbox(XGettingCollection<Integer> ints, int nullReplacement)static <T> Tvalidate(T value, Predicate<? super T> validator)static <T, E extends Exception>
Tvalidate(T value, Predicate<? super T> validator, Function<? super T,E> exceptor)static longvalidateIndex(long availableLength, long index)static longvalidateRange(long bound, long startIndex, long length)static <T> WeakReference<T>WeakReference(T referent)static <T> WeakReference<T>[]WeakReferences(int length)static <T> WeakReference<T>[]WeakReferences(T... referents)
-
Method Details
-
empty
-
emptyTable
-
BREAK
-
checkArrayRange
Central validation point for Java's current technical limitation of max int as max array capacity. Note that because of dependencies of many types to arrays (e.g. toArray() methods, etc.), this limitation indirectly affects many other types, for example String, collections, ByteBuffers (which is extremely painful). It can be read that there are plans to overcome this outdated insufficiency for Java 9 or 10, so it's best to have one central point of validation that can later easily be refactored out.- Parameters:
capacity- the desired (array-dependent) capacity which may effectively be not greater thanInteger.MAX_VALUE.- Returns:
- the safely downcasted capacity as an int value.
- Throws:
ArrayCapacityException- if the passed capacity is greater thanInteger.MAX_VALUE
-
notNull
Transiently ensures the passed object to be notnullby either returning it in case it is notnullor throwing aNullPointerExceptionotherwise.(Really, no idea why java.util.Objects.notNull got renamed to requireNotNull after some odd objection from some guy in the mailing list that the name would be misleading. Of course "notNull" means "the thing you pass has to be not null, otherwise you'll get an exception". What else could the meaning of a transient method named "notNull" be? If "requireNotNull" is needed to express this behavior, than what would "notNull" alone mean?
In the end, "requireNotNull" is just additional clutter, hence not usable and is replaced by this, still properly named "notNull" method.)- Type Parameters:
T- the type of the object to be ensured to be notnull.- Parameters:
object- the object to be ensured to be notnull.- Returns:
- the passed object, guaranteed to be not
null. - Throws:
NullPointerException- ifnullwas passed.
-
mayNull
public static final <T> T mayNull(T object)This method is a complete dummy, simply serving as a semantical counterpart tonotNull(Object).
The use is small, but still there:
- the sourcecode is easier to read if the same structure is used next to anotNull(Object)call instead of missing method calls and comments (like "may be null" or "optional"). - the IDE can search for all occurances of this method, listing all places where something may be null.- Parameters:
object- the passed reference.- Returns:
- the passed reference without doing ANYTHING else.
-
isTrue
Helper method to project ternary values to binary logic.
Useful for checking "really true" (not false and not unknown).- Parameters:
b- aBooleanobject.- Returns:
- false if
bisnullor false
-
isFalse
Helper method to project ternary values to binary logic.
Useful for checking "really false" (not true and not unknown).- Parameters:
b- aBooleanobject.- Returns:
- false if
bisnullortrue, otherwisetrue
-
isNotTrue
Helper method to project ternary values to binary logic.
Useful for checking "really not true" (either false or unknown).- Parameters:
b- aBooleanobject.- Returns:
trueifbisnullor false, otherwise false
-
isNotFalse
Helper method to project ternary values to binary logic.
Useful for checking "really not false" (either true or unknown).- Parameters:
b- aBooleanobject.- Returns:
trueifbisnullortrue, otherwise false
-
isNull
-
isNotNull
-
coalesce
public static final <T> T coalesce(T firstElement, T secondElement) -
coalesce
-
equal
public static final <T> boolean equal(T o1, T o2) -
unbox
-
unbox
-
unbox
-
unbox
-
unbox
-
unbox
-
unbox
-
unbox
-
unbox
-
unbox
-
unbox
-
unbox
-
unbox
-
unbox
-
unbox
-
unbox
-
box
-
box
-
box
-
box
-
box
-
box
-
box
-
box
-
unbox
-
unbox
-
unbox
-
unbox
-
unbox
-
unbox
-
booleans
public static boolean[] booleans(boolean... elements) -
bytes
public static byte[] bytes(byte... elements) -
toBytes
public static byte[] toBytes(int value) -
toBytes
public static byte[] toBytes(long value) -
shorts
public static short[] shorts(short... elements) -
ints
public static int[] ints(int... elements) -
longs
public static long[] longs(long... elements) -
floats
public static float[] floats(float... elements) -
doubles
public static double[] doubles(double... elements) -
chars
public static char[] chars(char... elements) -
array
-
objects
-
strings
-
times
Utility method to create a list of integers from 1 to the passednvalue. Useful for executing a logic viaXList.iterate(Consumer)exactelyntimes.- Parameters:
n-
-
range
Utility method to create a list of integers fromfirstValuetolastValue.- Parameters:
firstValue-lastValue-
-
List
-
List
-
ConstList
- Throws:
NullPointerException
-
ArrayView
-
Singleton
-
Constant
-
Enum
-
Enum
-
ConstEnum
@SafeVarargs public static <E> ConstHashEnum<E> ConstEnum(E... elements) throws NullPointerException- Throws:
NullPointerException
-
Table
-
Table
@SafeVarargs public static <K, V> HashTable<K,V> Table(KeyValue<? extends K,? extends V>... elements) -
Reference
-
ArrayForElementType
public static final <T> T[] ArrayForElementType(T sampleInstance, int length) -
ArrayOfSameType
public static <E> E[] ArrayOfSameType(E[] sampleArray, int length) -
ArrayOfSameType
public static final <E> E[] ArrayOfSameType(E[] sampleArray) -
Array
public static <E> E[] Array(E element) -
Array
public static <E> E[] Array(int length, E element) -
Array
-
Array
-
Array
-
Array
Static workaround for the Java typing deficiency that it is not possible to definepublic <T super E> T[] toArray(Class<T> type).- Type Parameters:
E- the collection's element type.T- the component type used to create the array, possibly a super type ofE- Parameters:
collection- the collection whose elements shall be copied to the array.type- theClassrepresenting typeTat runtime.- Returns:
- a new array instance of component type
Tcontaining all elements of the passed collection.
-
Array
Instantiaties a new array instance that has a compent type defined by the passedClasscomponentType, a length as defined by the passed length value and that is filled in order with elements supplied by the passedSupplierinstance.- Parameters:
length- the length of the array to be created.componentType- the component type of the array to be created.supplier- the function supplying the instances that make up the array's elements.- Returns:
- a new array instance filled with elements provided by the passed supplier.
- See Also:
Array(Class, int, Supplier)
-
Array
-
WeakReference
-
WeakReferences
-
WeakReferences
-
consolidateWeakReferences
Removes allnullentries and entries withnull-referents.- Type Parameters:
T-- Parameters:
array-
-
asX
-
asX
-
asX
-
hasNoContent
-
notEmpty
-
notEmpty
public static final <E> E[] notEmpty(E[] array) -
toKeyValue
-
KeyValue
-
_longKeyValue
-
toString
-
assembleString
-
synchronize
Ensures that the returnedXListinstance based on the passed list is thread safe to use.
This normally means wrapping the passed list in aSynchList, making it effectively synchronized.
If the passed list already is thread safe (indicated by the marker interfaceThreadSafe), then the list itself is returned without further actions. This automatically ensures that aSynchListis not redundantly wrapped again in anotherSynchList. -
synchronize
Ensures that the returnedXSetinstance based on the passed set is thread safe to use.
This normally means wrapping the passed set in aSynchSet, making it effectively synchronized.
If the passed set already is thread safe (indicated by the marker interfaceThreadSafe), then the set itself is returned without further actions. This automatically ensures that aSynchSetis not redundantly wrapped again in anotherSynchSet. -
synchronize
Ensures that the returnedXCollectioninstance based on the passed collection is thread safe to use.
This normally means wrapping the passed collection in aSynchCollection, making it effectively synchronized.
If the passed collection already is thread safe (indicated by the marker interfaceThreadSafe), then the collection itself is returned without further actions. This automatically ensures that aSynchCollectionis not redundantly wrapped again in anotherSynchCollection.- Type Parameters:
E- the element type.- Parameters:
collection- theXCollectioninstance to be synchronized.- Returns:
- a thread safe
XCollectionusing the passed collection.
-
toArray
Converts anIterableinto an array. -
addSuppressed
As usual, the JDK developers fail to create smoothly usable methods, so one has to clean up after them.
Usage:
throw addSuppressed(new SomethingIsWrongException(), e); -
addSuppressed
-
asUnchecked
-
on
Abbreviation for "execute on". Read as "on [subject] execute [logic]" Nifty little helper logic that allows to execute custom logic on a subject instance but still return that instance. Useful for method chaining.- Parameters:
subject-logic-
-
check
Forces the passed condition to evaluate to true by throwing anErrorotherwise.- Parameters:
condition-- Throws:
Error- if the passed condition fails.
-
check
Forces the passed condition to evaluate to true by throwing anErrorotherwise.- Parameters:
condition-message-- Throws:
Error- if the passed condition fails.
-
check
Forces the passed condition to evaluate to true by throwing anErrorotherwise.- Parameters:
condition-message-stackLevels-- Throws:
Error- if the passed condition fails.
-
validate
public static <T> T validate(T value, Predicate<? super T> validator) throws IllegalArgumentException- Throws:
IllegalArgumentException
-
illegalArgument
-
validate
public static <T, E extends Exception> T validate(T value, Predicate<? super T> validator, Function<? super T,E> exceptor) throws E extends Exception- Throws:
E extends Exception
-
repeat
-
repeat
-
repeat
-
repeat
-
validateIndex
public static final long validateIndex(long availableLength, long index) throws IndexBoundsException- Throws:
IndexBoundsException
-
validateRange
public static final long validateRange(long bound, long startIndex, long length) -
IndexBoundsException
public static final IndexBoundsException IndexBoundsException(long startIndex, long indexBound, long index, String message, int stackTraceCutDepth)
-