Skip to content

Commit

Permalink
include column name in prefix for checkCastTo instead
Browse files Browse the repository at this point in the history
  • Loading branch information
rbasralian committed Sep 18, 2024
1 parent b9dafb9 commit dc184e2
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,34 @@ default void exportAllTo(final Object @NotNull [] dest, @NotNull final T tuple)
*/
@FinalDefault
default <TYPE> ColumnSource<TYPE> cast(Class<? extends TYPE> clazz) {
return cast(clazz, (String) null);
}

/**
* Returns this {@code ColumnSource}, parameterized by {@code <TYPE>}, if the data type of this column (as given by
* {@link #getType()}) can be cast to {@code clazz}. This is analogous to casting the objects provided by this
* column source to {@code clazz}.
* <p>
* For example, the following code will throw an exception if the "MyString" column does not actually contain
* {@code String} data:
*
* <pre>
* ColumnSource&lt;String&gt; colSource = table.getColumnSource("MyString").cast(String.class)
* </pre>
* <p>
* Due to the nature of type erasure, the JVM will still insert an additional cast to {@code TYPE} when elements are
* retrieved from the column source, such as with {@code String myStr = colSource.get(0)}.
*
* @param clazz The target type.
* @param <TYPE> The target type, as a type parameter. Intended to be inferred from {@code clazz}.
* @param colName An optional column name, which will be included in exception messages.
* @return A {@code ColumnSource} parameterized by {@code TYPE}.
*/
@FinalDefault
default <TYPE> ColumnSource<TYPE> cast(Class<? extends TYPE> clazz, @Nullable String colName) {
Require.neqNull(clazz, "clazz");
TypeHelper.checkCastTo("ColumnSource", getType(), clazz);
final String castCheckPrefix = colName == null ? "ColumnSource" : "ColumnSource[" + colName + ']';
TypeHelper.checkCastTo(castCheckPrefix, getType(), clazz);
// noinspection unchecked
return (ColumnSource<TYPE>) this;
}
Expand Down Expand Up @@ -208,8 +234,38 @@ default <TYPE> ColumnSource<TYPE> cast(Class<? extends TYPE> clazz) {
*/
@FinalDefault
default <TYPE> ColumnSource<TYPE> cast(Class<? extends TYPE> clazz, @Nullable Class<?> componentType) {
return cast(clazz, componentType, null);
}

/**
* Returns this {@code ColumnSource}, parameterized by {@code <TYPE>}, if the data type of this column (as given by
* {@link #getType()}) can be cast to {@code clazz}. This is analogous to casting the objects provided by this
* column source to {@code clazz}. Additionally, this checks that the component type of this column (as given by
* {@link #getComponentType()}) can be cast to {@code componentType} (both must be present and castable, or both
* must be {@code null}).
*
* <p>
* For example, the following code will throw an exception if the "MyString" column does not actually contain
* {@code String} data:
*
* <pre>
* ColumnSource&lt;String&gt; colSource = table.getColumnSource("MyString").cast(String.class, null)
* </pre>
* <p>
* Due to the nature of type erasure, the JVM will still insert an additional cast to {@code TYPE} when elements are
* retrieved from the column source, such as with {@code String myStr = colSource.get(0)}.
*
* @param clazz The target type.
* @param componentType The target component type, may be {@code null}.
* @param colName An optional column name, which will be included in exception messages.
* @param <TYPE> The target type, as a type parameter. Intended to be inferred from {@code clazz}.
* @return A {@code ColumnSource} parameterized by {@code TYPE}.
*/
@FinalDefault
default <TYPE> ColumnSource<TYPE> cast(Class<? extends TYPE> clazz, @Nullable Class<?> componentType, @Nullable String colName) {
Require.neqNull(clazz, "clazz");
TypeHelper.checkCastTo("ColumnSource", getType(), getComponentType(), clazz, componentType);
final String castCheckPrefix = colName == null ? "ColumnSource" : "ColumnSource[" + colName + ']';
TypeHelper.checkCastTo(castCheckPrefix, getType(), getComponentType(), clazz, componentType);
// noinspection unchecked
return (ColumnSource<TYPE>) this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ default <T> ColumnSource<T> getColumnSource(String sourceName, Class<? extends T
@SuppressWarnings("rawtypes")
ColumnSource rawColumnSource = getColumnSource(sourceName);
// noinspection unchecked
return rawColumnSource.cast(clazz);
return rawColumnSource.cast(clazz, sourceName);
}

@Override
Expand All @@ -108,13 +108,8 @@ default <T> ColumnSource<T> getColumnSource(String sourceName, Class<? extends T
@Nullable Class<?> componentType) {
@SuppressWarnings("rawtypes")
ColumnSource rawColumnSource = getColumnSource(sourceName);
try {
// noinspection unchecked
return rawColumnSource.cast(clazz, componentType);
} catch (ClassCastException ex) {
throw new RuntimeException(
"Error retrieving ColumnSource with type " + clazz.getName() + " for column " + sourceName, ex);
}
// noinspection unchecked
return rawColumnSource.cast(clazz, componentType, sourceName);
}

// -----------------------------------------------------------------------------------------------------------------
Expand Down

0 comments on commit dc184e2

Please sign in to comment.