KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > IntAnnotation


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2003-2005, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs;
21
22 import java.io.IOException JavaDoc;
23
24 import edu.umd.cs.findbugs.xml.XMLAttributeList;
25 import edu.umd.cs.findbugs.xml.XMLOutput;
26
27 /**
28  * Bug annotation class for integer values.
29  *
30  * @author David Hovemeyer
31  * @see BugAnnotation
32  */

33 public class IntAnnotation implements BugAnnotation {
34     private static final long serialVersionUID = 1L;
35
36     private static final String JavaDoc DEFAULT_ROLE = "INT_DEFAULT";
37
38     private int value;
39     private String JavaDoc description;
40
41     /**
42      *
43      */

44     public static final String JavaDoc INT_SYNC_PERCENT = "INT_SYNC_PERCENT";
45     public static final String JavaDoc INT_OCCURRENCES = "INT_OCCURRENCES";
46
47     /**
48      * Constructor.
49      *
50      * @param value the integer value
51      */

52     public IntAnnotation(int value) {
53         this.value = value;
54         this.description = DEFAULT_ROLE;
55     }
56     
57     //@Override
58
@Override JavaDoc
59     public Object JavaDoc clone() {
60         try {
61             return super.clone();
62         } catch (CloneNotSupportedException JavaDoc e) {
63             throw new AssertionError JavaDoc(e);
64         }
65     }
66
67     /**
68      * Get the integer value.
69      *
70      * @return the integer value
71      */

72     public int getValue() {
73         return value;
74     }
75
76     public void accept(BugAnnotationVisitor visitor) {
77         visitor.visitIntAnnotation(this);
78     }
79
80     public String JavaDoc format(String JavaDoc key, ClassAnnotation primaryClass) {
81         if (!isSignificant()) return "";
82         return String.valueOf(value);
83     }
84
85     public void setDescription(String JavaDoc description) {
86         this.description = description;
87     }
88
89     public String JavaDoc getDescription() {
90         return description;
91     }
92
93     @Override JavaDoc
94     public int hashCode() {
95         return value;
96     }
97
98     @Override JavaDoc
99     public boolean equals(Object JavaDoc o) {
100         if (!(o instanceof IntAnnotation))
101             return false;
102         return value == ((IntAnnotation) o).value;
103     }
104
105     public int compareTo(BugAnnotation o) {
106         if (!(o instanceof IntAnnotation)) // BugAnnotations must be Comparable with any type of BugAnnotation
107
return this.getClass().getName().compareTo(o.getClass().getName());
108         return value - ((IntAnnotation) o).value;
109     }
110
111     @Override JavaDoc
112     public String JavaDoc toString() {
113         String JavaDoc pattern = I18N.instance().getAnnotationDescription(description);
114         FindBugsMessageFormat format = new FindBugsMessageFormat(pattern);
115         return format.format(new BugAnnotation[]{this}, null);
116     }
117
118     /* ----------------------------------------------------------------------
119      * XML Conversion support
120      * ---------------------------------------------------------------------- */

121
122     private static final String JavaDoc ELEMENT_NAME = "Int";
123
124     public void writeXML(XMLOutput xmlOutput) throws IOException JavaDoc {
125         writeXML(xmlOutput, false);
126     }
127
128     public void writeXML(XMLOutput xmlOutput, boolean addMessages) throws IOException JavaDoc {
129         XMLAttributeList attributeList = new XMLAttributeList()
130             .addAttribute("value", String.valueOf(value));
131         
132         String JavaDoc role = getDescription();
133         if (!role.equals(DEFAULT_ROLE))
134             attributeList.addAttribute("role", role);
135         
136         BugAnnotationUtil.writeXML(xmlOutput, ELEMENT_NAME, this, attributeList, addMessages);
137     }
138  
139     public boolean isSignificant() {
140         return !description.equals(INT_SYNC_PERCENT) && !description.equals(INT_OCCURRENCES);
141     }
142 }
143
144 // vim:ts=4
145
Popular Tags