Skip to content

Commit

Permalink
Fix style
Browse files Browse the repository at this point in the history
  • Loading branch information
liurenjie1024 committed Nov 21, 2024
1 parent dd338df commit d0fe14f
Show file tree
Hide file tree
Showing 66 changed files with 3,335 additions and 2,898 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/checkstyle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ jobs:
run: git fetch --all

- name: Run checkstyle
run: bash ./dev/checkstyle.sh
run: mvn checkstyle:check
8 changes: 1 addition & 7 deletions dev/checkstyle.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@

set -ex

# Assuming you are in the root of your git repository
if [[ -z "${GITHUB_BASE_REF+x}" ]]; then
MODIFIED_FILES=$(git diff --name-only)
else
MODIFIED_FILES=$(git diff --name-only "origin/${GITHUB_BASE_REF}")
fi

MODIFIED_FILES=$(git diff --name-only)

SRC_DIR="src/main/java/"
TEST_SRC_DIR="src/test/java/"
Expand Down
166 changes: 83 additions & 83 deletions dev/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
<property name="eachLine" value="true"/>
</module>

<module name="LineLength">
<property name="fileExtensions" value="java"/>
<property name="max" value="100"/>
<property name="ignorePattern" value="^package.*|^import.*|a href|href|http://|https://|ftp://"/>
</module>
<!-- <module name="LineLength">-->
<!-- <property name="fileExtensions" value="java"/>-->
<!-- <property name="max" value="100"/>-->
<!-- <property name="ignorePattern" value="^package.*|^import.*|a href|href|http://|https://|ftp://"/>-->
<!-- </module>-->

<module name="TreeWalker">
<module name="OuterTypeFilename"/>
Expand All @@ -56,21 +56,21 @@
<property name="message"
value="Consider using special escape sequence instead of octal value or Unicode escaped value."/>
</module>
<module name="AvoidEscapedUnicodeCharacters">
<property name="allowEscapesForControlCharacters" value="true"/>
<property name="allowByTailComment" value="true"/>
<property name="allowNonPrintableEscapes" value="true"/>
</module>
<!-- <module name="AvoidEscapedUnicodeCharacters">-->
<!-- <property name="allowEscapesForControlCharacters" value="true"/>-->
<!-- <property name="allowByTailComment" value="true"/>-->
<!-- <property name="allowNonPrintableEscapes" value="true"/>-->
<!-- </module>-->
<module name="AvoidStarImport"/>
<module name="OneTopLevelClass"/>
<module name="NoLineWrap">
<property name="tokens" value="PACKAGE_DEF, IMPORT, STATIC_IMPORT"/>
</module>
<module name="EmptyBlock">
<property name="option" value="TEXT"/>
<property name="tokens"
value="LITERAL_TRY, LITERAL_FINALLY, LITERAL_IF, LITERAL_ELSE, LITERAL_SWITCH"/>
</module>
<!-- <module name="EmptyBlock">-->
<!-- <property name="option" value="TEXT"/>-->
<!-- <property name="tokens"-->
<!-- value="LITERAL_TRY, LITERAL_FINALLY, LITERAL_IF, LITERAL_ELSE, LITERAL_SWITCH"/>-->
<!-- </module>-->
<module name="NeedBraces">
<property name="tokens"
value="LITERAL_DO, LITERAL_ELSE, LITERAL_FOR, LITERAL_IF, LITERAL_WHILE"/>
Expand Down Expand Up @@ -134,7 +134,7 @@
<module name="MissingSwitchDefault"/>
<module name="FallThrough"/>
<module name="UpperEll"/>
<module name="ModifierOrder"/>
<!-- <module name="ModifierOrder"/>-->
<module name="EmptyLineSeparator">
<property name="tokens"
value="PACKAGE_DEF, IMPORT, STATIC_IMPORT, CLASS_DEF, INTERFACE_DEF, ENUM_DEF,
Expand Down Expand Up @@ -180,16 +180,16 @@
<message key="name.invalidPattern"
value="Type name ''{0}'' must match pattern ''{1}''."/>
</module>
<module name="MemberName">
<property name="format" value="^[a-z][a-z0-9][a-zA-Z0-9]*$"/>
<message key="name.invalidPattern"
value="Member name ''{0}'' must match pattern ''{1}''."/>
</module>
<module name="ParameterName">
<property name="format" value="^[a-z]([a-z0-9][a-zA-Z0-9]*)?$"/>
<message key="name.invalidPattern"
value="Parameter name ''{0}'' must match pattern ''{1}''."/>
</module>
<!-- <module name="MemberName">-->
<!-- <property name="format" value="^[a-z][a-z0-9][a-zA-Z0-9]*$"/>-->
<!-- <message key="name.invalidPattern"-->
<!-- value="Member name ''{0}'' must match pattern ''{1}''."/>-->
<!-- </module>-->
<!-- <module name="ParameterName">-->
<!-- <property name="format" value="^[a-z]([a-z0-9][a-zA-Z0-9]*)?$"/>-->
<!-- <message key="name.invalidPattern"-->
<!-- value="Parameter name ''{0}'' must match pattern ''{1}''."/>-->
<!-- </module>-->
<module name="LambdaParameterName">
<property name="format" value="^[a-z]([a-z0-9][a-zA-Z0-9]*)?$"/>
<message key="name.invalidPattern"
Expand All @@ -200,11 +200,11 @@
<message key="name.invalidPattern"
value="Catch parameter name ''{0}'' must match pattern ''{1}''."/>
</module>
<module name="LocalVariableName">
<property name="format" value="^[a-z]([a-z0-9][a-zA-Z0-9]*)?$"/>
<message key="name.invalidPattern"
value="Local variable name ''{0}'' must match pattern ''{1}''."/>
</module>
<!-- <module name="LocalVariableName">-->
<!-- <property name="format" value="^[a-z]([a-z0-9][a-zA-Z0-9]*)?$"/>-->
<!-- <message key="name.invalidPattern"-->
<!-- value="Local variable name ''{0}'' must match pattern ''{1}''."/>-->
<!-- </module>-->
<module name="PatternVariableName">
<property name="format" value="^[a-z]([a-z0-9][a-zA-Z0-9]*)?$"/>
<message key="name.invalidPattern"
Expand Down Expand Up @@ -254,17 +254,17 @@
<property name="lineWrappingIndentation" value="4"/>
<property name="arrayInitIndent" value="2"/>
</module>
<module name="AbbreviationAsWordInName">
<property name="ignoreFinal" value="false"/>
<property name="allowedAbbreviationLength" value="0"/>
<property name="tokens"
value="CLASS_DEF, INTERFACE_DEF, ENUM_DEF, ANNOTATION_DEF, ANNOTATION_FIELD_DEF,
PARAMETER_DEF, VARIABLE_DEF, METHOD_DEF, PATTERN_VARIABLE_DEF, RECORD_DEF,
RECORD_COMPONENT_DEF"/>
</module>
<!-- <module name="AbbreviationAsWordInName">-->
<!-- <property name="ignoreFinal" value="false"/>-->
<!-- <property name="allowedAbbreviationLength" value="0"/>-->
<!-- <property name="tokens"-->
<!-- value="CLASS_DEF, INTERFACE_DEF, ENUM_DEF, ANNOTATION_DEF, ANNOTATION_FIELD_DEF,-->
<!-- PARAMETER_DEF, VARIABLE_DEF, METHOD_DEF, PATTERN_VARIABLE_DEF, RECORD_DEF,-->
<!-- RECORD_COMPONENT_DEF"/>-->
<!-- </module>-->
<module name="NoWhitespaceBeforeCaseDefaultColon"/>
<module name="OverloadMethodsDeclarationOrder"/>
<module name="VariableDeclarationUsageDistance"/>
<!-- <module name="OverloadMethodsDeclarationOrder"/>-->
<!-- <module name="VariableDeclarationUsageDistance"/>-->
<module name="CustomImportOrder">
<property name="sortImportsInGroupAlphabetically" value="true"/>
<property name="separateLineBetweenGroups" value="true"/>
Expand All @@ -290,13 +290,13 @@
METHOD_DEF, QUESTION, RESOURCE_SPECIFICATION, SUPER_CTOR_CALL, LAMBDA,
RECORD_DEF"/>
</module>
<module name="OperatorWrap">
<property name="option" value="NL"/>
<property name="tokens"
value="BAND, BOR, BSR, BXOR, DIV, EQUAL, GE, GT, LAND, LE, LITERAL_INSTANCEOF, LOR,
LT, MINUS, MOD, NOT_EQUAL, PLUS, QUESTION, SL, SR, STAR, METHOD_REF,
TYPE_EXTENSION_AND "/>
</module>
<!-- <module name="OperatorWrap">-->
<!-- <property name="option" value="NL"/>-->
<!-- <property name="tokens"-->
<!-- value="BAND, BOR, BSR, BXOR, DIV, EQUAL, GE, GT, LAND, LE, LITERAL_INSTANCEOF, LOR,-->
<!-- LT, MINUS, MOD, NOT_EQUAL, PLUS, QUESTION, SL, SR, STAR, METHOD_REF,-->
<!-- TYPE_EXTENSION_AND "/>-->
<!-- </module>-->
<module name="AnnotationLocation">
<property name="id" value="AnnotationLocationMostCases"/>
<property name="tokens"
Expand All @@ -308,47 +308,47 @@
<property name="tokens" value="VARIABLE_DEF"/>
<property name="allowSamelineMultipleAnnotations" value="true"/>
</module>
<module name="NonEmptyAtclauseDescription"/>
<!-- <module name="NonEmptyAtclauseDescription"/>-->
<module name="InvalidJavadocPosition"/>
<module name="JavadocTagContinuationIndentation"/>
<module name="SummaryJavadoc">
<property name="forbiddenSummaryFragments"
value="^@return the *|^This method returns |^A [{]@code [a-zA-Z0-9]+[}]( is a )"/>
</module>
<module name="JavadocParagraph"/>
<module name="RequireEmptyLineBeforeBlockTagGroup"/>
<module name="AtclauseOrder">
<property name="tagOrder" value="@param, @return, @throws, @deprecated"/>
<property name="target"
value="CLASS_DEF, INTERFACE_DEF, ENUM_DEF, METHOD_DEF, CTOR_DEF, VARIABLE_DEF"/>
</module>
<module name="JavadocMethod">
<property name="accessModifiers" value="public"/>
<property name="allowMissingParamTags" value="true"/>
<property name="allowMissingReturnTag" value="true"/>
<property name="allowedAnnotations" value="Override, Test"/>
<property name="tokens" value="METHOD_DEF, CTOR_DEF, ANNOTATION_FIELD_DEF, COMPACT_CTOR_DEF"/>
</module>
<module name="MissingJavadocMethod">
<property name="scope" value="public"/>
<property name="minLineCount" value="2"/>
<property name="allowedAnnotations" value="Override, Test"/>
<property name="tokens" value="METHOD_DEF, CTOR_DEF, ANNOTATION_FIELD_DEF,
COMPACT_CTOR_DEF"/>
</module>
<module name="MissingJavadocType">
<property name="scope" value="protected"/>
<property name="tokens"
value="CLASS_DEF, INTERFACE_DEF, ENUM_DEF,
RECORD_DEF, ANNOTATION_DEF"/>
<property name="excludeScope" value="nothing"/>
</module>
<!-- <module name="JavadocTagContinuationIndentation"/>-->
<!-- <module name="SummaryJavadoc">-->
<!-- <property name="forbiddenSummaryFragments"-->
<!-- value="^@return the *|^This method returns |^A [{]@code [a-zA-Z0-9]+[}]( is a )"/>-->
<!-- </module>-->
<!-- <module name="JavadocParagraph"/>-->
<!-- <module name="RequireEmptyLineBeforeBlockTagGroup"/>-->
<!-- <module name="AtclauseOrder">-->
<!-- <property name="tagOrder" value="@param, @return, @throws, @deprecated"/>-->
<!-- <property name="target"-->
<!-- value="CLASS_DEF, INTERFACE_DEF, ENUM_DEF, METHOD_DEF, CTOR_DEF, VARIABLE_DEF"/>-->
<!-- </module>-->
<!-- <module name="JavadocMethod">-->
<!-- <property name="accessModifiers" value="public"/>-->
<!-- <property name="allowMissingParamTags" value="true"/>-->
<!-- <property name="allowMissingReturnTag" value="true"/>-->
<!-- <property name="allowedAnnotations" value="Override, Test"/>-->
<!-- <property name="tokens" value="METHOD_DEF, CTOR_DEF, ANNOTATION_FIELD_DEF, COMPACT_CTOR_DEF"/>-->
<!-- </module>-->
<!-- <module name="MissingJavadocMethod">-->
<!-- <property name="scope" value="public"/>-->
<!-- <property name="minLineCount" value="2"/>-->
<!-- <property name="allowedAnnotations" value="Override, Test"/>-->
<!-- <property name="tokens" value="METHOD_DEF, CTOR_DEF, ANNOTATION_FIELD_DEF,-->
<!-- COMPACT_CTOR_DEF"/>-->
<!-- </module>-->
<!-- <module name="MissingJavadocType">-->
<!-- <property name="scope" value="protected"/>-->
<!-- <property name="tokens"-->
<!-- value="CLASS_DEF, INTERFACE_DEF, ENUM_DEF,-->
<!-- RECORD_DEF, ANNOTATION_DEF"/>-->
<!-- <property name="excludeScope" value="nothing"/>-->
<!-- </module>-->
<module name="MethodName">
<property name="format" value="^[a-z][a-z0-9][a-zA-Z0-9_]*$"/>
<message key="name.invalidPattern"
value="Method name ''{0}'' must match pattern ''{1}''."/>
</module>
<module name="SingleLineJavadoc"/>
<!-- <module name="SingleLineJavadoc"/>-->
<module name="EmptyCatchBlock">
<property name="exceptionVariableName" value="expected"/>
</module>
Expand Down
125 changes: 64 additions & 61 deletions src/main/java/com/nvidia/spark/rapids/jni/Arms.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,77 +25,80 @@
* This class contains utility methods for automatic resource management.
*/
public class Arms {
/**
* This method close the resource if an exception is thrown while executing the function.
*/
public static <R extends AutoCloseable, T> T closeIfException(R resource, Function<R, T> function) {
/**
* This method close the resource if an exception is thrown while executing the function.
*/
public static <R extends AutoCloseable, T> T closeIfException(R resource,
Function<R, T> function) {
try {
return function.apply(resource);
} catch (Exception e) {
if (resource != null) {
try {
return function.apply(resource);
} catch (Exception e) {
if (resource != null) {
try {
resource.close();
} catch (Exception inner) {
e.addSuppressed(inner);
}
}
throw e;
resource.close();
} catch (Exception inner) {
e.addSuppressed(inner);
}
}
throw e;
}
}

/**
* This method safely closes all the resources.
* <p>
* This method will iterate through all the resources and closes them. If any exception happened during the
* traversal, exception will be captured and rethrown after all resources closed.
* </p>
*/
public static <R extends AutoCloseable> void closeAll(Iterator<R> resources) {
Throwable t = null;
while (resources.hasNext()) {
try {
R resource = resources.next();
if (resource != null) {
resource.close();
}
} catch (Exception e) {
if (t == null) {
t = e;
} else {
t.addSuppressed(e);
}
}
/**
* This method safely closes all the resources.
* <p>
* This method will iterate through all the resources and closes them. If any exception happened during the
* traversal, exception will be captured and rethrown after all resources closed.
* </p>
*/
public static <R extends AutoCloseable> void closeAll(Iterator<R> resources) {
Throwable t = null;
while (resources.hasNext()) {
try {
R resource = resources.next();
if (resource != null) {
resource.close();
}
} catch (Exception e) {
if (t == null) {
t = e;
} else {
t.addSuppressed(e);
}
}
}

if (t != null) throw new RuntimeException(t);
if (t != null) {
throw new RuntimeException(t);
}
}


/**
* This method safely closes all the resources. See {@link #closeAll(Iterator)} for more details.
*/
public static <R extends AutoCloseable> void closeAll(R... resources) {
closeAll(Arrays.asList(resources));
}
/**
* This method safely closes all the resources. See {@link #closeAll(Iterator)} for more details.
*/
public static <R extends AutoCloseable> void closeAll(R... resources) {
closeAll(Arrays.asList(resources));
}

/**
* This method safely closes the resources. See {@link #closeAll(Iterator)} for more details.
*/
public static <R extends AutoCloseable> void closeAll(Collection<R> resources) {
closeAll(resources.iterator());
}
/**
* This method safely closes the resources. See {@link #closeAll(Iterator)} for more details.
*/
public static <R extends AutoCloseable> void closeAll(Collection<R> resources) {
closeAll(resources.iterator());
}

/**
* This method safely closes the resources after applying the function.
* <br/>
* See {@link #closeAll(Iterator)} for more details.
*/
public static <R extends AutoCloseable, C extends Collection<R>, V> V withResource(
C resource, Function<C, V> function) {
try {
return function.apply(resource);
} finally {
closeAll(resource);
}
/**
* This method safely closes the resources after applying the function.
* <br/>
* See {@link #closeAll(Iterator)} for more details.
*/
public static <R extends AutoCloseable, C extends Collection<R>, V> V withResource(
C resource, Function<C, V> function) {
try {
return function.apply(resource);
} finally {
closeAll(resource);
}
}
}
Loading

0 comments on commit d0fe14f

Please sign in to comment.