001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.reflect;
014
015import static org.apache.juneau.internal.ConsumerUtils.*;
016
017import java.lang.annotation.*;
018import java.util.*;
019import java.util.function.*;
020
021/**
022 * An ordered list of annotations and the classes/methods/packages they were found on.
023 *
024 * <h5 class='section'>See Also:</h5><ul>
025 * </ul>
026 *
027 * @serial exclude
028 */
029public final class AnnotationList extends ArrayList<AnnotationInfo<?>> {
030   private static final long serialVersionUID = 1L;
031
032   private static final Comparator<AnnotationInfo<?>> RANK_COMPARATOR = new Comparator<>() {
033      @Override
034      public int compare(AnnotationInfo<?> o1, AnnotationInfo<?> o2) {
035         return o1.rank - o2.rank;
036      }
037   };
038
039   /**
040    * Sort the annotations in this list based on rank.
041    *
042    * @return This object.
043    */
044   public AnnotationList sort() {
045      Collections.sort(this, RANK_COMPARATOR);
046      return this;
047   }
048
049   /**
050    * Performs an action on the specified matching values from all annotations in this list.
051    *
052    * @param <T> The annotation value type.
053    * @param type The annotation value type.
054    * @param name The annotation value name.
055    * @param filter A predicate to apply to the value to determine if action should be performed.  Can be <jk>null</jk>.
056    * @param action An action to perform on the value.
057    * @return This object.
058    */
059   public <T> AnnotationList forEachValue(Class<T> type, String name, Predicate<T> filter, Consumer<T> action) {
060      forEach(x -> x.forEachValue(type, name, filter, action));
061      return this;
062   }
063
064   /**
065    * Performs an action on all matching annotations in this list.
066    *
067    * @param <A> The annotation type.
068    * @param type The annotation type.
069    * @param filter A predicate to apply to the entries to determine if action should be performed.  Can be <jk>null</jk>.
070    * @param action An action to perform on the entry.
071    * @return This object.
072    */
073   @SuppressWarnings("unchecked")
074   public <A extends Annotation> AnnotationList forEach(Class<A> type, Predicate<AnnotationInfo<A>> filter, Consumer<AnnotationInfo<A>> action) {
075      forEach(x -> {
076         if (x.isType(type))
077            consume(filter, action, (AnnotationInfo<A>)x);
078      });
079      return this;
080   }
081
082   /**
083    * Performs an action on all matching annotations in this list.
084    *
085    * @param <A> The annotation type.
086    * @param filter A predicate to apply to the entries to determine if action should be performed.  Can be <jk>null</jk>.
087    * @param action An action to perform on the entry.
088    * @return This object.
089    */
090   public <A extends Annotation> AnnotationList forEach(Predicate<AnnotationInfo<?>> filter, Consumer<AnnotationInfo<?>> action) {
091      forEach(x -> consume(filter, action, x));
092      return this;
093   }
094}