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 java.util.*;
016import java.util.function.*;
017
018/**
019 * An ordered list of annotations and the classes/methods/packages they were found on.
020 */
021public class AnnotationList extends ArrayList<AnnotationInfo<?>> {
022   private static final long serialVersionUID = 1L;
023
024   private final Predicate<AnnotationInfo<?>> filter;
025
026   /**
027    * Constructor.
028    *
029    * No filtering.
030    */
031   public AnnotationList() {
032      this(null);
033   }
034
035   /**
036    * Constructor with optional filter.
037    *
038    * @param filter The filter to use to filter entries added to this list.
039    */
040   public AnnotationList(Predicate<AnnotationInfo<?>> filter) {
041      this.filter = filter;
042   }
043
044   @Override /* List */
045   public boolean add(AnnotationInfo<?> ai) {
046      if (filter == null || filter.test(ai)) {
047         super.add(ai);
048         return true;
049      }
050      return false;
051   }
052
053   private static final Comparator<AnnotationInfo<?>> RANK_COMPARATOR = new Comparator<AnnotationInfo<?>>() {
054      @Override
055      public int compare(AnnotationInfo<?> o1, AnnotationInfo<?> o2) {
056         return o1.rank - o2.rank;
057      }
058   };
059
060   /**
061    * Sort the annotations in this list based on rank.
062    *
063    * @return This object (for method chaining).
064    */
065   public AnnotationList sort() {
066      Collections.sort(this, RANK_COMPARATOR);
067      return this;
068   }
069}