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 likenotNull(Object)
orints(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()
orasX(List)
are NOT constructor-like methods because they do not (always) create new instances.
-
-
Constructor Summary
Constructors Constructor Description X()
-
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(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 <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 VarString
assembleString(VarString vs, XGettingCollection<?> collection)
RuntimeException
asUnchecked(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... booleans)
static Byte[]
box(byte... bytes)
static Character[]
box(char... chars)
static Double[]
box(double... doubles)
static Float[]
box(float... floats)
static Integer[]
box(int... ints)
static Long[]
box(long... longs)
static Short[]
box(short... shorts)
static ThrowBreak
BREAK()
static byte[]
bytes(byte... elements)
static char[]
chars(char... elements)
static void
check(BooleanTerm condition)
Forces the passed condition to evaluate to true by throwing anError
otherwise.static void
check(BooleanTerm condition, String message)
Forces the passed condition to evaluate to true by throwing anError
otherwise.static void
check(BooleanTerm condition, String message, int stackLevels)
Forces the passed condition to evaluate to true by throwing anError
otherwise.static int
checkArrayRange(long capacity)
Central validation point for Java's current technical limitation of max int as max array capacity.static <T> T
coalesce(T... elements)
static <T> T
coalesce(T firstElement, T secondElement)
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> boolean
equal(T o1, T o2)
static float[]
floats(float... elements)
static boolean
hasNoContent(XGettingCollection<?> collection)
static IllegalArgumentException
illegalArgument(Object object)
static IndexBoundsException
IndexBoundsException(long startIndex, long indexBound, long index, String message, int stackTraceCutDepth)
static int[]
ints(int... elements)
static boolean
isFalse(Boolean b)
Helper method to project ternary values to binary logic.
Useful for checking "really false" (not true and not unknown).static boolean
isNotFalse(Boolean b)
Helper method to project ternary values to binary logic.
Useful for checking "really not false" (either true or unknown).static boolean
isNotNull(Object reference)
static boolean
isNotTrue(Boolean b)
Helper method to project ternary values to binary logic.
Useful for checking "really not true" (either false or unknown).static boolean
isNull(Object reference)
static boolean
isTrue(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> 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").static <E> E[]
notEmpty(E[] array)
static <S extends Sized>
SnotEmpty(S sized)
static <T> T
notNull(T object)
Transiently ensures the passed object to be notnull
by either returning it in case it is notnull
or throwing aNullPointerException
otherwise.static Object[]
objects(Object... elements)
static <S> S
on(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 fromfirstValue
tolastValue
.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 returnedXCollection
instance 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 returnedXList
instance 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 returnedXSet
instance 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 passedn
value.static <T,K,V>
KeyValue<K,V>toKeyValue(T instance, Function<? super T,KeyValue<K,V>> mapper)
static String
toString(XGettingCollection<?> collection)
static boolean
unbox(Boolean d)
static boolean
unbox(Boolean d, boolean nullSubstitute)
static byte
unbox(Byte d)
static byte
unbox(Byte d, byte nullSubstitute)
static char
unbox(Character d)
static char
unbox(Character d, char nullSubstitute)
static double
unbox(Double d)
static double
unbox(Double d, double nullSubstitute)
static float
unbox(Float d)
static float
unbox(Float d, float nullSubstitute)
static int
unbox(Integer d)
static int[]
unbox(Integer[] array)
static int[]
unbox(Integer[] array, int nullReplacement)
static int
unbox(Integer d, int nullSubstitute)
static long
unbox(Long d)
static long[]
unbox(Long[] array)
static long[]
unbox(Long[] array, long nullReplacement)
static long
unbox(Long d, long nullSubstitute)
static short
unbox(Short d)
static short
unbox(Short d, short nullSubstitute)
static int[]
unbox(XGettingCollection<Integer> ints)
static int[]
unbox(XGettingCollection<Integer> ints, int nullReplacement)
static <T> T
validate(T value, Predicate<? super T> validator)
static <T,E extends Exception>
Tvalidate(T value, Predicate<? super T> validator, Function<? super T,E> exceptor)
static long
validateIndex(long availableLength, long index)
static long
validateRange(long availableLength, long startIndex, long length)
-
-
-
Method Detail
-
empty
public static final <T> Empty<T> empty()
-
emptyTable
public static final <K,V> EmptyTable<K,V> emptyTable()
-
BREAK
public static ThrowBreak BREAK()
-
checkArrayRange
public static final int checkArrayRange(long capacity) throws ArrayCapacityException
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
-
isTrue
public static final boolean isTrue(Boolean b)
Helper method to project ternary values to binary logic.
Useful for checking "really true" (not false and not unknown).- Parameters:
b
- aBoolean
object.- Returns:
- false if
b
isnull
or false
-
isFalse
public static final boolean isFalse(Boolean b)
Helper method to project ternary values to binary logic.
Useful for checking "really false" (not true and not unknown).- Parameters:
b
- aBoolean
object.- Returns:
- false if
b
isnull
ortrue
, otherwisetrue
-
isNotTrue
public static final boolean isNotTrue(Boolean b)
Helper method to project ternary values to binary logic.
Useful for checking "really not true" (either false or unknown).- Parameters:
b
- aBoolean
object.- Returns:
true
ifb
isnull
or false, otherwise false
-
isNotFalse
public static final boolean isNotFalse(Boolean b)
Helper method to project ternary values to binary logic.
Useful for checking "really not false" (either true or unknown).- Parameters:
b
- aBoolean
object.- Returns:
true
ifb
isnull
ortrue
, otherwise false
-
isNull
public static final boolean isNull(Object reference)
-
isNotNull
public static final boolean isNotNull(Object reference)
-
notNull
public static final <T> T notNull(T object) throws NullPointerException
Transiently ensures the passed object to be notnull
by either returning it in case it is notnull
or throwing aNullPointerException
otherwise.(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
- ifnull
was 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.
-
coalesce
public static final <T> T coalesce(T firstElement, T secondElement)
-
coalesce
@SafeVarargs public static final <T> T coalesce(T... elements)
-
equal
public static final <T> boolean equal(T o1, T o2)
-
unbox
public static final byte unbox(Byte d)
-
unbox
public static final byte unbox(Byte d, byte nullSubstitute)
-
unbox
public static final boolean unbox(Boolean d)
-
unbox
public static final boolean unbox(Boolean d, boolean nullSubstitute)
-
unbox
public static final short unbox(Short d)
-
unbox
public static final short unbox(Short d, short nullSubstitute)
-
unbox
public static final char unbox(Character d)
-
unbox
public static final int unbox(Integer d)
-
unbox
public static final int unbox(Integer d, int nullSubstitute)
-
unbox
public static final char unbox(Character d, char nullSubstitute)
-
unbox
public static final float unbox(Float d)
-
unbox
public static final float unbox(Float d, float nullSubstitute)
-
unbox
public static final long unbox(Long d)
-
unbox
public static final long unbox(Long d, long nullSubstitute)
-
unbox
public static final double unbox(Double d)
-
unbox
public static final double unbox(Double d, double nullSubstitute)
-
box
public static final Byte[] box(byte... bytes)
-
box
public static final Boolean[] box(boolean... booleans)
-
box
public static final Short[] box(short... shorts)
-
box
public static final Integer[] box(int... ints)
-
box
public static final Character[] box(char... chars)
-
box
public static final Float[] box(float... floats)
-
box
public static final Long[] box(long... longs)
-
box
public static final Double[] box(double... doubles)
-
unbox
public static final int[] unbox(Integer[] array)
-
unbox
public static final int[] unbox(Integer[] array, int nullReplacement)
-
unbox
public static final long[] unbox(Long[] array)
-
unbox
public static final long[] unbox(Long[] array, long nullReplacement)
-
unbox
public static final int[] unbox(XGettingCollection<Integer> ints)
-
unbox
public static final int[] unbox(XGettingCollection<Integer> ints, int nullReplacement)
-
booleans
public static boolean[] booleans(boolean... elements)
-
bytes
public static byte[] bytes(byte... elements)
-
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
@SafeVarargs public static <T> T[] array(T... elements)
-
times
public static XList<Integer> times(int n)
Utility method to create a list of integers from 1 to the passedn
value. Useful for executing a logic viaXList.iterate(Consumer)
exactelyn
times.- Parameters:
n
-
-
range
public static XList<Integer> range(int firstValue, int lastValue)
Utility method to create a list of integers fromfirstValue
tolastValue
.- Parameters:
firstValue
-lastValue
-
-
List
@SafeVarargs public static <E> XList<E> List(E... elements)
-
ConstList
@SafeVarargs public static <E> ConstList<E> ConstList(E... elements) throws NullPointerException
- Throws:
NullPointerException
-
ArrayView
@SafeVarargs public static <E> ArrayView<E> ArrayView(E... elements)
-
Singleton
public static <E> Singleton<E> Singleton(E element)
-
Constant
public static <E> Constant<E> Constant(E element)
-
Enum
@SafeVarargs public static <E> HashEnum<E> Enum(E... elements)
-
ConstEnum
@SafeVarargs public static <E> ConstHashEnum<E> ConstEnum(E... elements) throws NullPointerException
- Throws:
NullPointerException
-
Table
public static <K,V> HashTable<K,V> Table(K key, V value)
-
Table
@SafeVarargs public static <K,V> HashTable<K,V> Table(KeyValue<? extends K,? extends V>... elements)
-
Reference
public static <T> XReference<T> Reference(T object)
-
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(Class<E> componentType, int length)
-
Array
public 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)
.- 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
- theClass
representing typeT
at runtime.- Returns:
- a new array instance of component type
T
containing all elements of the passed collection.
-
Array
public static <E> E[] Array(Class<E> componentType, int length, Supplier<E> supplier)
Instantiaties a new array instance that has a compent type defined by the passedClass
componentType, a length as defined by the passed length value and that is filled in order with elements supplied by the passedSupplier
instance.- 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
public static <E> E[] Array(Class<E> componentType, int length, _intIndexedSupplier<E> supplier)
-
hasNoContent
public static boolean hasNoContent(XGettingCollection<?> collection)
-
notEmpty
public static final <S extends Sized> S notEmpty(S sized)
-
notEmpty
public static final <E> E[] notEmpty(E[] array)
-
toKeyValue
public static <T,K,V> KeyValue<K,V> toKeyValue(T instance, Function<? super T,KeyValue<K,V>> mapper)
-
KeyValue
public static <K,V> KeyValue<K,V> KeyValue(K key, V value)
-
_longKeyValue
public static _longKeyValue _longKeyValue(long key, long value)
-
toString
public static String toString(XGettingCollection<?> collection)
-
assembleString
public static VarString assembleString(VarString vs, XGettingCollection<?> collection)
-
synchronize
public static <E> XList<E> synchronize(XList<E> list)
Ensures that the returnedXList
instance 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 aSynchList
is not redundantly wrapped again in anotherSynchList
.
-
synchronize
public static <E> XSet<E> synchronize(XSet<E> set)
Ensures that the returnedXSet
instance 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 aSynchSet
is not redundantly wrapped again in anotherSynchSet
.
-
synchronize
public static <E> XCollection<E> synchronize(XCollection<E> collection)
Ensures that the returnedXCollection
instance 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 aSynchCollection
is not redundantly wrapped again in anotherSynchCollection
.- Type Parameters:
E
- the element type.- Parameters:
collection
- theXCollection
instance to be synchronized.- Returns:
- a thread safe
XCollection
using the passed collection.
-
addSuppressed
public static final <T extends Throwable> T addSuppressed(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);
-
addSuppressed
public static final <T extends Throwable> T addSuppressed(T throwable, Throwable... suppresseds)
-
asUnchecked
public RuntimeException asUnchecked(Exception e)
-
on
public static final <S> S on(S subject, Consumer<? super S> logic)
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
public static void check(BooleanTerm condition) throws Error
Forces the passed condition to evaluate to true by throwing anError
otherwise.- Parameters:
condition
-- Throws:
Error
- if the passed condition fails.
-
check
public static void check(BooleanTerm condition, String message) throws Error
Forces the passed condition to evaluate to true by throwing anError
otherwise.- Parameters:
condition
-message
-- Throws:
Error
- if the passed condition fails.
-
check
public static void check(BooleanTerm condition, String message, int stackLevels) throws Error
Forces the passed condition to evaluate to true by throwing anError
otherwise.- 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
public static IllegalArgumentException illegalArgument(Object object)
-
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
public static <P extends _intProcedure> P repeat(int amount, P logic)
-
repeat
public static <P extends _intProcedure> P repeat(int startValue, int length, P logic)
-
repeat
public static <P extends Runnable> P repeat(int amount, P logic)
-
repeat
public static <P extends Runnable> P repeat(int startValue, int length, P logic)
-
validateIndex
public static final long validateIndex(long availableLength, long index) throws IndexBoundsException
- Throws:
IndexBoundsException
-
validateRange
public static final long validateRange(long availableLength, long startIndex, long length)
-
IndexBoundsException
public static final IndexBoundsException IndexBoundsException(long startIndex, long indexBound, long index, String message, int stackTraceCutDepth)
-
-