Home | History | Annotate | Download | only in nodeTypes
      1 /*
      2  * Copyright (C) 2007-2010 Jlio Vilmar Gesser.
      3  * Copyright (C) 2011, 2013-2015 The JavaParser Team.
      4  *
      5  * This file is part of JavaParser.
      6  *
      7  * JavaParser can be used either under the terms of
      8  * a) the GNU Lesser General Public License as published by
      9  *     the Free Software Foundation, either version 3 of the License, or
     10  *     (at your option) any later version.
     11  * b) the terms of the Apache License
     12  *
     13  * You should have received a copy of both licenses in LICENCE.LGPL and
     14  * LICENCE.APACHE. Please refer to those files for details.
     15  *
     16  * JavaParser is distributed in the hope that it will be useful,
     17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     19  * GNU Lesser General Public License for more details.
     20  */
     21 
     22 package com.github.javaparser.ast.nodeTypes;
     23 
     24 import com.github.javaparser.ast.type.Type;
     25 
     26 import java.util.*;
     27 
     28 import static com.github.javaparser.utils.Utils.arrayToList;
     29 
     30 /**
     31  * A node that can have type arguments.
     32  * <pre>
     33  *     new X();        --> typeArguments == null
     34  *     new X&lt;>();      --> typeArguments.types = [], typeArguments.diamondOperator=true
     35  *     new X&lt;C,D>();   --> typeArguments.types = [C,D], typeArguments.diamondOperator=false
     36  * </pre>
     37  */
     38 public interface NodeWithTypeArguments<T> {
     39     /**
     40      * @return the types that can be found in the type arguments: &lt;String, Integer>.
     41      */
     42     List<Type<?>> getTypeArguments();
     43 
     44     /**
     45      * Allows you to set the generic arguments
     46      * @param typeArguments The list of types of the generics
     47      */
     48     T setTypeArguments(List<Type<?>> typeArguments);
     49 
     50     /**
     51      * @return whether the type arguments look like &lt;>.
     52      */
     53     default boolean isUsingDiamondOperator() {
     54         if(getTypeArguments()==null){
     55             return false;
     56         }
     57         return getTypeArguments().isEmpty();
     58     }
     59 
     60     /**
     61      * Sets the type arguments to &lt>.
     62      */
     63     default T setDiamondOperator() {
     64         final List<Type<?>> empty = new LinkedList<>();
     65         setTypeArguments(empty);
     66         return (T) this;
     67     }
     68 
     69     /**
     70      * Removes all type arguments, including the surrounding &lt;>.
     71      */
     72     default T removeTypeArguments() {
     73         setTypeArguments((List<Type<?>>) null);
     74         return (T) this;
     75     }
     76 
     77     default T setTypeArguments(Type<?>... typeArguments) {
     78         setTypeArguments(arrayToList(typeArguments));
     79         return (T) this;
     80     }
     81 }
     82