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

Use Java 11 collections conveniences everywhere #41399

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.github.mustachejava.codes.DefaultMustache;
import com.github.mustachejava.codes.IterableCode;
import com.github.mustachejava.codes.WriteCode;
import org.apache.lucene.search.highlight.DefaultEncoder;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;
Expand Down Expand Up @@ -86,11 +87,11 @@ public void encode(String value, Writer writer) {
}

static Encoder createEncoder(String mimeType) {
Supplier<Encoder> supplier = ENCODERS.get(mimeType);
final Supplier<Encoder> supplier = ENCODERS.get(mimeType);
if (supplier == null) {
throw new IllegalArgumentException("No encoder found for MIME type [" + mimeType + "]");
}
return supplier.get();
return ENCODERS.get(mimeType).get();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,22 @@
public class CustomMustacheFactoryTests extends ESTestCase {

public void testCreateEncoder() {
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> CustomMustacheFactory.createEncoder(null));
jasontedor marked this conversation as resolved.
Show resolved Hide resolved
assertThat(e.getMessage(), equalTo("No encoder found for MIME type [null]"));

e = expectThrows(IllegalArgumentException.class, () -> CustomMustacheFactory.createEncoder(""));
assertThat(e.getMessage(), equalTo("No encoder found for MIME type []"));

e = expectThrows(IllegalArgumentException.class, () -> CustomMustacheFactory.createEncoder("test"));
assertThat(e.getMessage(), equalTo("No encoder found for MIME type [test]"));
{
final IllegalArgumentException e =
expectThrows(IllegalArgumentException.class, () -> CustomMustacheFactory.createEncoder("non-existent"));
assertThat(e.getMessage(), equalTo("No encoder found for MIME type [non-existent]"));
}

{
final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> CustomMustacheFactory.createEncoder(""));
assertThat(e.getMessage(), equalTo("No encoder found for MIME type []"));
}

{
final IllegalArgumentException e =
expectThrows(IllegalArgumentException.class, () -> CustomMustacheFactory.createEncoder("test"));
assertThat(e.getMessage(), equalTo("No encoder found for MIME type [test]"));
}

assertThat(CustomMustacheFactory.createEncoder(CustomMustacheFactory.JSON_MIME_TYPE_WITH_CHARSET),
instanceOf(CustomMustacheFactory.JsonEscapeEncoder.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ private DocumentMapper parse(String type, Map<String, Object> mapping, String de
*
* Note: this copy can not be replaced by Map#copyOf because we rely on consistent serialization order since we do byte-level
* checks on the mapping between what we receive from the master and what we have locally. As Map#copyOf is not necessarily
* the same underlying map implementation, we could end up with a different iteration order.
* the same underlying map implementation, we could end up with a different iteration order. For reference, see
* MapperService#assertSerializtion and GitHub issues #10302 and #10318.
*
* Do not change this to Map#copyOf or any other method of copying meta that could change the iteration order.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ private synchronized void execute(T previousValue, ActionListener<List<T>> liste
collectedResponses.add(previousValue);
if (continuationPredicate.test(previousValue)) {
if (tasks.isEmpty()) {
listener.onResponse(List.copyOf(collectedResponses));
// noinspection Java9CollectionFactory (because the list can contain null entries)
listener.onResponse(Collections.unmodifiableList(new ArrayList<>(collectedResponses)));
return;
}
ChainTask<T> task = tasks.pop();
Expand All @@ -83,7 +84,8 @@ protected void doRun() {
}
});
} else {
listener.onResponse(List.copyOf(collectedResponses));
// noinspection Java9CollectionFactory (because the list can contain null entries)
listener.onResponse(Collections.unmodifiableList(new ArrayList<>(collectedResponses)));
}
}

Expand Down