Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2008 The Guava Authors
   *
   * 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 com.google.common.collect.testing.features;
 
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;

Utilities for collecting and validating tester requirements from annotations.

Author(s):
George van den Driessche
 
 public class FeatureUtil {
  
A cache of annotated objects (typically a Class or Method) to its set of annotations.
 
   private static Map<AnnotatedElementAnnotation[]> annotationCache =
       new HashMap<AnnotatedElementAnnotation[]>();
 
   private static final Map<Class<?>, TesterRequirements>
           new HashMap<Class<?>, TesterRequirements>();

  
Given a set of features, add to it all the features directly or indirectly implied by any of them, and return it.

Parameters:
features the set of features to expand
Returns:
the same set of features, expanded with all implied features
 
   public static Set<Feature<?>> addImpliedFeatures(Set<Feature<?>> features) {
     // The base case of the recursion is an empty set of features, which will
     // occur when the previous set contained only simple features.
     if (!features.isEmpty()) {
       features.addAll(impliedFeatures(features));
     }
     return features;
   }

  
Given a set of features, return a new set of all features directly or indirectly implied by any of them.

Parameters:
features the set of features whose implications to find
Returns:
the implied set of features
 
   public static Set<Feature<?>> impliedFeatures(Set<Feature<?>> features) {
     Set<Feature<?>> implied = new LinkedHashSet<Feature<?>>();
     for (Feature<?> feature : features) {
       implied.addAll(feature.getImpliedFeatures());
     }
     addImpliedFeatures(implied);
     return implied;
   }

  
Get the full set of requirements for a tester class.

Parameters:
testerClass a tester class
Returns:
all the constraints implicitly or explicitly required by the class or any of its superclasses.
Throws:
ConflictingRequirementsException if the requirements are mutually inconsistent.
 
   public static TesterRequirements getTesterRequirements(Class<?> testerClass)
       throws ConflictingRequirementsException {
     synchronized () {
       TesterRequirements requirements =
           .get(testerClass);
       if (requirements == null) {
         requirements = buildTesterRequirements(testerClass);
         .put(testerClassrequirements);
       }
       return requirements;
     }
   }

  
Get the full set of requirements for a tester class.

Parameters:
testerMethod a test method of a tester class
Returns:
all the constraints implicitly or explicitly required by the method, its declaring class, or any of its superclasses.
Throws:
ConflictingRequirementsException if the requirements are mutually inconsistent.
  public static TesterRequirements getTesterRequirements(Method testerMethod)
    return buildTesterRequirements(testerMethod);
  }

  
Construct the full set of requirements for a tester class.

Parameters:
testerClass a tester class
Returns:
all the constraints implicitly or explicitly required by the class or any of its superclasses.
Throws:
ConflictingRequirementsException if the requirements are mutually inconsistent.
    final TesterRequirements declaredRequirements =
        buildDeclaredTesterRequirements(testerClass);
    Class<?> baseClass = testerClass.getSuperclass();
    if (baseClass == null) {
      return declaredRequirements;
    } else {
      final TesterRequirements clonedBaseRequirements =
          new TesterRequirements(getTesterRequirements(baseClass));
      return incorporateRequirements(
          clonedBaseRequirementsdeclaredRequirementstesterClass);
    }
  }

  
Construct the full set of requirements for a tester method.

Parameters:
testerMethod a test method of a tester class
Returns:
all the constraints implicitly or explicitly required by the method, its declaring class, or any of its superclasses.
Throws:
ConflictingRequirementsException if the requirements are mutually inconsistent.
    TesterRequirements clonedClassRequirements = new TesterRequirements(
        getTesterRequirements(testerMethod.getDeclaringClass()));
    TesterRequirements declaredRequirements =
        buildDeclaredTesterRequirements(testerMethod);
        clonedClassRequirementsdeclaredRequirementstesterMethod);
  }

  
Construct the set of requirements specified by annotations directly on a tester class or method.

Parameters:
classOrMethod a tester class or a test method thereof
Returns:
all the constraints implicitly or explicitly required by annotations on the class or method.
Throws:
ConflictingRequirementsException if the requirements are mutually inconsistent.
      AnnotatedElement classOrMethod)
    TesterRequirements requirements = new TesterRequirements();
    Iterable<AnnotationtesterAnnotations =
        getTesterAnnotations(classOrMethod);
    for (Annotation testerAnnotation : testerAnnotations) {
      TesterRequirements moreRequirements =
          buildTesterRequirements(testerAnnotation);
          requirementsmoreRequirementstesterAnnotation);
    }
    return requirements;
  }

  
Find all the tester annotations declared on a tester class or method.

Parameters:
classOrMethod a class or method whose tester annotations to find
Returns:
an iterable sequence of tester annotations on the class
      AnnotatedElement classOrMethod) {
    List<Annotationresult = new ArrayList<Annotation>();
    Annotation[] annotations;
    synchronized () {
      annotations = .get(classOrMethod);
      if (annotations == null) {
        annotations = classOrMethod.getDeclaredAnnotations();
        .put(classOrMethodannotations);
      }
    }
    for (Annotation a : annotations) {
      Class<? extends AnnotationannotationClass = a.annotationType();
      if (annotationClass.isAnnotationPresent(TesterAnnotation.class)) {
        result.add(a);
      }
    }
    return result;
  }

  
Find all the constraints explicitly or implicitly specified by a single tester annotation.

Parameters:
testerAnnotation a tester annotation
Returns:
the requirements specified by the annotation
Throws:
ConflictingRequirementsException if the requirements are mutually inconsistent.
      Annotation testerAnnotation)
    Class<? extends AnnotationannotationClass = testerAnnotation.getClass();
    final Feature<?>[] presentFeatures;
    final Feature<?>[] absentFeatures;
    try {
      presentFeatures = (Feature[]) annotationClass.getMethod("value")
          .invoke(testerAnnotation);
      absentFeatures = (Feature[]) annotationClass.getMethod("absent")
          .invoke(testerAnnotation);
    } catch (Exception e) {
      throw new IllegalArgumentException(
          "Error extracting features from tester annotation."e);
    }
    Set<Feature<?>> allPresentFeatures =
        addImpliedFeatures(Helpers.<Feature<?>>copyToSet(presentFeatures));
    Set<Feature<?>> allAbsentFeatures =
        addImpliedFeatures(Helpers.<Feature<?>>copyToSet(absentFeatures));
    Set<Feature<?>> conflictingFeatures =
        intersection(allPresentFeaturesallAbsentFeatures);
    if (!conflictingFeatures.isEmpty()) {
      throw new ConflictingRequirementsException("Annotation explicitly or " +
          "implicitly requires one or more features to be both present " +
          "and absent.",
          conflictingFeaturestesterAnnotation);
    }
    return new TesterRequirements(allPresentFeaturesallAbsentFeatures);
  }

  
Incorporate additional requirements into an existing requirements object.

Parameters:
requirements the existing requirements object
moreRequirements more requirements to incorporate
source the source of the additional requirements (used only for error reporting)
Returns:
the existing requirements object, modified to include the additional requirements
Throws:
ConflictingRequirementsException if the additional requirements are inconsistent with the existing requirements
      TesterRequirements requirementsTesterRequirements moreRequirements,
      Object sourcethrows ConflictingRequirementsException {
    Set<Feature<?>> presentFeatures = requirements.getPresentFeatures();
    Set<Feature<?>> absentFeatures = requirements.getAbsentFeatures();
    Set<Feature<?>> morePresentFeatures = moreRequirements.getPresentFeatures();
    Set<Feature<?>> moreAbsentFeatures = moreRequirements.getAbsentFeatures();
        "absent"absentFeatures,
        "present"morePresentFeaturessource);
        "present"presentFeatures,
        "absent"moreAbsentFeaturessource);
    presentFeatures.addAll(morePresentFeatures);
    absentFeatures.addAll(moreAbsentFeatures);
    return requirements;
  }
  // Used by incorporateRequirements() only
  private static void checkConflict(
      String earlierRequirementSet<Feature<?>> earlierFeatures,
      String newRequirementSet<Feature<?>> newFeatures,
      Object sourcethrows ConflictingRequirementsException {
    Set<Feature<?>> conflictingFeatures;
    conflictingFeatures = intersection(newFeaturesearlierFeatures);
    if (!conflictingFeatures.isEmpty()) {
      throw new ConflictingRequirementsException(String.format(
          "Annotation requires to be %s features that earlier " +
          "annotations required to be %s.",
              newRequirementearlierRequirement),
          conflictingFeaturessource);
    }
  }

  
Construct a new java.util.Set that is the intersection of the given sets.
  // Calls generic varargs method.
  @SuppressWarnings("unchecked")
  public static <T> Set<T> intersection(
      Set<? extends T> set1Set<? extends T> set2) {
    return intersection(new Set[] {set1set2});
  }

  
Construct a new java.util.Set that is the intersection of all the given sets.

Parameters:
sets the sets to intersect
Returns:
the intersection of the sets
Throws:
java.lang.IllegalArgumentException if there are no sets to intersect
  public static <T> Set<T> intersection(Set<? extends T> ... sets) {
    if (sets.length == 0) {
      throw new IllegalArgumentException(
          "Can't intersect no sets; would have to return the universe.");
    }
    Set<T> results = Helpers.copyToSet(sets[0]);
    for (int i = 1; i < sets.lengthi++) {
      Set<? extends T> set = sets[i];
      results.retainAll(set);
    }
    return results;
  }
New to GrepCode? Check out our FAQ X