1package jdiff;
2
3import java.io.*;
4import 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@pobox.com
15 */
16class 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        ConstructorAPI constructorAPI = (ConstructorAPI)o;
85        if (compareNullIsLeast(name_, constructorAPI.name_) == 0 &&
86                compareNullIsLeast(type_, constructorAPI.type_) == 0)
87            return true;
88        return false;
89    }
90
91    /**
92     * Tests two methods for equality, using just the signature.
93     */
94    public boolean equalSignatures(Object o) {
95        if (getSignature().compareTo(((MethodAPI)o).getSignature()) == 0)
96            return true;
97        return false;
98    }
99
100    /** Cached result of getSignature(). */
101    private String signature_ = null;
102
103    /** Return the signature of the method. */
104    public String getSignature() {
105        if (signature_ != null)
106            return signature_;
107        if (params_ == null)
108            return type_;
109        String res = "";
110        boolean first = true;
111        Iterator iter = params_.iterator();
112        while (iter.hasNext()) {
113            if (!first)
114                res += ", ";
115            ParamAPI param = (ParamAPI)(iter.next());
116            res += param.toString();
117            first = false;
118        }
119        signature_ = res;
120        return res;
121    }
122}
123