Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ibdo :: get parameter as string #1014

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 41 additions & 11 deletions src/main/java/emissary/core/BaseDataObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -994,32 +995,61 @@ public boolean appendUniqueParameter(final String key, final CharSequence value)
}

@Override
@Deprecated
public String getStringParameter(final String key) {
return getStringParameter(key, DEFAULT_PARAM_SEPARATOR);
return getParameterAsConcatString(key);
}

@Nullable
@Override
@Deprecated
public String getStringParameter(final String key, final String sep) {
return getParameterAsConcatString(key, sep);
}

@Override
public Collection<String> getParameterAsStrings(final String key) {
final List<Object> obj = getParameter(key);

if (obj == null) {
return null;
return Collections.emptyList();
} else if (obj.isEmpty()) {
return null;
return Collections.emptyList();
} else if ((obj.size() == 1) && (obj.get(0) instanceof String)) {
return (String) obj.get(0);
return Collections.singletonList((String) obj.get(0));
} else if ((obj.size() == 1) && (obj.get(0) == null)) {
return null;
return Collections.emptyList();
} else {
final StringBuilder sb = new StringBuilder();
final List<String> values = new ArrayList<>(obj.size());
for (final Object item : obj) {
if (sb.length() > 0) {
sb.append(sep);
}
sb.append(item);
values.add(String.valueOf(item));
}
return sb.toString();
return Collections.unmodifiableList(values);
}
}

@Nullable
@Override
public String getParameterAsString(final String key) {
final Collection<String> paramStrings = getParameterAsStrings(key);
if (paramStrings.size() > 1) {
throw new IllegalStateException("There is more than one value for parameter[" + key + " ]!!");
}

return StringUtils.trimToNull(paramStrings.stream().findFirst().orElse(null));
}

@Nullable
@Override
public String getParameterAsConcatString(final String key) {
return getStringParameter(key, DEFAULT_PARAM_SEPARATOR);
}

@Nullable
@Override
public String getParameterAsConcatString(final String key, final String sep) {
final String strParameter = String.join(sep, getParameterAsStrings(key));
return StringUtils.isBlank(strParameter) ? null : strParameter;
}

/**
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/emissary/core/IBaseDataObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,9 @@ enum MergePolicy {
*
* @param key name of the metadata element
* @return the string value or null if no such element
* @deprecated use {@link #getParameterAsConcatString(String)}
*/
@Deprecated
String getStringParameter(String key);

/**
Expand All @@ -359,9 +361,46 @@ enum MergePolicy {
* @param key name of the metadata element
* @param sep the separator for multivalued fields
* @return the string value or null if no such element
* @deprecated use {@link #getParameterAsConcatString(String, String)}
*/
@Deprecated
String getStringParameter(String key, String sep);

/**
* Retrieve the Collection of metadata values identified by key where each element is converted to a string
*
* @param key name of the metadata element collection
* @return Collection of elements converted to strings
*/
Collection<String> getParameterAsStrings(String key);

/**
* Retrieve the metadata value identified by key where the element is converted to a string
*
* @param key name of the metadata element
* @return parameter converted to strings
* @throws IllegalStateException if the parameter has more than one value
*/
String getParameterAsString(String key);


/**
* Retrieve a specified metadata element as a string of concatenated values
*
* @param key name of the metadata element
* @return the string value or null if no such element
*/
String getParameterAsConcatString(String key);
dev-mlb marked this conversation as resolved.
Show resolved Hide resolved

/**
* Retrieve a specified metadata element as a string of concatenated values
*
* @param key name of the metadata element
* @param sep the separator for multivalued fields
* @return the string value or null if no such element
*/
String getParameterAsConcatString(String key, String sep);

/**
* Retrieve all the metadata elements of this object
*
Expand Down
Loading