Class Utilities
- java.lang.Object
-
- com.github.collinalpert.java2db.utilities.Utilities
-
public class Utilities extends java.lang.Object- Author:
- Collin Alpert
-
-
Constructor Summary
Constructors Constructor Description Utilities()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.util.List<TableColumnReference>getAllFields(java.lang.Class<? extends BaseEntity> instanceClass)Gets all the fields and the fields of foreign key objects in this entity.static java.util.List<TableColumnReference>getAllFields(java.lang.Class<? extends BaseEntity> instanceClass, java.lang.String alias)Gets all the fields and the fields of foreign key objects in this entity.static java.lang.StringgetColumnName(java.lang.reflect.Field field)Gets the corresponding table column name of a field.static java.util.List<java.lang.reflect.Field>getEntityFields(java.lang.Class<? extends BaseEntity> instanceClass)static java.util.List<java.lang.reflect.Field>getEntityFields(java.lang.Class<? extends BaseEntity> instanceClass, boolean includeForeignKeys)static java.util.List<java.lang.reflect.Field>getEntityFields(java.lang.Class<? extends BaseEntity> instanceClass, java.lang.Class<?> delimiter)static java.lang.StringgetTableName(java.lang.Class<?> type)Gets the database table name from theTableNameattribute on the class.static voidlog(java.lang.Object text)Prints messages to the query, while considering theDBConnection.LOG_QUERIESconstant.static voidlogf(java.lang.String text, java.lang.Object... params)Prints formatted messages to the query, while considering theDBConnection.LOG_QUERIESconstant.static java.lang.RunnablerunnableHandling(ThrowableRunnable<java.sql.SQLException> runnable, java.util.function.Consumer<java.sql.SQLException> exceptionHandling)Handles anSQLExceptionthat gets thrown inside aRunnable.static <V> java.util.function.Supplier<V>supplierHandling(ThrowableSupplier<V,java.sql.SQLException> supplier, java.util.function.Consumer<java.sql.SQLException> exceptionHandling)Handles anSQLExceptionthat gets thrown inside aSupplier.static <E extends java.lang.Throwable>
voidtryAction(ThrowableRunnable<E> runnable)Tries to perform a certain action while considering a checked exception that could occur.static <T,E extends java.lang.Throwable>
TtryGetValue(ThrowableSupplier<T,E> supplier)Tries to perform a certain supplier and retrieve its value while considering a checked exception that could occur.
-
-
-
Method Detail
-
getEntityFields
public static java.util.List<java.lang.reflect.Field> getEntityFields(java.lang.Class<? extends BaseEntity> instanceClass)
-
getEntityFields
public static java.util.List<java.lang.reflect.Field> getEntityFields(java.lang.Class<? extends BaseEntity> instanceClass, boolean includeForeignKeys)
-
getEntityFields
public static java.util.List<java.lang.reflect.Field> getEntityFields(java.lang.Class<? extends BaseEntity> instanceClass, java.lang.Class<?> delimiter)
-
getAllFields
public static java.util.List<TableColumnReference> getAllFields(java.lang.Class<? extends BaseEntity> instanceClass)
Gets all the fields and the fields of foreign key objects in this entity.- Parameters:
instanceClass- The class to get the fields from.- Returns:
- A list of columns including references to their table.
-
getAllFields
public static java.util.List<TableColumnReference> getAllFields(java.lang.Class<? extends BaseEntity> instanceClass, java.lang.String alias)
Gets all the fields and the fields of foreign key objects in this entity.- Parameters:
instanceClass- The class to get the fields from.alias- The alias that nested properties will use.- Returns:
- A list of columns including references to their table.
-
log
public static void log(java.lang.Object text)
Prints messages to the query, while considering theDBConnection.LOG_QUERIESconstant.- Parameters:
text- The message to print.
-
logf
public static void logf(java.lang.String text, java.lang.Object... params)Prints formatted messages to the query, while considering theDBConnection.LOG_QUERIESconstant.- Parameters:
text- The formatted text.params- The parameters to be inserted into the string.
-
getTableName
public static java.lang.String getTableName(java.lang.Class<?> type)
Gets the database table name from theTableNameattribute on the class. If there is no attribute, the class name in lower case characters is returned.- Parameters:
type- The entity to get the table name of.- Returns:
- The table name.
-
getColumnName
public static java.lang.String getColumnName(java.lang.reflect.Field field)
Gets the corresponding table column name of a field.- Parameters:
field- The field representing the column in a table.- Returns:
- The column name in the table.
-
supplierHandling
public static <V> java.util.function.Supplier<V> supplierHandling(ThrowableSupplier<V,java.sql.SQLException> supplier, java.util.function.Consumer<java.sql.SQLException> exceptionHandling)
Handles anSQLExceptionthat gets thrown inside aSupplier.- Type Parameters:
V- The return type of the operation.- Parameters:
supplier- TheSupplierthat throws the exception.exceptionHandling- The exception handling supplied for this exception.- Returns:
- The original
Supplierbut now with the added exception handling.
-
runnableHandling
public static java.lang.Runnable runnableHandling(ThrowableRunnable<java.sql.SQLException> runnable, java.util.function.Consumer<java.sql.SQLException> exceptionHandling)
Handles anSQLExceptionthat gets thrown inside aRunnable.- Parameters:
runnable- TheRunnablethat throws the exception.exceptionHandling- The exception handling supplied for this exception.- Returns:
- The original
Runnablebut now with the added exception handling.
-
tryAction
public static <E extends java.lang.Throwable> void tryAction(ThrowableRunnable<E> runnable)
Tries to perform a certain action while considering a checked exception that could occur.- Type Parameters:
E- The type of checked exception.- Parameters:
runnable- TheRunnableto try to execute.
-
tryGetValue
public static <T,E extends java.lang.Throwable> T tryGetValue(ThrowableSupplier<T,E> supplier)
Tries to perform a certain supplier and retrieve its value while considering a checked exception that could occur.- Type Parameters:
T- The type of value to return.E- The type of checked exception.- Parameters:
supplier- TheSupplierto try to execute.- Returns:
- The value returned by the supplier, assuming it can be excecuted without throwing an exception.
-
-