Skip to content
Permalink
Browse files
caching in the bean descriptor model
  • Loading branch information
mbenson committed Feb 8, 2019
1 parent fdd108f commit 870209bcf7c94648bcf7fff6ae706f3252245764
Showing 2 changed files with 125 additions and 4 deletions.
@@ -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 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,16 +47,23 @@
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);
this.beanClass = reader.meta.getHost();

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 MethodDescriptor getConstraintsForMethod(String methodName, Class<?>... p

@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 ConstructorDescriptor getConstraintsForConstructor(Class<?>... parameterT

@Override
public Set<ConstructorDescriptor> getConstrainedConstructors() {
return constructors.values().stream().collect(ToUnmodifiable.set());
return constrainedConstructors;
}

public PropertyDescriptor getProperty(String propertyName) {
@@ -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();
}
}

0 comments on commit 870209b

Please sign in to comment.