Skip to content

Commit

Permalink
♻️ Replace String.trim() with String.strip()
Browse files Browse the repository at this point in the history
  • Loading branch information
ujibang committed Apr 12, 2023
1 parent 77c19af commit 8b3f449
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ private static Configuration build(Reader confReader, Path confFilePath, Path co
} else if (confOverridesFilePath.toString().toLowerCase().endsWith(".conf")) {
// RHO format
overrides = Files.readAllLines(confOverridesFilePath).stream()
.filter(row -> !row.trim().startsWith("#")) // ingore comments lines
.filter(row -> !row.strip().startsWith("#")) // ingore comments lines
.collect(Collectors.joining());
} else {
throw new ConfigurationException("Configuration override file must have .json, .jsonc, .yml, .yaml or .conf extension: " + confOverridesFilePath);
Expand Down Expand Up @@ -455,7 +455,7 @@ private static void createParents(JXPathContext ctx, String path) {
createParents(ctx, parentPath);
}

var array = path.trim().endsWith("]");
var array = path.strip().endsWith("]");

if (array) {
// /a/b[2] -> /a/b
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private BsonArray parseCsv(CsvRequestParams params, String csv) throws IOExcepti

if (_v != null) {
// quote empty string
if ("".equals(_v.trim())) {
if ("".equals(_v.strip())) {
_v = "\"".concat(_v).concat("\"");
}

Expand All @@ -144,7 +144,7 @@ private BsonArray parseCsv(CsvRequestParams params, String csv) throws IOExcepti
var _v = vals.get(idx);

// quote empty string
if ("".equals(_v.trim())) {
if ("".equals(_v.strip())) {
_v = "\"".concat(_v).concat("\"");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ public BsonDocument getHintDocument() throws JsonParseException {
} else {
var ret = new BsonDocument();
hint.stream().forEach(s -> {
var _s = s.trim(); // the + sign is decoded into a space, in case remove it
var _s = s.strip(); // the + sign is decoded into a space, in case remove it

// manage the case where hint is a json object
try {
Expand Down
2 changes: 1 addition & 1 deletion commons/src/main/java/org/restheart/utils/BsonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ public static boolean checkType(Optional<BsonValue> o, String type) {
return false;
}

return switch (type.toLowerCase().trim()) {
return switch (type.toLowerCase().strip()) {
case "null" -> !o.isPresent();
case "notnull" -> o.isPresent();
case "object" -> o.get().isDocument();
Expand Down
8 changes: 4 additions & 4 deletions commons/src/main/java/org/restheart/utils/MetricsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static String xffValue(String xff, int rindex) {
if (xff == null) {
return null;
} else {
xff = xff.trim();
xff = xff.strip();

if (xff.startsWith("[")) {
xff = xff.substring(1);
Expand All @@ -91,12 +91,12 @@ public static String xffValue(String xff, int rindex) {

if (elements.length >= rindex) {
if (rindex >= elements.length) {
return elements[0].trim();
return elements[0].strip();
} else {
return elements[elements.length - 1 - rindex].trim();
return elements[elements.length - 1 - rindex].strip();
}
} else {
return elements[elements.length - 1].trim();
return elements[elements.length - 1].strip();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion commons/src/main/java/org/restheart/utils/URLUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ static public String removeTrailingSlashes(String s) {
return null;
}

s = s.trim();
s = s.strip();

if (s.length() < 2) {
return s;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private static long getTxnNumFromExc(MongoQueryException mqe) {
int end = mqe.getErrorMessage().indexOf(TXT_NUM_ERROR_MSG_SUFFIX_STARTED);

if (start >= 0 && end >= 0) {
String numStr = mqe.getErrorMessage().substring(start, end).trim();
String numStr = mqe.getErrorMessage().substring(start, end).strip();
numStr = removeWithTxnNumber(numStr);

return Long.parseLong(numStr);
Expand All @@ -138,7 +138,7 @@ private static long getTxnNumFromExc(MongoQueryException mqe) {
int end = mqe.getErrorMessage().indexOf(TXT_NUM_ERROR_MSG_SUFFIX_ABORTED);

if (start >= 0 && end >= 0) {
String numStr = mqe.getErrorMessage().substring(start, end).trim();
String numStr = mqe.getErrorMessage().substring(start, end).strip();
numStr = removeWithTxnNumber(numStr);

return Long.parseLong(numStr);
Expand All @@ -148,7 +148,7 @@ private static long getTxnNumFromExc(MongoQueryException mqe) {
end = mqe.getErrorMessage().indexOf(TXT_NUM_ERROR_MSG_SUFFIX_NONE);

if (start >= 0 && end >= 0) {
String numStr = mqe.getErrorMessage().substring(start, end).trim();
String numStr = mqe.getErrorMessage().substring(start, end).strip();
numStr = removeWithTxnNumber(numStr);

return Long.parseLong(numStr);
Expand All @@ -158,7 +158,7 @@ private static long getTxnNumFromExc(MongoQueryException mqe) {
int end = mqe.getErrorMessage().indexOf(TXT_NUM_ERROR_MSG_SUFFIX_COMMITTED);

if (start >= 0 && end >= 0) {
String numStr = mqe.getErrorMessage().substring(start, end).trim();
String numStr = mqe.getErrorMessage().substring(start, end).strip();
numStr = removeWithTxnNumber(numStr);

return Long.parseLong(numStr);
Expand Down Expand Up @@ -187,7 +187,7 @@ static String removeWithTxnNumber(String errorMsg) {
}

if (errorMsg.contains(TXN)) {
var t = errorMsg.substring(errorMsg.lastIndexOf(TXN) + TXN.length()).trim();
var t = errorMsg.substring(errorMsg.lastIndexOf(TXN) + TXN.length()).strip();

if (t.indexOf(":") >= 0) {
t = t.substring(t.indexOf(":")+1);
Expand All @@ -197,7 +197,7 @@ static String removeWithTxnNumber(String errorMsg) {
var comma = t.indexOf(",");

if (closingBracket > 0 || comma > 0) {
return t.substring(0, comma > 0 ? comma : closingBracket).trim();
return t.substring(0, comma > 0 ? comma : closingBracket).strip();
} else {
return t;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static void inject(HttpServerExchange exchange) {

if (_rep != null && !_rep.isEmpty()) {
try {
rep = REPRESENTATION_FORMAT.valueOf(_rep.trim().toUpperCase());
rep = REPRESENTATION_FORMAT.valueOf(_rep.strip().toUpperCase());
} catch (IllegalArgumentException iae) {
response.addWarning("illegal rep parameter " + _rep + " (must be STANDARD, NESTED or HAL; S is an alias for STANDARD; PLAIN_JSON, PJ are aliases for NESTED)");
}
Expand Down Expand Up @@ -191,11 +191,11 @@ public static void inject(HttpServerExchange exchange) {
return;
}

if (sort_by.stream().anyMatch(s -> s.trim().equals("_last_updated_on") || s.trim().equals("+_last_updated_on") || s.trim().equals("-_last_updated_on"))) {
if (sort_by.stream().anyMatch(s -> s.strip().equals("_last_updated_on") || s.strip().equals("+_last_updated_on") || s.strip().equals("-_last_updated_on"))) {
response.addWarning("unexepecting sorting; the _last_updated_on timestamp is generated from the _etag property if present");
}

if (sort_by.stream().anyMatch(s -> s.trim().equals("_created_on") || s.trim().equals("_created_on") || s.trim().equals("_created_on"))) {
if (sort_by.stream().anyMatch(s -> s.strip().equals("_created_on") || s.strip().equals("_created_on") || s.strip().equals("_created_on"))) {
response.addWarning("unexepecting sorting; the _created_on timestamp is generated from the _id property if it is an ObjectId");
}

Expand Down Expand Up @@ -333,7 +333,7 @@ public static void inject(HttpServerExchange exchange) {

if (_docIdType != null && !_docIdType.isEmpty()) {
try {
docIdType = DOC_ID_TYPE.valueOf(_docIdType.trim().toUpperCase());
docIdType = DOC_ID_TYPE.valueOf(_docIdType.strip().toUpperCase());
} catch (IllegalArgumentException iae) {
response.setInError(HttpStatus.SC_BAD_REQUEST, "illegal " + DOC_ID_TYPE_QPARAM_KEY + " paramenter; must be " + Arrays.toString(DOC_ID_TYPE.values()));
return;
Expand Down Expand Up @@ -366,7 +366,7 @@ public static void inject(HttpServerExchange exchange) {
String _halMode = __halMode.getFirst();

try {
request.setHalMode(HAL_MODE.valueOf(_halMode.trim().toUpperCase()));
request.setHalMode(HAL_MODE.valueOf(_halMode.strip().toUpperCase()));
} catch (IllegalArgumentException iae) {
response.setInError(HttpStatus.SC_BAD_REQUEST, "illegal " + HAL_QPARAM_KEY + " paramenter; valid values are " + Arrays.toString(HAL_MODE.values()));
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public String generateResponse(METRICS_GATHERING_LEVEL metricsLevel, MetricRegis
sb.append(generateResponse(registry, null, null, timestamp));
}

return sb.toString().trim();
return sb.toString().strip();
}

public String generateResponse(MetricRegistry registry, String databaseName, String collectionName, long timestamp) {
Expand Down Expand Up @@ -423,7 +423,7 @@ public static AcceptHeaderEntry of(String acceptHeaderEntry) {
double qValue = 1.0;
String specialization = null;
for (int i = 1; i < entries.size(); i++) {
String element = entries.get(i).trim();
String element = entries.get(i).strip();
if (element.startsWith("q=")) {
try {
qValue = Double.parseDouble(element.substring(2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class MetricsInstrumentationInterceptor implements MongoInterceptor {
@VisibleForTesting
static boolean isFilledAndNotMetrics(String dbOrCollectionName) {
return dbOrCollectionName != null
&& !dbOrCollectionName.trim().isEmpty()
&& !dbOrCollectionName.strip().isEmpty()
&& !dbOrCollectionName.equalsIgnoreCase(_METRICS);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public static String getMessageFromMongoException(MongoException me) {
var e = msg.indexOf("' on server");

if (b >= 0 && e >= 0) {
yield "Invalid filter: " + msg.substring(b+3, e).trim();
yield "Invalid filter: " + msg.substring(b+3, e).strip();
} else {
yield msg;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void init() {
List<String> _whitelist = arg(config, "whitelist");
this.whitelist = _whitelist.stream()
.filter(item -> item != null)
.map(item -> item.trim())
.map(item -> item.strip())
.map(item -> item.toLowerCase())
.map(item -> removeTrailingSlashes(item))
.map(item -> item.concat("/"))
Expand All @@ -74,7 +74,7 @@ public void init() {
List<String> _ingoreList = arg(config, "ignore-paths");
_ingoreList.stream()
.filter(item -> item != null)
.map(item -> item.trim())
.map(item -> item.strip())
.map(item -> item.toLowerCase())
.map(item -> PathTemplate.create(item))
.forEach(item -> this.ignoreLists.add(item, true));
Expand Down

0 comments on commit 8b3f449

Please sign in to comment.