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 2019/02/08 23:25:03 UTC
[bval] branch master updated: caching in the bean descriptor model
This is an automated email from the ASF dual-hosted git repository.
mbenson pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/bval.git
The following commit(s) were added to refs/heads/master by this push:
new 870209b caching in the bean descriptor model
870209b is described below
commit 870209bcf7c94648bcf7fff6ae706f3252245764
Author: Matt Benson <mb...@apache.org>
AuthorDate: Fri Feb 8 17:24:57 2019 -0600
caching in the bean descriptor model
---
.../java/org/apache/bval/jsr/descriptor/BeanD.java | 25 ++++-
.../java/org/apache/bval/util/CollectionSet.java | 104 +++++++++++++++++++++
2 files changed, 125 insertions(+), 4 deletions(-)
diff --git a/bval-jsr/src/main/java/org/apache/bval/jsr/descriptor/BeanD.java b/bval-jsr/src/main/java/org/apache/bval/jsr/descriptor/BeanD.java
index f6b6473..72c9b83 100644
--- a/bval-jsr/src/main/java/org/apache/bval/jsr/descriptor/BeanD.java
+++ b/bval-jsr/src/main/java/org/apache/bval/jsr/descriptor/BeanD.java
@@ -19,7 +19,9 @@
package org.apache.bval.jsr.descriptor;
import java.lang.reflect.Type;
+import java.util.Collections;
import java.util.EnumSet;
+import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
@@ -33,6 +35,7 @@ import javax.validation.metadata.PropertyDescriptor;
import org.apache.bval.jsr.groups.GroupStrategy;
import org.apache.bval.jsr.metadata.Signature;
import org.apache.bval.jsr.util.ToUnmodifiable;
+import org.apache.bval.util.CollectionSet;
import org.apache.bval.util.Exceptions;
import org.apache.bval.util.StringUtils;
@@ -44,6 +47,9 @@ public class BeanD<T> extends ElementD<Class<T>, MetadataReader.ForBean<T>> impl
private final Map<Signature, ConstructorD<T>> constructors;
private final Map<Signature, MethodD> methods;
private final GroupStrategy groupStrategy;
+
+ private final Set<ConstructorDescriptor> constrainedConstructors;
+ private final Map<Set<MethodType>, Set<MethodDescriptor>> methodCache = new HashMap<>();
BeanD(MetadataReader.ForBean<T> reader) {
super(reader);
@@ -51,9 +57,13 @@ public class BeanD<T> extends ElementD<Class<T>, MetadataReader.ForBean<T>> impl
groupStrategy = reader.getGroupStrategy();
propertiesMap = reader.getProperties(this);
- properties = propertiesMap.values().stream().filter(DescriptorManager::isConstrained).collect(ToUnmodifiable.set());
+ properties =
+ propertiesMap.values().stream().filter(DescriptorManager::isConstrained).collect(ToUnmodifiable.set());
constructors = reader.getConstructors(this);
methods = reader.getMethods(this);
+
+ constrainedConstructors =
+ constructors.isEmpty() ? Collections.emptySet() : new CollectionSet<>(constructors.values());
}
@Override
@@ -85,8 +95,15 @@ public class BeanD<T> extends ElementD<Class<T>, MetadataReader.ForBean<T>> impl
@Override
public Set<MethodDescriptor> getConstrainedMethods(MethodType methodType, MethodType... methodTypes) {
- final EnumSet<MethodType> filter = EnumSet.of(methodType, methodTypes);
- return methods.values().stream().filter(m -> filter.contains(m.getMethodType())).collect(ToUnmodifiable.set());
+ return methodCache.computeIfAbsent(EnumSet.of(methodType, methodTypes), k -> {
+ if (methods.isEmpty() || k.isEmpty()) {
+ return Collections.emptySet();
+ }
+ if (k.size() == MethodType.values().length) {
+ return new CollectionSet<>(methods.values());
+ }
+ return methods.values().stream().filter(m -> k.contains(m.getMethodType())).collect(ToUnmodifiable.set());
+ });
}
@Override
@@ -96,7 +113,7 @@ public class BeanD<T> extends ElementD<Class<T>, MetadataReader.ForBean<T>> impl
@Override
public Set<ConstructorDescriptor> getConstrainedConstructors() {
- return constructors.values().stream().collect(ToUnmodifiable.set());
+ return constrainedConstructors;
}
public PropertyDescriptor getProperty(String propertyName) {
diff --git a/bval-jsr/src/main/java/org/apache/bval/util/CollectionSet.java b/bval-jsr/src/main/java/org/apache/bval/util/CollectionSet.java
new file mode 100644
index 0000000..6484c60
--- /dev/null
+++ b/bval-jsr/src/main/java/org/apache/bval/util/CollectionSet.java
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.bval.util;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.Set;
+
+/**
+ * Unmodifiable {@link Set} backed by a {@link Collection} obeying {@link Set} semantics.
+ *
+ * @param <E>
+ */
+public class CollectionSet<E> implements Set<E> {
+ private final Collection<E> wrapped;
+
+ public CollectionSet(Collection<? extends E> wrapped) {
+ super();
+ this.wrapped = Collections.unmodifiableCollection(Validate.notNull(wrapped));
+ Validate.isTrue(wrapped.stream().distinct().count() == wrapped.size(), "Collection values are not distinct");
+ }
+
+ @Override
+ public int size() {
+ return wrapped.size();
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return wrapped.isEmpty();
+ }
+
+ @Override
+ public boolean contains(Object o) {
+ return wrapped.contains(o);
+ }
+
+ @Override
+ public Iterator<E> iterator() {
+ return wrapped.iterator();
+ }
+
+ @Override
+ public Object[] toArray() {
+ return wrapped.toArray();
+ }
+
+ @Override
+ public <T> T[] toArray(T[] a) {
+ return wrapped.toArray(a);
+ }
+
+ @Override
+ public boolean add(E e) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean remove(Object o) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean containsAll(Collection<?> c) {
+ return wrapped.containsAll(c);
+ }
+
+ @Override
+ public boolean addAll(Collection<? extends E> c) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean retainAll(Collection<?> c) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean removeAll(Collection<?> c) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void clear() {
+ throw new UnsupportedOperationException();
+ }
+}