Skip to content

Commit

Permalink
more strings cleanup and refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Nicholas Walter Knize <nknize@apache.org>
  • Loading branch information
nknize committed May 17, 2023
1 parent 5f94a4d commit 845bfbb
Show file tree
Hide file tree
Showing 45 changed files with 155 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class SimulateIndexTemplateRequest extends TimedRequest {
private PutComposableIndexTemplateRequest indexTemplateV2Request;

public SimulateIndexTemplateRequest(String indexName) {
if (org.opensearch.core.common.Strings.isNullOrEmpty(indexName)) {
if (Strings.isNullOrEmpty(indexName)) {
throw new IllegalArgumentException("index name cannot be null or empty");
}
this.indexName = indexName;
Expand Down
26 changes: 26 additions & 0 deletions libs/core/src/main/java/org/opensearch/core/common/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -489,4 +489,30 @@ public static Set<String> commaDelimitedListToSet(String str) {
public static boolean isNullOrEmpty(@Nullable String s) {
return s == null || s.isEmpty();
}

/**
* Capitalize a <code>String</code>, changing the first letter to
* upper case as per {@link Character#toUpperCase(char)}.
* No other letters are changed.
*
* @param str the String to capitalize, may be <code>null</code>
* @return the capitalized String, <code>null</code> if null
*/
public static String capitalize(String str) {
return changeFirstCharacterCase(str, true);
}

private static String changeFirstCharacterCase(String str, boolean capitalize) {
if (str == null || str.length() == 0) {
return str;
}
StringBuilder sb = new StringBuilder(str.length());
if (capitalize) {
sb.append(Character.toUpperCase(str.charAt(0)));
} else {
sb.append(Character.toLowerCase(str.charAt(0)));
}
sb.append(str.substring(1));
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.core.common;

import org.opensearch.common.util.set.Sets;
import org.opensearch.test.OpenSearchTestCase;

/** tests for Strings utility class */
public class StringsTests extends OpenSearchTestCase {
public void testSplitStringToSet() {
assertEquals(Strings.tokenizeByCommaToSet(null), Sets.newHashSet());
assertEquals(Strings.tokenizeByCommaToSet(""), Sets.newHashSet());
assertEquals(Strings.tokenizeByCommaToSet("a,b,c"), Sets.newHashSet("a", "b", "c"));
assertEquals(Strings.tokenizeByCommaToSet("a, b, c"), Sets.newHashSet("a", "b", "c"));
assertEquals(Strings.tokenizeByCommaToSet(" a , b, c "), Sets.newHashSet("a", "b", "c"));
assertEquals(Strings.tokenizeByCommaToSet("aa, bb, cc"), Sets.newHashSet("aa", "bb", "cc"));
assertEquals(Strings.tokenizeByCommaToSet(" a "), Sets.newHashSet("a"));
assertEquals(Strings.tokenizeByCommaToSet(" a "), Sets.newHashSet("a"));
assertEquals(Strings.tokenizeByCommaToSet(" aa "), Sets.newHashSet("aa"));
assertEquals(Strings.tokenizeByCommaToSet(" "), Sets.newHashSet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@

import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.snowball.SnowballFilter;
import org.opensearch.common.Strings;
import org.opensearch.common.settings.Settings;
import org.opensearch.core.common.Strings;
import org.opensearch.env.Environment;
import org.opensearch.index.IndexSettings;
import org.opensearch.index.analysis.AbstractTokenFilterFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@
import org.apache.lucene.analysis.ru.RussianLightStemFilter;
import org.apache.lucene.analysis.snowball.SnowballFilter;
import org.apache.lucene.analysis.sv.SwedishLightStemFilter;
import org.opensearch.common.Strings;
import org.opensearch.common.settings.Settings;
import org.opensearch.core.common.Strings;
import org.opensearch.env.Environment;
import org.opensearch.index.IndexSettings;
import org.opensearch.index.analysis.AbstractTokenFilterFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

package org.opensearch.index.rankeval;

import org.opensearch.common.Strings;
import org.opensearch.core.common.Strings;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.io.stream.Writeable;
Expand Down Expand Up @@ -129,7 +129,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws

@Override
public String toString() {
return Strings.toString(XContentType.JSON, this);
return org.opensearch.common.Strings.toString(XContentType.JSON, this);
}

@Override
Expand Down Expand Up @@ -158,10 +158,10 @@ static class DocumentKey {
private final String index;

DocumentKey(String index, String docId) {
if (org.opensearch.core.common.Strings.isNullOrEmpty(index)) {
if (Strings.isNullOrEmpty(index)) {
throw new IllegalArgumentException("Index must be set for each rated document");
}
if (org.opensearch.core.common.Strings.isNullOrEmpty(docId)) {
if (Strings.isNullOrEmpty(docId)) {
throw new IllegalArgumentException("DocId must be set for each rated document");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public String getPassword() {
}

public boolean isAuthenticated() {
return org.opensearch.core.common.Strings.isNullOrEmpty(username) == false && Strings.isNullOrEmpty(password) == false;
return Strings.isNullOrEmpty(username) == false && Strings.isNullOrEmpty(password) == false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

package org.opensearch.repositories.azure;

import org.opensearch.core.common.Strings;
import reactor.core.scheduler.Schedulers;

import com.azure.core.http.policy.HttpPipelinePolicy;
Expand All @@ -45,6 +44,7 @@
import org.opensearch.common.settings.SettingsException;
import org.opensearch.common.settings.SettingsModule;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.core.common.Strings;
import org.opensearch.test.OpenSearchTestCase;

import java.io.IOException;
Expand Down Expand Up @@ -340,7 +340,7 @@ public void testMultipleProxies() throws UnknownHostException {
assertThat(azure2Proxy, notNullValue());
assertThat(azure2Proxy.getType(), is(ProxySettings.ProxyType.HTTP));
assertThat(azure2Proxy.getAddress(), is(new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 8081)));
assertTrue(org.opensearch.core.common.Strings.isNullOrEmpty(azure2Proxy.getUsername()));
assertTrue(Strings.isNullOrEmpty(azure2Proxy.getUsername()));
assertTrue(Strings.isNullOrEmpty(azure2Proxy.getPassword()));
assertEquals(mock.storageSettings.get("azure3").getProxySettings(), ProxySettings.NO_PROXY_SETTINGS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected Collection<Class<? extends Plugin>> getPlugins() {
protected Settings nodeSettings() {
Settings.Builder builder = Settings.builder().put(super.nodeSettings());

if (org.opensearch.core.common.Strings.isNullOrEmpty(System.getProperty("test.google.endpoint")) == false) {
if (Strings.isNullOrEmpty(System.getProperty("test.google.endpoint")) == false) {
builder.put("gcs.client.default.endpoint", System.getProperty("test.google.endpoint"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public String getPassword() {
}

public boolean isAuthenticated() {
return org.opensearch.core.common.Strings.isNullOrEmpty(username) == false && Strings.isNullOrEmpty(password) == false;
return Strings.isNullOrEmpty(username) == false && Strings.isNullOrEmpty(password) == false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public String getPassword() {
}

public boolean isAuthenticated() {
return org.opensearch.core.common.Strings.isNullOrEmpty(username) == false && Strings.isNullOrEmpty(password) == false;
return Strings.isNullOrEmpty(username) == false && Strings.isNullOrEmpty(password) == false;
}

public ProxySettings recreateWithNewHostAndPort(final String host, final int port) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,7 @@ static ProxySettings validateAndCreateProxySettings(final Settings settings, fin
final int proxyPort = getConfigValue(settings, clientName, PROXY_PORT_SETTING);
final SecureString proxyUserName = getConfigValue(settings, clientName, PROXY_USERNAME_SETTING);
final SecureString proxyPassword = getConfigValue(settings, clientName, PROXY_PASSWORD_SETTING);
if (awsProtocol != Protocol.HTTPS
&& proxyType == ProxySettings.ProxyType.DIRECT
&& org.opensearch.core.common.Strings.hasText(proxyHost)) {
if (awsProtocol != Protocol.HTTPS && proxyType == ProxySettings.ProxyType.DIRECT && Strings.hasText(proxyHost)) {
// This is backward compatibility for the current behaviour.
// The default value for Protocol settings is HTTPS,
// The expectation of ex-developers that protocol is the same as the proxy protocol
Expand All @@ -498,13 +496,10 @@ static ProxySettings validateAndCreateProxySettings(final Settings settings, fin
}
// Validate proxy settings
if (proxyType == ProxySettings.ProxyType.DIRECT
&& (proxyPort != 80
|| org.opensearch.core.common.Strings.hasText(proxyHost)
|| org.opensearch.core.common.Strings.hasText(proxyUserName)
|| org.opensearch.core.common.Strings.hasText(proxyPassword))) {
&& (proxyPort != 80 || Strings.hasText(proxyHost) || Strings.hasText(proxyUserName) || Strings.hasText(proxyPassword))) {
throw new SettingsException("S3 proxy port or host or username or password have been set but proxy type is not defined.");
}
if (proxyType != ProxySettings.ProxyType.DIRECT && org.opensearch.core.common.Strings.isEmpty(proxyHost)) {
if (proxyType != ProxySettings.ProxyType.DIRECT && Strings.isEmpty(proxyHost)) {
throw new SettingsException("S3 proxy type has been set but proxy host or port is not defined.");
}
if (proxyType == ProxySettings.ProxyType.DIRECT) {
Expand Down Expand Up @@ -585,8 +580,8 @@ static class IrsaCredentials {
private final String roleSessionName;

IrsaCredentials(String identityTokenFile, String roleArn, String roleSessionName) {
this.identityTokenFile = org.opensearch.core.common.Strings.isNullOrEmpty(identityTokenFile) ? null : identityTokenFile;
this.roleArn = org.opensearch.core.common.Strings.isNullOrEmpty(roleArn) ? null : roleArn;
this.identityTokenFile = Strings.isNullOrEmpty(identityTokenFile) ? null : identityTokenFile;
this.roleArn = Strings.isNullOrEmpty(roleArn) ? null : roleArn;
this.roleSessionName = Strings.isNullOrEmpty(roleSessionName) ? "s3-sdk-java-" + System.currentTimeMillis() : roleSessionName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.action.support.IndicesOptions;
import org.opensearch.action.support.clustermanager.ClusterManagerNodeRequest;
import org.opensearch.common.Strings;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.common.Strings;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;

Expand Down Expand Up @@ -130,7 +130,7 @@ public RestoreRemoteStoreRequest source(Map<String, Object> source) {
String name = entry.getKey();
if (name.equals("indices")) {
if (entry.getValue() instanceof String) {
indices(org.opensearch.core.common.Strings.splitStringByCommaToArray((String) entry.getValue()));
indices(Strings.splitStringByCommaToArray((String) entry.getValue()));
} else if (entry.getValue() instanceof ArrayList) {
indices((ArrayList<String>) entry.getValue());
} else {
Expand Down Expand Up @@ -179,6 +179,6 @@ public int hashCode() {

@Override
public String toString() {
return Strings.toString(XContentType.JSON, this);
return org.opensearch.common.Strings.toString(XContentType.JSON, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
import org.opensearch.action.support.IndicesOptions;
import org.opensearch.action.support.clustermanager.ClusterManagerNodeRequest;
import org.opensearch.common.Nullable;
import org.opensearch.common.Strings;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.logging.DeprecationLogger;
import org.opensearch.common.settings.Settings;
import org.opensearch.core.common.Strings;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.XContentType;
Expand Down Expand Up @@ -533,7 +533,7 @@ public RestoreSnapshotRequest source(Map<String, Object> source) {
String name = entry.getKey();
if (name.equals("indices")) {
if (entry.getValue() instanceof String) {
indices(org.opensearch.core.common.Strings.splitStringByCommaToArray((String) entry.getValue()));
indices(Strings.splitStringByCommaToArray((String) entry.getValue()));
} else if (entry.getValue() instanceof ArrayList) {
indices((ArrayList<String>) entry.getValue());
} else {
Expand Down Expand Up @@ -684,6 +684,6 @@ public int hashCode() {

@Override
public String toString() {
return Strings.toString(XContentType.JSON, this);
return org.opensearch.common.Strings.toString(XContentType.JSON, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.opensearch.action.support.IndicesOptions;
import org.opensearch.action.support.master.AcknowledgedRequest;
import org.opensearch.action.support.master.AcknowledgedResponse;
import org.opensearch.common.Strings;
import org.opensearch.common.bytes.BytesArray;
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.io.stream.StreamInput;
Expand All @@ -49,6 +48,7 @@
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.common.Strings;
import org.opensearch.core.xcontent.MediaType;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
Expand Down Expand Up @@ -248,9 +248,9 @@ public static XContentBuilder simpleMapping(String... source) {
String fieldName = source[i++];
if (RESERVED_FIELDS.contains(fieldName)) {
builder.startObject(fieldName);
String[] s1 = org.opensearch.core.common.Strings.splitStringByCommaToArray(source[i]);
String[] s1 = Strings.splitStringByCommaToArray(source[i]);
for (String s : s1) {
String[] s2 = Strings.split(s, "=");
String[] s2 = org.opensearch.common.Strings.split(s, "=");
if (s2.length != 2) {
throw new IllegalArgumentException("malformed " + s);
}
Expand All @@ -270,7 +270,7 @@ public static XContentBuilder simpleMapping(String... source) {
builder.startObject(fieldName);
String[] s1 = org.opensearch.core.common.Strings.splitStringByCommaToArray(source[i]);
for (String s : s1) {
String[] s2 = Strings.split(s, "=");
String[] s2 = org.opensearch.common.Strings.split(s, "=");
if (s2.length != 2) {
throw new IllegalArgumentException("malformed " + s);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@
import org.opensearch.cluster.metadata.Metadata;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.Nullable;
import org.opensearch.common.Strings;
import org.opensearch.common.inject.Inject;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.io.stream.Writeable;
import org.opensearch.common.util.concurrent.CountDown;
import org.opensearch.core.common.Strings;
import org.opensearch.core.ParseField;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
Expand Down Expand Up @@ -254,7 +254,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
builder.array(ALIASES_FIELD.getPreferredName(), aliases);
}
builder.array(ATTRIBUTES_FIELD.getPreferredName(), attributes);
if (org.opensearch.core.common.Strings.isNullOrEmpty(dataStream) == false) {
if (Strings.isNullOrEmpty(dataStream) == false) {
builder.field(DATA_STREAM_FIELD.getPreferredName(), dataStream);
}
builder.endObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ static void validate(Metadata metadata, String rolloverTarget, String newIndexNa
"rollover target is a ["
+ indexAbstraction.getType().getDisplayName()
+ "] but one of ["
+ org.opensearch.core.common.Strings.collectionToCommaDelimitedString(
+ Strings.collectionToCommaDelimitedString(
VALID_ROLLOVER_TARGETS.stream().map(IndexAbstraction.Type::getDisplayName).collect(Collectors.toList())
)
+ "] was expected"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,19 +455,15 @@ public void writeTo(StreamOutput out) throws IOException {

@Override
public String getDescription() {
return "requests["
+ requests.size()
+ "], indices["
+ org.opensearch.core.common.Strings.collectionToDelimitedString(indices, ", ")
+ "]";
return "requests[" + requests.size() + "], indices[" + Strings.collectionToDelimitedString(indices, ", ") + "]";
}

private void applyGlobalMandatoryParameters(DocWriteRequest<?> request) {
request.index(valueOrDefault(request.index(), globalIndex));
}

private static String valueOrDefault(String value, String globalDefault) {
if (org.opensearch.core.common.Strings.isNullOrEmpty(value) && !Strings.isNullOrEmpty(globalDefault)) {
if (Strings.isNullOrEmpty(value) && Strings.isNullOrEmpty(globalDefault) == false) {
return globalDefault;
}
return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import org.opensearch.common.settings.Setting;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.core.common.Strings;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.env.Environment;
import org.opensearch.index.Index;
Expand Down Expand Up @@ -1045,7 +1046,7 @@ public static List<AliasMetadata> resolveAndValidateAliases(
List<AliasMetadata> resolvedAliases = new ArrayList<>();
for (Alias alias : aliases) {
aliasValidator.validateAlias(alias, index, metadata);
if (org.opensearch.core.common.Strings.hasLength(alias.filter())) {
if (Strings.hasLength(alias.filter())) {
aliasValidator.validateAliasFilter(alias.name(), alias.filter(), queryShardContext, xContentRegistry);
}
AliasMetadata aliasMetadata = AliasMetadata.builder(alias.name())
Expand Down Expand Up @@ -1430,10 +1431,7 @@ static void prepareResizeIndexSettings(
resizeIntoName,
indexSettingsBuilder.build()
);
indexSettingsBuilder.put(
initialRecoveryIdFilter,
org.opensearch.core.common.Strings.arrayToCommaDelimitedString(nodesToAllocateOn.toArray())
);
indexSettingsBuilder.put(initialRecoveryIdFilter, Strings.arrayToCommaDelimitedString(nodesToAllocateOn.toArray()));
} else if (type == ResizeType.SPLIT) {
validateSplitIndex(currentState, resizeSourceIndex.getName(), resizeIntoName, indexSettingsBuilder.build());
indexSettingsBuilder.putNull(initialRecoveryIdFilter);
Expand Down
Loading

0 comments on commit 845bfbb

Please sign in to comment.