-
-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create an utility class for annotation features
Signed-off-by: Hakan <ozler.hakan@gmail.com>
- Loading branch information
1 parent
2abca12
commit 2f442d1
Showing
5 changed files
with
56 additions
and
45 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.poiji.util; | ||
|
||
import com.poiji.annotation.ExcelCellName; | ||
import com.poiji.exception.HeaderMissingException; | ||
import com.poiji.option.PoijiOptions; | ||
|
||
import java.util.Collection; | ||
import java.util.Set; | ||
import java.util.function.BiPredicate; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Created by hakan on 2.05.2020 | ||
*/ | ||
public class AnnotationUtil { | ||
|
||
|
||
/** | ||
* Validate that all headers specified via @ExcelCellName annotations are present in the list of header names. | ||
* <p> | ||
* Validation is only performed if it is set in the PoijiOptions | ||
* | ||
* @throws HeaderMissingException if one or more headers are missing | ||
*/ | ||
public static <T> void validateMandatoryNameColumns(PoijiOptions options, | ||
Class<T> modelType, | ||
Collection<String> headerNames) { | ||
if (options.getNamedHeaderMandatory()) { | ||
Collection<ExcelCellName> excelCellNames = ReflectUtil.findRecursivePoijiAnnotations(modelType, | ||
ExcelCellName.class); | ||
|
||
BiPredicate<String, String> comparator = options.getCaseInsensitive() | ||
? String::equalsIgnoreCase | ||
: String::equals; | ||
|
||
Set<String> missingHeaders = excelCellNames.stream() | ||
.filter(excelCellName -> headerNames.stream() | ||
.noneMatch(title -> comparator.test(excelCellName.value(), title))) | ||
.map(ExcelCellName::value) | ||
.collect(Collectors.toSet()); | ||
|
||
if (!missingHeaders.isEmpty()) { | ||
throw new HeaderMissingException("Some headers are missing in the sheet: " + missingHeaders); | ||
} | ||
} | ||
} | ||
} |