forked from opensearch-project/neural-search
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: xinyual <[email protected]>
- Loading branch information
Showing
3 changed files
with
55 additions
and
36 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
src/main/java/org/opensearch/neuralsearch/processor/chunker/ChunkerUtils.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,50 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.neuralsearch.processor.chunker; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.commons.lang3.math.NumberUtils; | ||
|
||
import java.util.Map; | ||
|
||
public class ChunkerUtils { | ||
public static String validateStringParameters( | ||
Map<String, Object> parameters, | ||
String fieldName, | ||
String defaultValue, | ||
boolean allowEmpty | ||
) { | ||
if (!parameters.containsKey(fieldName)) { | ||
// all parameters are optional | ||
return defaultValue; | ||
} | ||
Object fieldValue = parameters.get(fieldName); | ||
if (!(fieldValue instanceof String)) { | ||
throw new IllegalArgumentException("Chunker parameter [" + fieldName + "] cannot be cast to [" + String.class.getName() + "]"); | ||
} else if (!allowEmpty && StringUtils.isEmpty(fieldValue.toString())) { | ||
throw new IllegalArgumentException("Chunker parameter: " + fieldName + " should not be empty."); | ||
} | ||
return (String) fieldValue; | ||
} | ||
|
||
public static int validatePositiveIntegerParameter(Map<String, Object> parameters, String fieldName, int defaultValue) { | ||
// this method validate that parameter is a positive integer | ||
if (!parameters.containsKey(fieldName)) { | ||
// all parameters are optional | ||
return defaultValue; | ||
} | ||
String fieldValue = parameters.get(fieldName).toString(); | ||
if (!(NumberUtils.isParsable(fieldValue))) { | ||
throw new IllegalArgumentException( | ||
"fixed length parameter [" + fieldName + "] cannot be cast to [" + Number.class.getName() + "]" | ||
); | ||
} | ||
if (NumberUtils.createInteger(fieldValue) <= 0) { | ||
throw new IllegalArgumentException("fixed length parameter [" + fieldName + "] must be positive"); | ||
} | ||
return Integer.valueOf(fieldValue); | ||
} | ||
} |
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