From 3b38df4393c0a5f4d1c6db4ed695295bbc4f9d8f Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Sun, 17 Jul 2022 08:50:57 -0400 Subject: [PATCH] Use forEach() --- .../exception/DefaultExceptionContext.java | 68 ++++++++----------- 1 file changed, 29 insertions(+), 39 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/exception/DefaultExceptionContext.java b/src/main/java/org/apache/commons/lang3/exception/DefaultExceptionContext.java index 638674261..51bde8b50 100644 --- a/src/main/java/org/apache/commons/lang3/exception/DefaultExceptionContext.java +++ b/src/main/java/org/apache/commons/lang3/exception/DefaultExceptionContext.java @@ -18,9 +18,10 @@ package org.apache.commons.lang3.exception; import java.io.Serializable; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.ImmutablePair; @@ -58,37 +59,8 @@ public class DefaultExceptionContext implements ExceptionContext, Serializable { * {@inheritDoc} */ @Override - public DefaultExceptionContext setContextValue(final String label, final Object value) { - contextValues.removeIf(p -> StringUtils.equals(label, p.getKey())); - addContextValue(label, value); - return this; - } - - /** - * {@inheritDoc} - */ - @Override - public List getContextValues(final String label) { - final List values = new ArrayList<>(); - for (final Pair pair : contextValues) { - if (StringUtils.equals(label, pair.getKey())) { - values.add(pair.getValue()); - } - } - return values; - } - - /** - * {@inheritDoc} - */ - @Override - public Object getFirstContextValue(final String label) { - for (final Pair pair : contextValues) { - if (StringUtils.equals(label, pair.getKey())) { - return pair.getValue(); - } - } - return null; + public List> getContextEntries() { + return contextValues; } /** @@ -96,19 +68,23 @@ public class DefaultExceptionContext implements ExceptionContext, Serializable { */ @Override public Set getContextLabels() { - final Set labels = new HashSet<>(); - for (final Pair pair : contextValues) { - labels.add(pair.getKey()); - } - return labels; + return stream().map(Pair::getKey).collect(Collectors.toSet()); } /** * {@inheritDoc} */ @Override - public List> getContextEntries() { - return contextValues; + public List getContextValues(final String label) { + return stream().filter(pair -> StringUtils.equals(label, pair.getKey())).map(Pair::getValue).collect(Collectors.toList()); + } + + /** + * {@inheritDoc} + */ + @Override + public Object getFirstContextValue(final String label) { + return stream().filter(pair -> StringUtils.equals(label, pair.getKey())).findFirst().map(Pair::getValue).orElse(null); } /** @@ -156,4 +132,18 @@ public class DefaultExceptionContext implements ExceptionContext, Serializable { return buffer.toString(); } + /** + * {@inheritDoc} + */ + @Override + public DefaultExceptionContext setContextValue(final String label, final Object value) { + contextValues.removeIf(p -> StringUtils.equals(label, p.getKey())); + addContextValue(label, value); + return this; + } + + private Stream> stream() { + return contextValues.stream(); + } + }