You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bval.apache.org by mb...@apache.org on 2018/02/21 21:01:52 UTC
[02/17] bval git commit: BV2: utility classes
http://git-wip-us.apache.org/repos/asf/bval/blob/fad43eb8/bval-jsr/src/main/java/org/apache/bval/jsr/util/NodeContextBuilderImpl.java
----------------------------------------------------------------------
diff --git a/bval-jsr/src/main/java/org/apache/bval/jsr/util/NodeContextBuilderImpl.java b/bval-jsr/src/main/java/org/apache/bval/jsr/util/NodeContextBuilderImpl.java
index 80e5b8f..f32db9f 100644
--- a/bval-jsr/src/main/java/org/apache/bval/jsr/util/NodeContextBuilderImpl.java
+++ b/bval-jsr/src/main/java/org/apache/bval/jsr/util/NodeContextBuilderImpl.java
@@ -18,18 +18,19 @@
*/
package org.apache.bval.jsr.util;
-import org.apache.bval.jsr.ConstraintValidatorContextImpl;
+import org.apache.bval.jsr.job.ConstraintValidatorContextImpl;
import javax.validation.ConstraintValidatorContext;
+import javax.validation.ConstraintValidatorContext.ConstraintViolationBuilder.ContainerElementNodeBuilderCustomizableContext;
import javax.validation.ConstraintValidatorContext.ConstraintViolationBuilder.NodeContextBuilder;
/**
* Description: Implementation of {@link NodeContextBuilder}.<br/>
*/
-final class NodeContextBuilderImpl implements ConstraintValidatorContext.ConstraintViolationBuilder.NodeContextBuilder {
- private final ConstraintValidatorContextImpl parent;
- private final String messageTemplate;
- private final PathImpl propertyPath;
+public final class NodeContextBuilderImpl implements ConstraintValidatorContext.ConstraintViolationBuilder.NodeContextBuilder {
+ private final ConstraintValidatorContextImpl<?> context;
+ private final String template;
+ private final PathImpl path;
// The name of the last "added" node, it will only be added if it has a non-null name
// The actual incorporation in the path will take place when the definition of the current leaf node is complete
private final NodeImpl node;
@@ -40,10 +41,10 @@ final class NodeContextBuilderImpl implements ConstraintValidatorContext.Constra
* @param template
* @param path
*/
- NodeContextBuilderImpl(ConstraintValidatorContextImpl contextImpl, String template, PathImpl path, NodeImpl node) {
- parent = contextImpl;
- messageTemplate = template;
- propertyPath = path;
+ NodeContextBuilderImpl(ConstraintValidatorContextImpl<?> contextImpl, String template, PathImpl path, NodeImpl node) {
+ this.context = contextImpl;
+ this.template = template;
+ this.path = path;
this.node = node;
}
@@ -53,8 +54,8 @@ final class NodeContextBuilderImpl implements ConstraintValidatorContext.Constra
@Override
public ConstraintValidatorContext.ConstraintViolationBuilder.NodeBuilderDefinedContext atKey(Object key) {
node.setKey(key);
- propertyPath.addNode(node);
- return new NodeBuilderDefinedContextImpl(parent, messageTemplate, propertyPath);
+ path.addNode(node);
+ return new NodeBuilderDefinedContextImpl(context, template, path);
}
/**
@@ -63,8 +64,8 @@ final class NodeContextBuilderImpl implements ConstraintValidatorContext.Constra
@Override
public ConstraintValidatorContext.ConstraintViolationBuilder.NodeBuilderDefinedContext atIndex(Integer index) {
node.setIndex(index);
- propertyPath.addNode(node);
- return new NodeBuilderDefinedContextImpl(parent, messageTemplate, propertyPath);
+ path.addNode(node);
+ return new NodeBuilderDefinedContextImpl(context, template, path);
}
/**
@@ -78,14 +79,14 @@ final class NodeContextBuilderImpl implements ConstraintValidatorContext.Constra
@Override
public ConstraintValidatorContext.ConstraintViolationBuilder.NodeBuilderCustomizableContext addPropertyNode(
String name) {
- propertyPath.addNode(node);
- return new NodeBuilderCustomizableContextImpl(parent, messageTemplate, propertyPath, name);
+ path.addNode(node);
+ return new NodeBuilderCustomizableContextImpl(context, template, path, name);
}
@Override
public ConstraintValidatorContext.ConstraintViolationBuilder.LeafNodeBuilderCustomizableContext addBeanNode() {
- propertyPath.addNode(node);
- return new LeafNodeBuilderCustomizableContextImpl(parent, messageTemplate, propertyPath);
+ path.addNode(node);
+ return new LeafNodeBuilderCustomizableContextImpl(context, template, path);
}
/**
@@ -93,9 +94,18 @@ final class NodeContextBuilderImpl implements ConstraintValidatorContext.Constra
*/
@Override
public ConstraintValidatorContext addConstraintViolation() {
- propertyPath.addNode(node);
- parent.addError(messageTemplate, propertyPath);
- return parent;
+ path.addNode(node);
+ context.addError(template, path);
+ return context;
}
-}
\ No newline at end of file
+ @Override
+ public ContainerElementNodeBuilderCustomizableContext addContainerElementNode(String name, Class<?> containerType,
+ Integer typeArgumentIndex) {
+ final NodeImpl node = new NodeImpl.ContainerElementNodeImpl(name, containerType, typeArgumentIndex);
+ path.addNode(node);
+ return new ContainerElementNodeBuilderCustomizableContextImpl(context, template, path, name, containerType,
+ typeArgumentIndex);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/bval/blob/fad43eb8/bval-jsr/src/main/java/org/apache/bval/jsr/util/NodeImpl.java
----------------------------------------------------------------------
diff --git a/bval-jsr/src/main/java/org/apache/bval/jsr/util/NodeImpl.java b/bval-jsr/src/main/java/org/apache/bval/jsr/util/NodeImpl.java
index 5a08f0e..96f9421 100644
--- a/bval-jsr/src/main/java/org/apache/bval/jsr/util/NodeImpl.java
+++ b/bval-jsr/src/main/java/org/apache/bval/jsr/util/NodeImpl.java
@@ -21,16 +21,25 @@ package org.apache.bval.jsr.util;
import javax.validation.ElementKind;
import javax.validation.Path;
import javax.validation.Path.Node;
+
+import org.apache.bval.util.Exceptions;
+
import java.io.Serializable;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.function.Consumer;
public class NodeImpl implements Path.Node, Serializable {
private static final long serialVersionUID = 1L;
private static final String INDEX_OPEN = "[";
private static final String INDEX_CLOSE = "]";
- private List<Class<?>> parameterTypes;
+
+ private static <T extends Path.Node> Optional<T> optional(Class<T> type, Object o) {
+ return Optional.ofNullable(o).filter(type::isInstance).map(type::cast);
+ }
/**
* Append a Node to the specified StringBuilder.
@@ -63,7 +72,7 @@ public class NodeImpl implements Path.Node, Serializable {
* @return NodeImpl
*/
public static NodeImpl atIndex(Integer index) {
- NodeImpl result = new NodeImpl();
+ final NodeImpl result = new NodeImpl();
result.setIndex(index);
return result;
}
@@ -74,7 +83,7 @@ public class NodeImpl implements Path.Node, Serializable {
* @return NodeImpl
*/
public static NodeImpl atKey(Object key) {
- NodeImpl result = new NodeImpl();
+ final NodeImpl result = new NodeImpl();
result.setKey(key);
return result;
}
@@ -85,6 +94,9 @@ public class NodeImpl implements Path.Node, Serializable {
private int parameterIndex;
private Object key;
private ElementKind kind;
+ private List<Class<?>> parameterTypes;
+ private Class<?> containerType;
+ private Integer typeArgumentIndex;
/**
* Create a new NodeImpl instance.
@@ -99,13 +111,18 @@ public class NodeImpl implements Path.Node, Serializable {
* @param node
*/
NodeImpl(Path.Node node) {
- this.name = node.getName();
+ this(node.getName());
this.inIterable = node.isInIterable();
this.index = node.getIndex();
this.key = node.getKey();
this.kind = node.getKind();
}
+ <T extends Path.Node> NodeImpl(Path.Node node, Class<T> nodeType, Consumer<T> handler) {
+ this(node);
+ Optional.of(node).filter(nodeType::isInstance).map(nodeType::cast).ifPresent(handler);
+ }
+
private NodeImpl() {
}
@@ -191,10 +208,8 @@ public class NodeImpl implements Path.Node, Serializable {
@Override
public <T extends Node> T as(final Class<T> nodeType) {
- if (nodeType.isInstance(this)) {
- return nodeType.cast(this);
- }
- throw new ClassCastException("Type " + nodeType + " not supported");
+ Exceptions.raiseUnless(nodeType.isInstance(this), ClassCastException::new, "Type %s not supported", nodeType);
+ return nodeType.cast(this);
}
/**
@@ -213,29 +228,13 @@ public class NodeImpl implements Path.Node, Serializable {
if (this == o) {
return true;
}
- if (o == null || getClass() != o.getClass()) {
- return false;
- }
-
- NodeImpl node = (NodeImpl) o;
-
- if (inIterable != node.inIterable) {
- return false;
- }
- if (index != null ? !index.equals(node.index) : node.index != null) {
- return false;
- }
- if (key != null ? !key.equals(node.key) : node.key != null) {
- return false;
- }
- if (name != null ? !name.equals(node.name) : node.name != null) {
- return false;
- }
- if (kind != null ? !kind.equals(node.kind) : node.kind != null) {
+ if (o == null || !getClass().equals(o.getClass())) {
return false;
}
+ final NodeImpl node = (NodeImpl) o;
- return true;
+ return inIterable == node.inIterable && Objects.equals(index, node.index) && Objects.equals(key, node.key)
+ && Objects.equals(name, node.name) && kind == node.kind;
}
/**
@@ -243,12 +242,7 @@ public class NodeImpl implements Path.Node, Serializable {
*/
@Override
public int hashCode() {
- int result = name != null ? name.hashCode() : 0;
- result = 31 * result + (inIterable ? 1 : 0);
- result = 31 * result + (index != null ? index.hashCode() : 0);
- result = 31 * result + (key != null ? key.hashCode() : 0);
- result = 31 * result + (kind != null ? kind.hashCode() : 0);
- return result;
+ return Objects.hash(name, Boolean.valueOf(inIterable), index, key, kind);
}
public int getParameterIndex() {
@@ -263,12 +257,24 @@ public class NodeImpl implements Path.Node, Serializable {
this.parameterTypes = parameterTypes;
}
+ public Class<?> getContainerClass() {
+ return containerType;
+ }
+
+ public Integer getTypeArgumentIndex() {
+ return typeArgumentIndex;
+ }
+
+ public void inContainer(Class<?> containerType, Integer typeArgumentIndex) {
+ this.containerType = containerType;
+ this.typeArgumentIndex = typeArgumentIndex;
+ }
+
+ @SuppressWarnings("serial")
public static class ParameterNodeImpl extends NodeImpl implements Path.ParameterNode {
public ParameterNodeImpl(final Node cast) {
super(cast);
- if (ParameterNodeImpl.class.isInstance(cast)) {
- setParameterIndex(ParameterNodeImpl.class.cast(cast).getParameterIndex());
- }
+ optional(Path.ParameterNode.class, cast).ifPresent(n -> setParameterIndex(n.getParameterIndex()));
}
public ParameterNodeImpl(final String name, final int idx) {
@@ -282,12 +288,11 @@ public class NodeImpl implements Path.Node, Serializable {
}
}
+ @SuppressWarnings("serial")
public static class ConstructorNodeImpl extends NodeImpl implements Path.ConstructorNode {
public ConstructorNodeImpl(final Node cast) {
super(cast);
- if (NodeImpl.class.isInstance(cast)) {
- setParameterTypes(NodeImpl.class.cast(cast).parameterTypes);
- }
+ optional(Path.ConstructorNode.class, cast).ifPresent(n -> setParameterTypes(n.getParameterTypes()));
}
public ConstructorNodeImpl(final String simpleName, List<Class<?>> paramTypes) {
@@ -301,6 +306,7 @@ public class NodeImpl implements Path.Node, Serializable {
}
}
+ @SuppressWarnings("serial")
public static class CrossParameterNodeImpl extends NodeImpl implements Path.CrossParameterNode {
public CrossParameterNodeImpl() {
super("<cross-parameter>");
@@ -316,12 +322,11 @@ public class NodeImpl implements Path.Node, Serializable {
}
}
+ @SuppressWarnings("serial")
public static class MethodNodeImpl extends NodeImpl implements Path.MethodNode {
public MethodNodeImpl(final Node cast) {
super(cast);
- if (MethodNodeImpl.class.isInstance(cast)) {
- setParameterTypes(MethodNodeImpl.class.cast(cast).getParameterTypes());
- }
+ optional(Path.MethodNode.class, cast).ifPresent(n -> setParameterTypes(n.getParameterTypes()));
}
public MethodNodeImpl(final String name, final List<Class<?>> classes) {
@@ -335,6 +340,7 @@ public class NodeImpl implements Path.Node, Serializable {
}
}
+ @SuppressWarnings("serial")
public static class ReturnValueNodeImpl extends NodeImpl implements Path.ReturnValueNode {
public ReturnValueNodeImpl(final Node cast) {
super(cast);
@@ -350,6 +356,7 @@ public class NodeImpl implements Path.Node, Serializable {
}
}
+ @SuppressWarnings("serial")
public static class PropertyNodeImpl extends NodeImpl implements Path.PropertyNode {
public PropertyNodeImpl(final String name) {
super(name);
@@ -357,6 +364,8 @@ public class NodeImpl implements Path.Node, Serializable {
public PropertyNodeImpl(final Node cast) {
super(cast);
+ optional(Path.PropertyNode.class, cast)
+ .ifPresent(n -> inContainer(n.getContainerClass(), n.getTypeArgumentIndex()));
}
@Override
@@ -365,6 +374,7 @@ public class NodeImpl implements Path.Node, Serializable {
}
}
+ @SuppressWarnings("serial")
public static class BeanNodeImpl extends NodeImpl implements Path.BeanNode {
public BeanNodeImpl() {
// no-op
@@ -372,6 +382,8 @@ public class NodeImpl implements Path.Node, Serializable {
public BeanNodeImpl(final Node cast) {
super(cast);
+ optional(Path.BeanNode.class, cast)
+ .ifPresent(n -> inContainer(n.getContainerClass(), n.getTypeArgumentIndex()));
}
@Override
@@ -379,4 +391,24 @@ public class NodeImpl implements Path.Node, Serializable {
return ElementKind.BEAN;
}
}
+
+ @SuppressWarnings("serial")
+ public static class ContainerElementNodeImpl extends NodeImpl implements Path.ContainerElementNode {
+
+ public ContainerElementNodeImpl(String name, Class<?> containerType, Integer typeArgumentIndex) {
+ super(name);
+ inContainer(containerType, typeArgumentIndex);
+ }
+
+ public ContainerElementNodeImpl(final Node cast) {
+ super(cast);
+ optional(Path.ContainerElementNode.class, cast)
+ .ifPresent(n -> inContainer(n.getContainerClass(), n.getTypeArgumentIndex()));
+ }
+
+ @Override
+ public ElementKind getKind() {
+ return ElementKind.CONTAINER_ELEMENT;
+ }
+ }
}
http://git-wip-us.apache.org/repos/asf/bval/blob/fad43eb8/bval-jsr/src/main/java/org/apache/bval/jsr/util/PathImpl.java
----------------------------------------------------------------------
diff --git a/bval-jsr/src/main/java/org/apache/bval/jsr/util/PathImpl.java b/bval-jsr/src/main/java/org/apache/bval/jsr/util/PathImpl.java
index 59fba83..430d257 100644
--- a/bval-jsr/src/main/java/org/apache/bval/jsr/util/PathImpl.java
+++ b/bval-jsr/src/main/java/org/apache/bval/jsr/util/PathImpl.java
@@ -18,16 +18,19 @@
*/
package org.apache.bval.jsr.util;
-import javax.validation.Path;
import java.io.Serializable;
-import java.util.ArrayList;
import java.util.Iterator;
+import java.util.LinkedList;
import java.util.List;
+import java.util.Objects;
+
+import javax.validation.Path;
+
+import org.apache.bval.util.Exceptions;
/**
- * Description: object holding the property path as a list of nodes.
- * (Implementation partially based on reference implementation)
- * <br/>
+ * Description: object holding the property path as a list of nodes. (Implementation partially based on reference
+ * implementation) <br/>
* This class is not synchronized.
*
* @version $Rev: 1498347 $ $Date: 2013-07-01 12:06:18 +0200 (lun., 01 juil. 2013) $
@@ -41,8 +44,8 @@ public class PathImpl implements Path, Serializable {
/**
* Builds non-root paths from expressions.
*/
- private static class PathImplBuilder implements PathNavigation.Callback<PathImpl> {
- PathImpl result = new PathImpl();
+ public static class Builder implements PathNavigation.Callback<PathImpl> {
+ private final PathImpl result = PathImpl.create();
/**
* {@inheritDoc}
@@ -72,9 +75,6 @@ public class PathImpl implements Path, Serializable {
*/
@Override
public PathImpl result() {
- if (result.nodeList.isEmpty()) {
- throw new IllegalStateException();
- }
return result;
}
@@ -85,11 +85,8 @@ public class PathImpl implements Path, Serializable {
public void handleGenericInIterable() {
result.addNode(NodeImpl.atIndex(null));
}
-
}
- private final List<NodeImpl> nodeList;
-
/**
* Returns a {@code Path} instance representing the path described by the given string. To create a root node the
* empty string should be passed. Note: This signature is to maintain pluggability with the RI impl.
@@ -102,7 +99,7 @@ public class PathImpl implements Path, Serializable {
if (propertyPath == null || propertyPath.isEmpty()) {
return create();
}
- return PathNavigation.navigateAndReturn(propertyPath, new PathImplBuilder());
+ return PathNavigation.navigateAndReturn(propertyPath, new Builder());
}
/**
@@ -127,6 +124,10 @@ public class PathImpl implements Path, Serializable {
return path == null ? null : new PathImpl(path);
}
+ public static PathImpl of(Path path) {
+ return path instanceof PathImpl ? (PathImpl) path : copy(path);
+ }
+
private static NodeImpl newNode(final Node cast) {
if (PropertyNode.class.isInstance(cast)) {
return new NodeImpl.PropertyNodeImpl(cast);
@@ -140,9 +141,6 @@ public class PathImpl implements Path, Serializable {
if (ConstructorNode.class.isInstance(cast)) {
return new NodeImpl.ConstructorNodeImpl(cast);
}
- if (ConstructorNode.class.isInstance(cast)) {
- return new NodeImpl.ConstructorNodeImpl(cast);
- }
if (ReturnValueNode.class.isInstance(cast)) {
return new NodeImpl.ReturnValueNodeImpl(cast);
}
@@ -152,18 +150,19 @@ public class PathImpl implements Path, Serializable {
if (CrossParameterNode.class.isInstance(cast)) {
return new NodeImpl.CrossParameterNodeImpl(cast);
}
+ if (ContainerElementNode.class.isInstance(cast)) {
+ return new NodeImpl.ContainerElementNodeImpl(cast);
+ }
return new NodeImpl(cast);
}
+ private final LinkedList<NodeImpl> nodeList = new LinkedList<>();
+
private PathImpl() {
- nodeList = new ArrayList<NodeImpl>();
}
- private PathImpl(Iterable<Node> path) {
- this();
- for (final Node node : path) {
- nodeList.add(newNode(node));
- }
+ private PathImpl(Iterable<? extends Node> nodes) {
+ nodes.forEach(n -> nodeList.add(newNode(n)));
}
/**
@@ -176,7 +175,7 @@ public class PathImpl implements Path, Serializable {
if (nodeList.size() != 1) {
return false;
}
- Path.Node first = nodeList.get(0);
+ final Path.Node first = nodeList.peekFirst();
return !first.isInIterable() && first.getName() == null;
}
@@ -186,13 +185,10 @@ public class PathImpl implements Path, Serializable {
* @return PathImpl
*/
public PathImpl getPathWithoutLeafNode() {
- List<Node> nodes = new ArrayList<Node>(nodeList);
- PathImpl path = null;
- if (nodes.size() > 1) {
- nodes.remove(nodes.size() - 1);
- path = new PathImpl(nodes);
+ if (nodeList.size() < 2) {
+ return null;
}
- return path;
+ return new PathImpl(nodeList.subList(0, nodeList.size() - 1));
}
/**
@@ -202,12 +198,11 @@ public class PathImpl implements Path, Serializable {
* to add
*/
public void addNode(Node node) {
- NodeImpl impl = node instanceof NodeImpl ? (NodeImpl) node : newNode(node);
+ final NodeImpl impl = node instanceof NodeImpl ? (NodeImpl) node : newNode(node);
if (isRootPath()) {
- nodeList.set(0, impl);
- } else {
- nodeList.add(impl);
+ nodeList.pop();
}
+ nodeList.add(impl);
}
/**
@@ -229,7 +224,6 @@ public class PathImpl implements Path, Serializable {
return;
}
}
-
final NodeImpl node;
if ("<cross-parameter>".equals(name)) {
node = new NodeImpl.CrossParameterNodeImpl();
@@ -237,7 +231,6 @@ public class PathImpl implements Path, Serializable {
node = new NodeImpl.PropertyNodeImpl(name);
}
addNode(node);
-
}
/**
@@ -248,11 +241,10 @@ public class PathImpl implements Path, Serializable {
* if no nodes are found
*/
public NodeImpl removeLeafNode() {
- if (isRootPath() || nodeList.isEmpty()) {
- throw new IllegalStateException("No nodes in path!");
- }
+ Exceptions.raiseIf(isRootPath() || nodeList.isEmpty(), IllegalStateException::new, "No nodes in path!");
+
try {
- return nodeList.remove(nodeList.size() - 1);
+ return nodeList.removeLast();
} finally {
if (nodeList.isEmpty()) {
nodeList.add(new NodeImpl((String) null));
@@ -269,7 +261,7 @@ public class PathImpl implements Path, Serializable {
if (nodeList.isEmpty()) {
return null;
}
- return (NodeImpl) nodeList.get(nodeList.size() - 1);
+ return nodeList.peekLast();
}
/**
@@ -292,14 +284,14 @@ public class PathImpl implements Path, Serializable {
if (path instanceof PathImpl && ((PathImpl) path).isRootPath()) {
return true;
}
- Iterator<Node> pathIter = path.iterator();
- Iterator<Node> thisIter = iterator();
+ final Iterator<Node> pathIter = path.iterator();
+ final Iterator<Node> thisIter = iterator();
while (pathIter.hasNext()) {
- Node pathNode = pathIter.next();
+ final Node pathNode = pathIter.next();
if (!thisIter.hasNext()) {
return false;
}
- Node thisNode = thisIter.next();
+ final Node thisNode = thisIter.next();
if (pathNode.isInIterable()) {
if (!thisNode.isInIterable()) {
return false;
@@ -328,7 +320,7 @@ public class PathImpl implements Path, Serializable {
*/
@Override
public String toString() {
- StringBuilder builder = new StringBuilder();
+ final StringBuilder builder = new StringBuilder();
for (Path.Node node : this) {
NodeImpl.appendNode(node, builder);
}
@@ -346,9 +338,7 @@ public class PathImpl implements Path, Serializable {
if (o == null || !getClass().equals(o.getClass())) {
return false;
}
-
- PathImpl path = (PathImpl) o;
- return nodeList == path.nodeList || nodeList != null && nodeList.equals(path.nodeList);
+ return Objects.equals(nodeList, ((PathImpl) o).nodeList);
}
/**
@@ -356,7 +346,6 @@ public class PathImpl implements Path, Serializable {
*/
@Override
public int hashCode() {
- return nodeList == null ? 0 : nodeList.hashCode();
+ return Objects.hashCode(nodeList);
}
-
}
http://git-wip-us.apache.org/repos/asf/bval/blob/fad43eb8/bval-jsr/src/main/java/org/apache/bval/jsr/util/PathNavigation.java
----------------------------------------------------------------------
diff --git a/bval-jsr/src/main/java/org/apache/bval/jsr/util/PathNavigation.java b/bval-jsr/src/main/java/org/apache/bval/jsr/util/PathNavigation.java
index 36fb919..7ba6bc3 100644
--- a/bval-jsr/src/main/java/org/apache/bval/jsr/util/PathNavigation.java
+++ b/bval-jsr/src/main/java/org/apache/bval/jsr/util/PathNavigation.java
@@ -18,12 +18,17 @@ package org.apache.bval.jsr.util;
import javax.validation.ValidationException;
+import org.apache.bval.util.Exceptions;
import org.apache.commons.lang3.StringEscapeUtils;
+import org.apache.commons.lang3.Validate;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.text.ParsePosition;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
import java.util.logging.Logger;
/**
@@ -67,13 +72,13 @@ public class PathNavigation {
/**
* Callback "procedure" that always returns null.
*/
- public static abstract class CallbackProcedure implements Callback<Object> {
+ public static abstract class CallbackProcedure implements Callback<Void> {
/**
* {@inheritDoc}
*/
@Override
- public final Object result() {
+ public final Void result() {
complete();
return null;
}
@@ -85,6 +90,34 @@ public class PathNavigation {
}
}
+ public static class CompositeCallbackProcedure extends CallbackProcedure {
+ private final List<Callback<?>> delegates;
+
+ public CompositeCallbackProcedure(Callback<?>... delegates) {
+ this(new ArrayList<>(Arrays.asList(delegates)));
+ }
+
+ public CompositeCallbackProcedure(List<Callback<?>> delegates) {
+ super();
+ this.delegates = Validate.notNull(delegates);
+ }
+
+ @Override
+ public void handleProperty(String name) {
+ delegates.forEach(d -> d.handleProperty(name));
+ }
+
+ @Override
+ public void handleIndexOrKey(String value) {
+ delegates.forEach(d -> d.handleIndexOrKey(value));
+ }
+
+ @Override
+ public void handleGenericInIterable() {
+ delegates.forEach(Callback::handleGenericInIterable);
+ }
+ }
+
private static class QuotedStringParser {
String parseQuotedString(CharSequence path, PathPosition pos) throws Exception {
final int len = path.length();
@@ -118,8 +151,7 @@ public class PathNavigation {
@Override
protected void handleNextChar(CharSequence path, PathPosition pos, Writer target) throws IOException {
- final int
- codePoints = StringEscapeUtils.UNESCAPE_JAVA.translate(path, pos.getIndex(), target);
+ final int codePoints = StringEscapeUtils.UNESCAPE_JAVA.translate(path, pos.getIndex(), target);
if (codePoints == 0) {
super.handleNextChar(path, pos, target);
} else {
@@ -128,13 +160,12 @@ public class PathNavigation {
}
}
}
-
}
private static final Logger LOG = Logger.getLogger(PathNavigation.class.getName());
private static final QuotedStringParser QUOTED_STRING_PARSER;
-
+
static {
QuotedStringParser quotedStringParser;
try {
@@ -164,10 +195,10 @@ public class PathNavigation {
public static <T> T navigateAndReturn(CharSequence propertyPath, Callback<? extends T> callback) {
try {
parse(propertyPath == null ? "" : propertyPath, new PathPosition(callback));
- } catch (ValidationException ex) {
+ } catch (ValidationException | IllegalArgumentException ex) {
throw ex;
- } catch (Exception ex) {
- throw new ValidationException(String.format("invalid property: %s", propertyPath), ex);
+ } catch (Exception e) {
+ Exceptions.raise(ValidationException::new, e, "invalid property: %s", propertyPath);
}
return callback.result();
}
@@ -190,23 +221,21 @@ public class PathNavigation {
char c = path.charAt(here);
switch (c) {
case ']':
- throw new IllegalStateException(String.format("Position %s: unexpected '%s'", here, c));
+ Exceptions.raise(IllegalStateException::new, "Position %s: unexpected '%s'", here, c);
case '[':
handleIndex(path, pos.next());
break;
case '.':
- if (sep) {
- throw new IllegalStateException(
- String.format("Position %s: expected property, index/key, or end of expression", here));
- }
+ Exceptions.raiseIf(sep, IllegalStateException::new,
+ "Position %s: expected property, index/key, or end of expression", here);
+
sep = true;
pos.next();
// fall through:
default:
- if (!sep) {
- throw new IllegalStateException(String.format(
- "Position %s: expected property path separator, index/key, or end of expression", here));
- }
+ Exceptions.raiseUnless(sep, IllegalStateException::new,
+ "Position %s: expected property path separator, index/key, or end of expression", here);
+
pos.handleProperty(parseProperty(path, pos));
}
sep = false;
@@ -214,8 +243,8 @@ public class PathNavigation {
}
private static String parseProperty(CharSequence path, PathPosition pos) throws Exception {
- int len = path.length();
- int start = pos.getIndex();
+ final int len = path.length();
+ final int start = pos.getIndex();
loop: while (pos.getIndex() < len) {
switch (path.charAt(pos.getIndex())) {
case '[':
@@ -225,27 +254,28 @@ public class PathNavigation {
}
pos.next();
}
- if (pos.getIndex() > start) {
- return path.subSequence(start, pos.getIndex()).toString();
- }
- throw new IllegalStateException(String.format("Position %s: expected property", start));
+ Exceptions.raiseIf(pos.getIndex() == start, IllegalStateException::new, "Position %s: expected property",
+ start);
+
+ return path.subSequence(start, pos.getIndex()).toString();
}
/**
* Handles an index/key. If the text contained between [] is surrounded by a pair of " or ', it will be treated as a
- * string which may contain Java escape sequences. This function is only available if commons-lang3 is available on the classpath!
+ * string which may contain Java escape sequences. This function is only available if commons-lang3 is available on
+ * the classpath!
*
* @param path
* @param pos
* @throws Exception
*/
private static void handleIndex(CharSequence path, PathPosition pos) throws Exception {
- int len = path.length();
- int start = pos.getIndex();
+ final int len = path.length();
+ final int start = pos.getIndex();
if (start < len) {
- char first = path.charAt(pos.getIndex());
+ final char first = path.charAt(pos.getIndex());
if (first == '"' || first == '\'') {
- String s = QUOTED_STRING_PARSER.parseQuotedString(path, pos);
+ final String s = QUOTED_STRING_PARSER.parseQuotedString(path, pos);
if (s != null && path.charAt(pos.getIndex()) == ']') {
pos.handleIndexOrKey(s);
pos.next();
@@ -254,7 +284,7 @@ public class PathNavigation {
}
// no quoted string; match ] greedily
while (pos.getIndex() < len) {
- int here = pos.getIndex();
+ final int here = pos.getIndex();
try {
if (path.charAt(here) == ']') {
if (here == start) {
@@ -269,13 +299,13 @@ public class PathNavigation {
}
}
}
- throw new IllegalStateException(String.format("Position %s: unparsable index", start));
+ Exceptions.raise(IllegalStateException::new, "Position %s: unparsable index", start);
}
/**
* ParsePosition/Callback
*/
- private static class PathPosition extends ParsePosition implements Callback<Object> {
+ private static class PathPosition extends ParsePosition implements Callback<Void> {
final Callback<?> delegate;
/**
@@ -336,7 +366,7 @@ public class PathNavigation {
* {@inheritDoc}
*/
@Override
- public Object result() {
+ public Void result() {
return null;
}
@@ -344,9 +374,8 @@ public class PathNavigation {
* {@inheritDoc}
*/
/*
- * Override equals to make findbugs happy;
- * would simply ignore but doesn't seem to be possible at the inner class level
- * without attaching the filter to the containing class.
+ * Override equals to make findbugs happy; would simply ignore but doesn't seem to be possible at the inner
+ * class level without attaching the filter to the containing class.
*/
@Override
public boolean equals(Object obj) {
@@ -364,5 +393,4 @@ public class PathNavigation {
return super.hashCode();
}
}
-
}
http://git-wip-us.apache.org/repos/asf/bval/blob/fad43eb8/bval-jsr/src/main/java/org/apache/bval/jsr/util/Proxies.java
----------------------------------------------------------------------
diff --git a/bval-jsr/src/main/java/org/apache/bval/jsr/util/Proxies.java b/bval-jsr/src/main/java/org/apache/bval/jsr/util/Proxies.java
index 1fa033c..1d0c1ee 100644
--- a/bval-jsr/src/main/java/org/apache/bval/jsr/util/Proxies.java
+++ b/bval-jsr/src/main/java/org/apache/bval/jsr/util/Proxies.java
@@ -26,7 +26,7 @@ public final class Proxies {
private static final Set<String> KNOWN_PROXY_CLASSNAMES;
static {
- final Set<String> s = new HashSet<String>();
+ final Set<String> s = new HashSet<>();
s.add("org.jboss.weld.bean.proxy.ProxyObject");
KNOWN_PROXY_CLASSNAMES = Collections.unmodifiableSet(s);
}
http://git-wip-us.apache.org/repos/asf/bval/blob/fad43eb8/bval-jsr/src/main/java/org/apache/bval/jsr/util/ToUnmodifiable.java
----------------------------------------------------------------------
diff --git a/bval-jsr/src/main/java/org/apache/bval/jsr/util/ToUnmodifiable.java b/bval-jsr/src/main/java/org/apache/bval/jsr/util/ToUnmodifiable.java
new file mode 100644
index 0000000..a470e0d
--- /dev/null
+++ b/bval-jsr/src/main/java/org/apache/bval/jsr/util/ToUnmodifiable.java
@@ -0,0 +1,34 @@
+package org.apache.bval.jsr.util;
+
+import java.util.Collections;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Function;
+import java.util.function.Supplier;
+import java.util.stream.Collector;
+import java.util.stream.Collectors;
+
+/**
+ * Utility {@link Collector} definitions.
+ */
+public class ToUnmodifiable {
+
+ public static <T> Collector<T, ?, Set<T>> set(Supplier<Set<T>> set) {
+ return Collectors.collectingAndThen(Collectors.toCollection(set), Collections::unmodifiableSet);
+ }
+
+ public static <T> Collector<T, ?, Set<T>> set() {
+ return Collectors.collectingAndThen(Collectors.toCollection(LinkedHashSet::new), Collections::unmodifiableSet);
+ }
+
+ public static <T> Collector<T, ?, List<T>> list() {
+ return Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList);
+ }
+
+ public static <T, K, U> Collector<T, ?, Map<K, U>> map(Function<? super T, ? extends K> keyMapper,
+ Function<? super T, ? extends U> valueMapper) {
+ return Collectors.collectingAndThen(Collectors.toMap(keyMapper, valueMapper), Collections::unmodifiableMap);
+ }
+}