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 = (o1, o2) -> o1.rank - o2.rank;
033
034   /**
035    * Sort the annotations in this list based on rank.
036    *
037    * @return This object.
038    */
039   public AnnotationList sort() {
040      Collections.sort(this, RANK_COMPARATOR);
041      return this;
042   }
043
044   /**
045    * Performs an action on the specified matching values from all annotations in this list.
046    *
047    * @param <T> The annotation value type.
048    * @param type The annotation value type.
049    * @param name The annotation value name.
050    * @param filter A predicate to apply to the value to determine if action should be performed.  Can be <jk>null</jk>.
051    * @param action An action to perform on the value.
052    * @return This object.
053    */
054   public <T> AnnotationList forEachValue(Class<T> type, String name, Predicate<T> filter, Consumer<T> action) {
055      forEach(x -> x.forEachValue(type, name, filter, action));
056      return this;
057   }
058
059   /**
060    * Performs an action on all matching annotations in this list.
061    *
062    * @param <A> The annotation type.
063    * @param type The annotation type.
064    * @param filter A predicate to apply to the entries to determine if action should be performed.  Can be <jk>null</jk>.
065    * @param action An action to perform on the entry.
066    * @return This object.
067    */
068   @SuppressWarnings("unchecked")
069   public <A extends Annotation> AnnotationList forEach(Class<A> type, Predicate<AnnotationInfo<A>> filter, Consumer<AnnotationInfo<A>> action) {
070      forEach(x -> {
071         if (x.isType(type))
072            consume(filter, action, (AnnotationInfo<A>)x);
073      });
074      return this;
075   }
076
077   /**
078    * Performs an action on all matching annotations in this list.
079    *
080    * @param <A> The annotation type.
081    * @param filter A predicate to apply to the entries to determine if action should be performed.  Can be <jk>null</jk>.
082    * @param action An action to perform on the entry.
083    * @return This object.
084    */
085   public <A extends Annotation> AnnotationList forEach(Predicate<AnnotationInfo<?>> filter, Consumer<AnnotationInfo<?>> action) {
086      forEach(x -> consume(filter, action, x));
087      return this;
088   }
089}