-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TableDefinition column name helpers (#4813)
Adds some helper methods for on `TableDefinition#checkHasColumn`, `TableDefinition#checkHasColumns`, and `TableDefinition#getColumnNameSet`. Additionally, fixes up call sites that were (ab)using `Table#getColumnSourceMap` to simply get the keySet. This invokes a potentially extraneous Table#coalesce which can be avoided in these cases. In support of common scaffolding so #4771 won't need to call `Table#getColumnSource` for validation purposes.
- Loading branch information
1 parent
af1443c
commit e60cb54
Showing
41 changed files
with
282 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
engine/api/src/main/java/io/deephaven/engine/table/impl/NoSuchColumnException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/** | ||
* Copyright (c) 2016-2022 Deephaven Data Labs and Patent Pending | ||
*/ | ||
package io.deephaven.engine.table.impl; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Exception thrown when a column is not found. | ||
*/ | ||
public class NoSuchColumnException extends IllegalArgumentException { | ||
|
||
public static final String DELIMITER = ", "; | ||
|
||
public static final String DEFAULT_FORMAT_STR = "Unknown column names [%s], available column names are [%s]"; | ||
|
||
public enum Type { | ||
MISSING, AVAILABLE, REQUESTED | ||
} | ||
|
||
/** | ||
* Equivalent to {@code throwIf(available, Collections.singleton(requested))}. | ||
* | ||
* @param available the available columns | ||
* @param requested the requested columns | ||
* @see #throwIf(Set, Collection) | ||
*/ | ||
public static void throwIf(Set<String> available, String requested) { | ||
throwIf(available, Collections.singleton(requested)); | ||
} | ||
|
||
/** | ||
* Equivalent to {@code throwIf(available, requested, DEFAULT_FORMAT_STR, Type.MISSING, Type.AVAILABLE)} where | ||
* {@code DEFAULT_FORMAT_STR} is {@value DEFAULT_FORMAT_STR}. | ||
* | ||
* @param available the available columns | ||
* @param requested the requested columns | ||
* @see #throwIf(Set, Collection, String, Type...) | ||
*/ | ||
public static void throwIf(Set<String> available, Collection<String> requested) { | ||
throwIf(available, requested, DEFAULT_FORMAT_STR, Type.MISSING, Type.AVAILABLE); | ||
} | ||
|
||
/** | ||
* Throws a {@link NoSuchColumnException} if any name from {@code requested} is not in {@code available}. The | ||
* message will be constructed by {@link String#join(CharSequence, Iterable) joining} the respective collection with | ||
* {@value DELIMITER} and presenting them to {@link String#format(String, Object...) format} in {@code types} order. | ||
* | ||
* @param available the available columns | ||
* @param requested the requested columns | ||
* @param formatStr the format string | ||
* @param types the collection types order for formatting | ||
*/ | ||
public static void throwIf(Set<String> available, Collection<String> requested, String formatStr, Type... types) { | ||
final List<String> missing = requested | ||
.stream() | ||
.filter(Predicate.not(available::contains)) | ||
.collect(Collectors.toList()); | ||
if (!missing.isEmpty()) { | ||
final Object[] formatArgs = new Object[types.length]; | ||
for (int i = 0; i < types.length; ++i) { | ||
switch (types[i]) { | ||
case MISSING: | ||
formatArgs[i] = String.join(DELIMITER, missing); | ||
break; | ||
case AVAILABLE: | ||
formatArgs[i] = String.join(DELIMITER, available); | ||
break; | ||
case REQUESTED: | ||
formatArgs[i] = String.join(DELIMITER, requested); | ||
break; | ||
default: | ||
throw new IllegalStateException("Unexpected case " + types[i]); | ||
} | ||
} | ||
throw new NoSuchColumnException(String.format(formatStr, formatArgs)); | ||
} | ||
} | ||
|
||
/** | ||
* Thrown when an operation can not find a required column(s). | ||
* | ||
* <p> | ||
* Callers may prefer to use {@link #throwIf(Set, Collection, String, Type...)} when applicable. | ||
* | ||
* @param message the message | ||
*/ | ||
public NoSuchColumnException(String message) { | ||
super(message); | ||
} | ||
|
||
/** | ||
* Thrown when an operation can not find a required column(s). | ||
* | ||
* <p> | ||
* Callers may prefer to use {@link #throwIf(Set, Collection)} when applicable. | ||
* | ||
* @param presentColumns the column names present in the table | ||
* @param missingColumns the request column names that were not found | ||
*/ | ||
public NoSuchColumnException(Collection<String> presentColumns, Collection<String> missingColumns) { | ||
this(String.format(DEFAULT_FORMAT_STR, | ||
String.join(DELIMITER, missingColumns), | ||
String.join(DELIMITER, presentColumns))); | ||
} | ||
|
||
/** | ||
* Thrown when an operation can not find a required column. | ||
* | ||
* @param presentColumns the column names present in the table | ||
* @param missingColumn the request column name that was not found | ||
*/ | ||
public NoSuchColumnException(Collection<String> presentColumns, String missingColumn) { | ||
this(presentColumns, Collections.singleton(missingColumn)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 0 additions & 33 deletions
33
engine/table/src/main/java/io/deephaven/engine/table/impl/NoSuchColumnException.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.