< prev index next >

src/java.compiler/share/classes/javax/lang/model/util/ElementFilter.java

Print this page


   1 /*
   2  * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.lang.model.util;
  27 
  28 import java.util.Collections;
  29 import java.util.List;
  30 import java.util.Set;
  31 import java.util.EnumSet;
  32 import java.util.ArrayList;
  33 import java.util.LinkedHashSet;
  34 
  35 import javax.lang.model.element.*;
  36 import javax.lang.model.element.ModuleElement.Directive;
  37 import javax.lang.model.element.ModuleElement.DirectiveKind;
  38 import javax.lang.model.element.ModuleElement.ExportsDirective;

  39 import javax.lang.model.element.ModuleElement.ProvidesDirective;
  40 import javax.lang.model.element.ModuleElement.RequiresDirective;
  41 import javax.lang.model.element.ModuleElement.UsesDirective;
  42 
  43 
  44 /**
  45  * Filters for selecting just the elements of interest from a
  46  * collection of elements.  The returned sets and lists are new
  47  * collections and do use the argument as a backing store.  The
  48  * methods in this class do not make any attempts to guard against
  49  * concurrent modifications of the arguments.  The returned sets and
  50  * lists are mutable but unsafe for concurrent access.  A returned set
  51  * has the same iteration order as the argument set to a method.
  52  *
  53  * <p>If iterables and sets containing {@code null} are passed as
  54  * arguments to methods in this class, a {@code NullPointerException}
  55  * will be thrown.
  56  *
  57  * <p>Note that a <i>static import</i> statement can make the text of
  58  * calls to the methods in this class more concise; for example:


 222         for (Element e : elements) {
 223             if (targetKinds.contains(e.getKind()))
 224                 list.add(clazz.cast(e));
 225         }
 226         return list;
 227     }
 228 
 229     // Assumes targetKinds and E are sensible.
 230     private static <E extends Element> Set<E> setFilter(Set<? extends Element> elements,
 231                                                         Set<ElementKind> targetKinds,
 232                                                         Class<E> clazz) {
 233         // Return set preserving iteration order of input set.
 234         Set<E> set = new LinkedHashSet<>();
 235         for (Element e : elements) {
 236             if (targetKinds.contains(e.getKind()))
 237                 set.add(clazz.cast(e));
 238         }
 239         return set;
 240     }
 241 
 242 
 243 
 244     /**
 245      * Returns a list of export directives in {@code directives}.
 246      * @return a list of export directives in {@code directives}
 247      * @param directives the directives to filter
 248      * @since 9
 249      */
 250     public static List<ExportsDirective>
 251             exportsIn(Iterable<? extends Directive> directives) {
 252         return listFilter(directives, DirectiveKind.EXPORTS, ExportsDirective.class);
 253     }
 254 
 255     /**
 256      * Returns a list of provides directives in {@code directives}.
 257      * @return a list of provides directives in {@code directives}











 258      * @param directives the directives to filter
 259      * @since 9
 260      */
 261     public static List<ProvidesDirective>
 262             providesIn(Iterable<? extends Directive> directives) {
 263         return listFilter(directives, DirectiveKind.PROVIDES, ProvidesDirective.class);
 264     }
 265 
 266     /**
 267      * Returns a list of requires directives in {@code directives}.
 268      * @return a list of requires directives in {@code directives}
 269      * @param directives the directives to filter
 270      * @since 9
 271      */
 272     public static List<RequiresDirective>
 273             requiresIn(Iterable<? extends Directive> directives) {
 274         return listFilter(directives, DirectiveKind.REQUIRES, RequiresDirective.class);
 275     }
 276 
 277     /**
 278      * Returns a list of uses directives in {@code directives}.
 279      * @return a list of uses directives in {@code directives}
 280      * @param directives the directives to filter
 281      * @since 9
 282      */
 283     public static List<UsesDirective>
 284             usesIn(Iterable<? extends Directive> directives) {
 285         return listFilter(directives, DirectiveKind.USES, UsesDirective.class);
 286     }
 287 
 288     // Assumes directiveKind and D are sensible.
 289     private static <D extends Directive> List<D> listFilter(Iterable<? extends Directive> directives,
 290                                                           DirectiveKind directiveKind,
 291                                                           Class<D> clazz) {
 292         List<D> list = new ArrayList<>();
 293         for (Directive d : directives) {
 294             if (d.getKind() == directiveKind)
 295                 list.add(clazz.cast(d));
 296         }
 297         return list;
 298     }
 299 }
   1 /*
   2  * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.lang.model.util;
  27 
  28 import java.util.Collections;
  29 import java.util.List;
  30 import java.util.Set;
  31 import java.util.EnumSet;
  32 import java.util.ArrayList;
  33 import java.util.LinkedHashSet;
  34 
  35 import javax.lang.model.element.*;
  36 import javax.lang.model.element.ModuleElement.Directive;
  37 import javax.lang.model.element.ModuleElement.DirectiveKind;
  38 import javax.lang.model.element.ModuleElement.ExportsDirective;
  39 import javax.lang.model.element.ModuleElement.OpensDirective;
  40 import javax.lang.model.element.ModuleElement.ProvidesDirective;
  41 import javax.lang.model.element.ModuleElement.RequiresDirective;
  42 import javax.lang.model.element.ModuleElement.UsesDirective;
  43 
  44 
  45 /**
  46  * Filters for selecting just the elements of interest from a
  47  * collection of elements.  The returned sets and lists are new
  48  * collections and do use the argument as a backing store.  The
  49  * methods in this class do not make any attempts to guard against
  50  * concurrent modifications of the arguments.  The returned sets and
  51  * lists are mutable but unsafe for concurrent access.  A returned set
  52  * has the same iteration order as the argument set to a method.
  53  *
  54  * <p>If iterables and sets containing {@code null} are passed as
  55  * arguments to methods in this class, a {@code NullPointerException}
  56  * will be thrown.
  57  *
  58  * <p>Note that a <i>static import</i> statement can make the text of
  59  * calls to the methods in this class more concise; for example:


 223         for (Element e : elements) {
 224             if (targetKinds.contains(e.getKind()))
 225                 list.add(clazz.cast(e));
 226         }
 227         return list;
 228     }
 229 
 230     // Assumes targetKinds and E are sensible.
 231     private static <E extends Element> Set<E> setFilter(Set<? extends Element> elements,
 232                                                         Set<ElementKind> targetKinds,
 233                                                         Class<E> clazz) {
 234         // Return set preserving iteration order of input set.
 235         Set<E> set = new LinkedHashSet<>();
 236         for (Element e : elements) {
 237             if (targetKinds.contains(e.getKind()))
 238                 set.add(clazz.cast(e));
 239         }
 240         return set;
 241     }
 242 


 243     /**
 244      * Returns a list of {@code exports} directives in {@code directives}.
 245      * @return a list of {@code exports} directives in {@code directives}
 246      * @param directives the directives to filter
 247      * @since 9
 248      */
 249     public static List<ExportsDirective>
 250             exportsIn(Iterable<? extends Directive> directives) {
 251         return listFilter(directives, DirectiveKind.EXPORTS, ExportsDirective.class);
 252     }
 253 
 254     /**
 255      * Returns a list of {@code opens} directives in {@code directives}.
 256      * @return a list of {@code opens} directives in {@code directives}
 257      * @param directives the directives to filter
 258      * @since 9
 259      */
 260     public static List<OpensDirective>
 261             opensIn(Iterable<? extends Directive> directives) {
 262         return listFilter(directives, DirectiveKind.OPENS, OpensDirective.class);
 263     }
 264 
 265     /**
 266      * Returns a list of {@code provides} directives in {@code directives}.
 267      * @return a list of {@code provides} directives in {@code directives}
 268      * @param directives the directives to filter
 269      * @since 9
 270      */
 271     public static List<ProvidesDirective>
 272             providesIn(Iterable<? extends Directive> directives) {
 273         return listFilter(directives, DirectiveKind.PROVIDES, ProvidesDirective.class);
 274     }
 275 
 276     /**
 277      * Returns a list of {@code requires} directives in {@code directives}.
 278      * @return a list of {@code requires} directives in {@code directives}
 279      * @param directives the directives to filter
 280      * @since 9
 281      */
 282     public static List<RequiresDirective>
 283             requiresIn(Iterable<? extends Directive> directives) {
 284         return listFilter(directives, DirectiveKind.REQUIRES, RequiresDirective.class);
 285     }
 286 
 287     /**
 288      * Returns a list of {@code uses} directives in {@code directives}.
 289      * @return a list of {@code uses} directives in {@code directives}
 290      * @param directives the directives to filter
 291      * @since 9
 292      */
 293     public static List<UsesDirective>
 294             usesIn(Iterable<? extends Directive> directives) {
 295         return listFilter(directives, DirectiveKind.USES, UsesDirective.class);
 296     }
 297 
 298     // Assumes directiveKind and D are sensible.
 299     private static <D extends Directive> List<D> listFilter(Iterable<? extends Directive> directives,
 300                                                           DirectiveKind directiveKind,
 301                                                           Class<D> clazz) {
 302         List<D> list = new ArrayList<>();
 303         for (Directive d : directives) {
 304             if (d.getKind() == directiveKind)
 305                 list.add(clazz.cast(d));
 306         }
 307         return list;
 308     }
 309 }
< prev index next >