Home | History | Annotate | Download | only in jdiff
      1 package jdiff;
      2 
      3 import java.io.*;
      4 import java.util.*;
      5 
      6 /**
      7  * Class to represent a constructor, analogous to ConstructorDoc in the
      8  * Javadoc doclet API.
      9  *
     10  * The method used for Collection comparison (compareTo) must make its
     11  * comparison based upon everything that is known about this constructor.
     12  *
     13  * See the file LICENSE.txt for copyright details.
     14  * @author Matthew Doar, mdoar (at) pobox.com
     15  */
     16 class ConstructorAPI implements Comparable {
     17     /**
     18      * Name of the constructor.
     19      * Either this or type_ must be non-null
     20      */
     21     public String name_ = null;
     22 
     23     /**
     24      * The type of the constructor, being all the parameter types
     25      * separated by commas.
     26      * Either this or name_ must be non-null.
     27      */
     28     public String type_ = null;
     29 
     30     /**
     31      * The exceptions thrown by this constructor, being all the exception types
     32      * separated by commas. "no exceptions" if no exceptions are thrown.
     33      */
     34     public String exceptions_ = "no exceptions";
     35 
     36     /** Modifiers for this class. */
     37     public Modifiers modifiers_;
     38 
     39     public List params_; // ParamAPI[]
     40 
     41     /** The doc block, default is null. */
     42     public String doc_ = null;
     43 
     44     /** Constructor. */
     45     public ConstructorAPI(String name, String type, Modifiers modifiers) {
     46         if (name == null && type == null) {
     47             throw new IllegalArgumentException("Cannot have constructor with both name and type"
     48                 + "being null");
     49         }
     50         name_ = name;
     51         type_ = type;
     52         modifiers_ = modifiers;
     53         params_ = new ArrayList();
     54     }
     55 
     56     private static <T extends Comparable<? super T>> int compareNullIsLeast(T c1, T c2) {
     57         return c1 == null ? (c2 == null ? 0 : -1) : (c2 == null ? 1 : c1.compareTo(c2));
     58     }
     59 
     60     /** Compare two ConstructorAPI objects by type and modifiers. */
     61     public int compareTo(Object o) {
     62         ConstructorAPI constructorAPI = (ConstructorAPI)o;
     63         int comp = compareNullIsLeast(name_, constructorAPI.name_);
     64         if (comp != 0)
     65             return comp;
     66         comp = compareNullIsLeast(getSignature(), constructorAPI.getSignature());
     67         if (comp != 0)
     68             return comp;
     69         comp = exceptions_.compareTo(constructorAPI.exceptions_);
     70         if (comp != 0)
     71             return comp;
     72         comp = modifiers_.compareTo(constructorAPI.modifiers_);
     73         if (comp != 0)
     74             return comp;
     75         if (APIComparator.docChanged(doc_, constructorAPI.doc_))
     76             return -1;
     77         return 0;
     78     }
     79 
     80     /**
     81      * Tests two constructors, using just the name and type, used by indexOf().
     82      */
     83     public boolean equals(Object o) {
     84 
     85         ConstructorAPI constructorAPI = (ConstructorAPI)o;
     86         if (compareNullIsLeast(name_, constructorAPI.name_) == 0 &&
     87                 compareNullIsLeast(getSignature(), constructorAPI.getSignature()) == 0)
     88             return true;
     89         return false;
     90     }
     91 
     92     /**
     93      * Tests two methods for equality, using just the signature.
     94      */
     95     public boolean equalSignatures(Object o) {
     96         if (getSignature().compareTo(((MethodAPI)o).getSignature()) == 0)
     97             return true;
     98         return false;
     99     }
    100 
    101     /** Cached result of getSignature(). */
    102     private String signature_ = null;
    103 
    104     /** Return the signature of the method. */
    105     public String getSignature() {
    106         if (signature_ != null)
    107             return signature_;
    108         if (params_ == null)
    109             return type_;
    110         String res = "";
    111         boolean first = true;
    112         Iterator iter = params_.iterator();
    113         while (iter.hasNext()) {
    114             if (!first)
    115                 res += ", ";
    116             ParamAPI param = (ParamAPI)(iter.next());
    117             res += param.toString();
    118             first = false;
    119         }
    120         signature_ = res;
    121         return res;
    122     }
    123 }