Skip to content

Commit

Permalink
Changed a couple ArrayListMultimaps back to SetMultimaps where sets s…
Browse files Browse the repository at this point in the history
…hould still be used
  • Loading branch information
drallen committed May 28, 2010
1 parent a13d040 commit f9a8509
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 8 deletions.
Expand Up @@ -23,6 +23,7 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -37,12 +38,14 @@
import org.jboss.weld.metadata.TypeStore;
import org.jboss.weld.resources.ClassTransformer;
import org.jboss.weld.util.Proxies;
import org.jboss.weld.util.collections.ArraySetSupplier;
import org.jboss.weld.util.collections.Arrays2;
import org.jboss.weld.util.collections.ImmutableArraySet;
import org.jboss.weld.util.reflection.HierarchyDiscovery;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
import com.google.common.collect.SetMultimap;

/**
* Represents functionality common for all annotated items, mainly different
Expand Down Expand Up @@ -126,7 +129,7 @@ private static void addMetaAnnotation(Multimap<Class<? extends Annotation>, Anno
private final Map<Class<? extends Annotation>, Annotation> annotationMap;
// The meta-annotation map (annotation type -> set of annotations containing
// meta-annotation) of the item
private final ArrayListMultimap<Class<? extends Annotation>, Annotation> metaAnnotationMap;
private final SetMultimap<Class<? extends Annotation>, Annotation> metaAnnotationMap;

private final Class<T> rawType;
private final Type[] actualTypeArguments;
Expand All @@ -150,13 +153,13 @@ public AbstractWeldAnnotated(Map<Class<? extends Annotation>, Annotation> annota
throw new WeldException(ANNOTATION_MAP_NULL);
}
this.annotationMap = annotationMap;
this.metaAnnotationMap = ArrayListMultimap.<Class<? extends Annotation>, Annotation> create();
this.metaAnnotationMap = Multimaps.newSetMultimap(new HashMap<Class<? extends Annotation>, Collection<Annotation>>(), ArraySetSupplier.<Annotation>instance());
for (Annotation annotation : annotationMap.values())
{
addMetaAnnotations(metaAnnotationMap, annotation, annotation.annotationType().getAnnotations(), false);
addMetaAnnotations(metaAnnotationMap, annotation, classTransformer.getTypeStore().get(annotation.annotationType()), false);
}
this.metaAnnotationMap.trimToSize();
ArraySetSupplier.trimSetsToSize(metaAnnotationMap);

if (declaredAnnotationMap == null)
{
Expand All @@ -183,13 +186,14 @@ protected AbstractWeldAnnotated(Map<Class<? extends Annotation>, Annotation> ann
throw new WeldException(ANNOTATION_MAP_NULL);
}
this.annotationMap = annotationMap;
this.metaAnnotationMap = ArrayListMultimap.<Class<? extends Annotation>, Annotation> create();
this.metaAnnotationMap = Multimaps.newSetMultimap(new HashMap<Class<? extends Annotation>, Collection<Annotation>>(), ArraySetSupplier.<Annotation>instance());
for (Annotation annotation : annotationMap.values())
{
addMetaAnnotations(metaAnnotationMap, annotation, annotation.annotationType().getAnnotations(), false);
addMetaAnnotations(metaAnnotationMap, annotation, typeStore.get(annotation.annotationType()), false);
this.annotationMap.put(annotation.annotationType(), annotation);
}
this.metaAnnotationMap.trimToSize();
ArraySetSupplier.trimSetsToSize(metaAnnotationMap);

if (declaredAnnotationMap == null)
{
Expand Down
Expand Up @@ -48,12 +48,15 @@
import org.jboss.weld.introspector.WeldMethod;
import org.jboss.weld.resources.ClassTransformer;
import org.jboss.weld.util.Names;
import org.jboss.weld.util.collections.ArraySetSupplier;
import org.jboss.weld.util.collections.ImmutableArraySet;
import org.jboss.weld.util.reflection.HierarchyDiscovery;
import org.jboss.weld.util.reflection.Reflections;
import org.jboss.weld.util.reflection.SecureReflections;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimaps;
import com.google.common.collect.SetMultimap;

/**
* Represents an annotated class
Expand Down Expand Up @@ -120,7 +123,7 @@ private static <T> void mapConstructorAnnotations(ArrayListMultimap<Class<? exte

// The meta-annotation map (annotation type -> set of annotations containing
// meta-annotation) of the item
private final ArrayListMultimap<Class<? extends Annotation>, Annotation> declaredMetaAnnotationMap;
private final SetMultimap<Class<? extends Annotation>, Annotation> declaredMetaAnnotationMap;

private final boolean discovered;

Expand Down Expand Up @@ -352,13 +355,14 @@ protected WeldClassImpl(Class<T> rawType, Type type, AnnotatedType<T> annotatedT
this.declaredAnnotatedMethods.trimToSize();
this.declaredMethodsByAnnotatedParameters.trimToSize();

this.declaredMetaAnnotationMap = ArrayListMultimap.<Class<? extends Annotation>, Annotation> create();
this.declaredMetaAnnotationMap = Multimaps.newSetMultimap(new HashMap<Class<? extends Annotation>, Collection<Annotation>>(), ArraySetSupplier.<Annotation>instance());
for (Annotation declaredAnnotation : declaredAnnotationMap.values())
{
addMetaAnnotations(declaredMetaAnnotationMap, declaredAnnotation, declaredAnnotation.annotationType().getAnnotations(), true);
addMetaAnnotations(declaredMetaAnnotationMap, declaredAnnotation, classTransformer.getTypeStore().get(declaredAnnotation.annotationType()), true);
this.declaredMetaAnnotationMap.put(declaredAnnotation.annotationType(), declaredAnnotation);
}
ArraySetSupplier.trimSetsToSize(declaredMetaAnnotationMap);
}

@SuppressWarnings("unchecked")
Expand Down
Expand Up @@ -146,6 +146,11 @@ public <T> T[] toArray(T[] a)
return elements.toArray(a);
}

public void trimToSize()
{
elements.trimToSize();
}

// Needed to provide set equals semantics
@Override
public boolean equals(Object obj)
Expand Down
@@ -0,0 +1,64 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat, Inc. and/or its affiliates, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed 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.jboss.weld.util.collections;

import java.util.Set;

import com.google.common.base.Supplier;
import com.google.common.collect.SetMultimap;

/**
* Provides new instances of {@link ArraySet} to Google collections.
*
* @author David Allen
*
*/
public class ArraySetSupplier<V> implements Supplier<Set<V>>
{
private static final Supplier<?> INSTANCE = new ArraySetSupplier<Object>();

private ArraySetSupplier() {}

@SuppressWarnings("unchecked")
public static <V> Supplier<Set<V>> instance()
{
return (Supplier<Set<V>>) INSTANCE;
}

public Set<V> get()
{
return new ArraySet<V>();
}

/**
* Helper method which will trim each set in the multimap to its current size.
* @param <K> Key type
* @param <V> Value type
* @param multimap the set multimap using ArraySet<V> as the values
*/
public static <K, V> void trimSetsToSize(SetMultimap<K, V> multimap)
{
for (K key : multimap.keySet())
{
if (multimap.get(key) instanceof ArraySet<?>)
{
((ArraySet<?>)multimap.get(key)).trimToSize();
}
}
}
}

0 comments on commit f9a8509

Please sign in to comment.