KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > imports > AccessResult


1 ////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2005 Oliver Burn
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
package com.puppycrawl.tools.checkstyle.checks.imports;
20
21 import java.io.Serializable JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25 /**
26  * Represents the result of an access check.
27  *
28  * @author Oliver Burn
29  */

30 final class AccessResult
31         implements Serializable JavaDoc
32 {
33     /** Numeric value for access result ALLOWED. */
34     private static final int CODE_ALLOWED = 10;
35     /** Numeric value for access result DISALLOWED. */
36     private static final int CODE_DISALLOWED = 20;
37     /** Numeric value for access result UNKNOWN. */
38     private static final int CODE_UNKNOWN = 30;
39     /** Label for access result ALLOWED. */
40     private static final String JavaDoc LABEL_ALLOWED = "ALLOWED";
41     /** Label for access result DISALLOWED. */
42     private static final String JavaDoc LABEL_DISALLOWED = "DISALLOWED";
43     /** Label for access result UNKNOWN. */
44     private static final String JavaDoc LABEL_UNKNOWN = "UNKNOWN";
45
46     /** Represents that access is allowed. */
47     public static final AccessResult ALLOWED = new AccessResult(CODE_ALLOWED,
48             LABEL_ALLOWED);
49     /** Represents that access is disallowed. */
50     public static final AccessResult DISALLOWED = new AccessResult(
51             CODE_DISALLOWED, LABEL_DISALLOWED);
52     /** Represents that access is unknown. */
53     public static final AccessResult UNKNOWN = new AccessResult(CODE_UNKNOWN,
54             LABEL_UNKNOWN);
55
56     /** map from results names to the respective result */
57     private static final Map JavaDoc NAME_TO_LEVEL = new HashMap JavaDoc();
58     static {
59         NAME_TO_LEVEL.put(LABEL_ALLOWED, ALLOWED);
60         NAME_TO_LEVEL.put(LABEL_DISALLOWED, DISALLOWED);
61         NAME_TO_LEVEL.put(LABEL_UNKNOWN, UNKNOWN);
62     }
63
64     /** Code for the access result. */
65     private final int mCode;
66     /** Label for the access result. */
67     private final String JavaDoc mLabel;
68
69     /**
70      * Constructs an instance.
71      *
72      * @param aCode the code for the result.
73      * @param aLabel the label for the result.
74      */

75     private AccessResult(final int aCode, final String JavaDoc aLabel)
76     {
77         mCode = aCode;
78         mLabel = aLabel.trim();
79     }
80
81     /**
82      * @return the label for the result.
83      */

84     String JavaDoc getLabel()
85     {
86         return mLabel;
87     }
88
89     /** {@inheritDoc} */
90     public String JavaDoc toString()
91     {
92         return getLabel();
93     }
94
95     /** {@inheritDoc} */
96     public boolean equals(Object JavaDoc aObj)
97     {
98         boolean result = false;
99
100         if ((aObj instanceof AccessResult)
101                 && (((AccessResult) aObj).mCode == this.mCode))
102         {
103             result = true;
104         }
105
106         return result;
107     }
108
109     /** {@inheritDoc} */
110     public int hashCode()
111     {
112         return mCode;
113     }
114
115     /**
116      * SeverityLevel factory method.
117      *
118      * @param aName access result name.
119      * @return the {@link AccessResult} associated with the supplied name.
120      */

121     public static AccessResult getInstance(String JavaDoc aName)
122     {
123         // canonicalize argument
124
final String JavaDoc arName = aName.trim();
125
126         final AccessResult retVal = (AccessResult) NAME_TO_LEVEL.get(arName);
127         if (retVal == null) {
128             throw new IllegalArgumentException JavaDoc(arName);
129         }
130         return retVal;
131     }
132
133     /**
134      * Ensures that we don't get multiple instances of one SeverityLevel
135      * during deserialization. See Section 3.6 of the Java Object
136      * Serialization Specification for details.
137      *
138      * @return the serialization replacement object
139      */

140     private Object JavaDoc readResolve()
141     {
142         return getInstance(mLabel);
143     }
144
145 }
146
Popular Tags