Skip to content

Commit

Permalink
Revert " ibdo :: get parameter as string (#1014)"
Browse files Browse the repository at this point in the history
This reverts commit 2e798ab.
  • Loading branch information
jpdahlke authored Dec 23, 2024
1 parent 2e798ab commit 2012997
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 109 deletions.
29 changes: 24 additions & 5 deletions src/main/java/emissary/core/BaseDataObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -965,14 +965,33 @@ public boolean appendUniqueParameter(final String key, final CharSequence value)
return true;
}

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

@Nullable
@Override
public String getParameterAsString(final String key) {
final var obj = getParameterAsStrings(key);
if (obj.size() > 1) {
logger.warn("Multiple values for parameter, returning the first - parameter:{}, number of values:{}", key, obj.size());
public String getStringParameter(final String key, final String sep) {
final List<Object> obj = getParameter(key);
if (obj == null) {
return null;
} else if (obj.isEmpty()) {
return null;
} else if ((obj.size() == 1) && (obj.get(0) instanceof String)) {
return (String) obj.get(0);
} else if ((obj.size() == 1) && (obj.get(0) == null)) {
return null;
} else {
final StringBuilder sb = new StringBuilder();
for (final Object item : obj) {
if (sb.length() > 0) {
sb.append(sep);
}
sb.append(item);
}
return sb.toString();
}
return StringUtils.trimToNull(obj.stream().findFirst().orElse(null));
}

/**
Expand Down
64 changes: 2 additions & 62 deletions src/main/java/emissary/core/IBaseDataObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,15 @@
import emissary.core.channels.SeekableByteChannelFactory;
import emissary.directory.DirectoryEntry;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.time.Instant;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

public interface IBaseDataObject {

Expand Down Expand Up @@ -353,72 +348,17 @@ 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
default String getStringParameter(final String key) {
return getParameterAsConcatString(key);
}
String getStringParameter(String key);

/**
* Retrieve a specified metadata element as a string value
*
* @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
default String getStringParameter(final String key, final String sep) {
return getParameterAsConcatString(key, 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
*/
default Collection<String> getParameterAsStrings(final String key) {
final var obj = getParameter(key);
if (CollectionUtils.isEmpty(obj) || ((obj.size() == 1) && (obj.get(0) == null))) {
return Collections.emptyList();
} else if ((obj.size() == 1) && (obj.get(0) instanceof String)) {
return Collections.singletonList((String) obj.get(0));
} else {
return obj.stream().map(String::valueOf).collect(Collectors.toList());
}
}

/**
* 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
*/
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
*/
default String getParameterAsConcatString(final String key) {
return getParameterAsConcatString(key, DEFAULT_PARAM_SEPARATOR);
}

/**
* 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
*/
default String getParameterAsConcatString(final String key, final String sep) {
final var strParameter = String.join(sep, getParameterAsStrings(key));
return StringUtils.isBlank(strParameter) ? null : strParameter;
}
String getStringParameter(String key, String sep);

/**
* Retrieve all the metadata elements of this object
Expand Down
42 changes: 0 additions & 42 deletions src/test/java/emissary/core/BaseDataObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1333,46 +1333,4 @@ void testNewInputStream() throws IOException {
assertArrayEquals(bytes2, byteArrayOutputStream.toByteArray());
}
}

@Test
void testGetParameterAsString() throws IOException {
assertNotNull(this.b);

try (LogbackTester logbackTester = new LogbackTester(BaseDataObject.class.getName())) {
this.b.putParameter("A", 1L);
this.b.appendParameter("A", "TWO");
this.b.appendParameter("A", "THREE");
assertEquals("1", this.b.getParameterAsString("A"));
assertEquals("1;TWO;THREE", this.b.getParameterAsConcatString("A"));
LogbackTester.SimplifiedLogEvent logEvent = new LogbackTester.SimplifiedLogEvent(Level.WARN,
"Multiple values for parameter, returning the first - parameter:A, number of values:3", null);
logbackTester.checkLogList(Collections.singletonList(logEvent));
}

this.b.putParameter("A", 2L);
assertEquals("2", this.b.getParameterAsString("A"));
assertEquals("2", this.b.getParameterAsConcatString("A"));

this.b.putParameter("A", "THREE");
assertEquals("THREE", this.b.getParameterAsString("A"));
assertEquals("THREE", this.b.getParameterAsConcatString("A"));

this.b.putParameter("A", null);
assertNull(this.b.getParameterAsString("A"));
assertNull(this.b.getParameterAsConcatString("A"));

this.b.putParameter("A", "");
assertNull(this.b.getParameterAsString("A"));
assertNull(this.b.getParameterAsConcatString("A"));

assertNull(this.b.getParameterAsString("DNE"));
assertNull(this.b.getParameterAsConcatString("DNE"));

this.b.putParameter("A", null);
this.b.appendParameter("A", "FOUR");
this.b.appendParameter("A", " ");
assertEquals("null", this.b.getParameterAsString("A"));
assertEquals("null;FOUR; ", this.b.getParameterAsConcatString("A"));
}

}

0 comments on commit 2012997

Please sign in to comment.