1package jdiff;
2
3import java.io.*;
4import java.util.*;
5
6/**
7 * Track the various modifiers for a program element.
8 *
9 * The method used for Collection comparison (compareTo) must make its
10 * comparison based upon everything that is known about this set of modifiers.
11 *
12 * See the file LICENSE.txt for copyright details.
13 * @author Matthew Doar, mdoar@pobox.com
14 */
15class Modifiers implements Comparable {
16
17    /** Set if the program element is static. */
18    public boolean isStatic = false;
19
20    /** Set if the program element is final. */
21    public boolean isFinal = false;
22
23    /** Set if the program element is deprecated. */
24    public boolean isDeprecated = false;
25
26    /**
27     * The visibility level; "public", "protected", "package" or
28     * "private"
29     */
30    public String visibility = null;
31
32    /** Default constructor. */
33    public Modifiers() {
34    }
35
36    /** Compare two Modifiers objects by their contents. */
37    public int compareTo(Object o) {
38        Modifiers oModifiers = (Modifiers)o;
39        if (isStatic != oModifiers.isStatic)
40            return -1;
41        if (isFinal != oModifiers.isFinal)
42            return -1;
43        if (isDeprecated != oModifiers.isDeprecated)
44            return -1;
45        if (visibility != null) {
46            int comp = visibility.compareTo(oModifiers.visibility);
47            if (comp != 0)
48                return comp;
49        }
50        return 0;
51    }
52
53    /**
54     * Generate a String describing the differences between the current
55     * (old) Modifiers object and a new Modifiers object. The string has
56     * no leading space, but does end in a period.
57     *
58     * @param newModifiers The new Modifiers object.
59     * @return The description of the differences, null if there is no change.
60     */
61    public String diff(Modifiers newModifiers) {
62        String res = "";
63        boolean hasContent = false;
64        if (isStatic != newModifiers.isStatic) {
65            res += "Change from ";
66            if (isStatic)
67                res += "static to non-static.<br>";
68            else
69                res += "non-static to static.<br>";
70            hasContent = true;
71        }
72        if (isFinal != newModifiers.isFinal) {
73            if (hasContent)
74                res += " ";
75            res += "Change from ";
76            if (isFinal)
77                res += "final to non-final.<br>";
78            else
79                res += "non-final to final.<br>";
80            hasContent = true;
81        }
82        if (isDeprecated != newModifiers.isDeprecated) {
83            if (hasContent)
84                res += " ";
85            if (isDeprecated)
86                res += "Change from deprecated to undeprecated.<br>";
87            else
88                res += "<b>Now deprecated</b>.<br>";
89            hasContent = true;
90        }
91        if (visibility != null) {
92            int comp = visibility.compareTo(newModifiers.visibility);
93            if (comp != 0) {
94                if (hasContent)
95                    res += " ";
96                res += "Change of visibility from " + visibility + " to " +
97                    newModifiers.visibility + ".<br>";
98                hasContent = true;
99            }
100        }
101        if (res.compareTo("") == 0)
102            return null;
103        return res;
104    }
105}
106