View Javadoc

1   /*
2    *   Copyright (C) Christian Schulte, 2012-253
3    *   All rights reserved.
4    *
5    *   Redistribution and use in source and binary forms, with or without
6    *   modification, are permitted provided that the following conditions
7    *   are met:
8    *
9    *     o Redistributions of source code must retain the above copyright
10   *       notice, this list of conditions and the following disclaimer.
11   *
12   *     o Redistributions in binary form must reproduce the above copyright
13   *       notice, this list of conditions and the following disclaimer in
14   *       the documentation and/or other materials provided with the
15   *       distribution.
16   *
17   *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18   *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19   *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20   *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
21   *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22   *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23   *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24   *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25   *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26   *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27   *
28   *   $JOMC: JavaLanguage.java 4681 2012-12-29 00:30:19Z schulte $
29   *
30   */
31  package org.jomc.model;
32  
33  import java.util.HashSet;
34  import java.util.Set;
35  
36  /**
37   * Java language support.
38   *
39   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
40   * @version $JOMC: JavaLanguage.java 4681 2012-12-29 00:30:19Z schulte $
41   * @since 1.4
42   */
43  class JavaLanguage
44  {
45  
46      /**
47       * The Java Language Specification - Java SE 7 Edition - Chapter 18. Syntax
48       * <pre>
49       * BasicType:
50       *      byte
51       *      short
52       *      char
53       *      int
54       *      long
55       *      float
56       *      double
57       * </pre>
58       */
59      static final Set<String> BASIC_TYPES = new HashSet<String>( 8 );
60  
61      /**
62       * The Java Language Specification - Java SE 7 Edition - Chapter 3.10.3. Boolean Literals
63       * <pre>
64       * BooleanLiteral: one of
65       *      true false
66       * </pre>
67       */
68      static final Set<String> BOOLEAN_LITERALS = new HashSet<String>( 2 );
69  
70      /**
71       * The Java Language Specification - Java SE 7 Edition - Chapter 3.9. Keywords
72       * <pre>
73       * Keyword: one of
74       *      abstract   continue   for          new         switch
75       *      assert     default    if           package     synchronized
76       *      boolean    do         goto         private     this
77       *      break      double     implements   protected   throw
78       *      byte       else       import       public      throws
79       *      case       enum       instanceof   return      transient
80       *      catch      extends    int          short       try
81       *      char       final      interface    static      void
82       *      class      finally    long         strictfp    volatile
83       *      const      float      native       super       while
84       * </pre>
85       */
86      static final Set<String> KEYWORDS = new HashSet<String>( 50 );
87  
88      /**
89       * The Java Language Specification - Java SE 7 Edition - Chapter 3.10.7. The Null Literal
90       * <pre>
91       * NullLiteral:
92       *      null
93       * </pre>
94       */
95      static final String NULL_LITERAL = "null";
96  
97      static
98      {
99          // JLS - Java SE 7 Edition - Chapter 18. Syntax - BasicType
100         BASIC_TYPES.add( "boolean" );
101         BASIC_TYPES.add( "byte" );
102         BASIC_TYPES.add( "char" );
103         BASIC_TYPES.add( "double" );
104         BASIC_TYPES.add( "float" );
105         BASIC_TYPES.add( "short" );
106         BASIC_TYPES.add( "int" );
107         BASIC_TYPES.add( "long" );
108 
109         // JLS - Java SE 7 Edition - 3.10.3. Boolean Literals
110         BOOLEAN_LITERALS.add( "true" );
111         BOOLEAN_LITERALS.add( "false" );
112 
113         // JLS - Java SE 7 Edition - Chapter 3. Lexical Structure - 3.9. Keywords
114         KEYWORDS.add( "abstract" );
115         KEYWORDS.add( "assert" );
116         KEYWORDS.add( "boolean" );
117         KEYWORDS.add( "break" );
118         KEYWORDS.add( "byte" );
119         KEYWORDS.add( "case" );
120         KEYWORDS.add( "catch" );
121         KEYWORDS.add( "char" );
122         KEYWORDS.add( "class" );
123         KEYWORDS.add( "const" );
124         KEYWORDS.add( "continue" );
125         KEYWORDS.add( "default" );
126         KEYWORDS.add( "do" );
127         KEYWORDS.add( "double" );
128         KEYWORDS.add( "else" );
129         KEYWORDS.add( "enum" );
130         KEYWORDS.add( "extends" );
131         KEYWORDS.add( "final" );
132         KEYWORDS.add( "finally" );
133         KEYWORDS.add( "float" );
134         KEYWORDS.add( "for" );
135         KEYWORDS.add( "if" );
136         KEYWORDS.add( "goto" );
137         KEYWORDS.add( "implements" );
138         KEYWORDS.add( "import" );
139         KEYWORDS.add( "instanceof" );
140         KEYWORDS.add( "int" );
141         KEYWORDS.add( "interface" );
142         KEYWORDS.add( "long" );
143         KEYWORDS.add( "native" );
144         KEYWORDS.add( "new" );
145         KEYWORDS.add( "package" );
146         KEYWORDS.add( "private" );
147         KEYWORDS.add( "protected" );
148         KEYWORDS.add( "public" );
149         KEYWORDS.add( "return" );
150         KEYWORDS.add( "short" );
151         KEYWORDS.add( "static" );
152         KEYWORDS.add( "strictfp" );
153         KEYWORDS.add( "super" );
154         KEYWORDS.add( "switch" );
155         KEYWORDS.add( "synchronized" );
156         KEYWORDS.add( "this" );
157         KEYWORDS.add( "throw" );
158         KEYWORDS.add( "throws" );
159         KEYWORDS.add( "transient" );
160         KEYWORDS.add( "try" );
161         KEYWORDS.add( "void" );
162         KEYWORDS.add( "volatile" );
163         KEYWORDS.add( "while" );
164     }
165 
166     /**
167      * Tests whether a given string is case-insensitively equal to a Java keyword.
168      *
169      * @param s The string to test.
170      *
171      * @return {@code true}, if {@code s} is case-insensitively equal to a Java keyword; {@code false}, if not.
172      */
173     static boolean isKeyword( final String s )
174     {
175         for ( final String keyword : KEYWORDS )
176         {
177             if ( keyword.equalsIgnoreCase( s ) )
178             {
179                 return true;
180             }
181         }
182 
183         return false;
184     }
185 
186     /**
187      * Tests whether a given string is case-insensitively equal to a Java boolean literal.
188      *
189      * @param s The string to test.
190      *
191      * @return {@code true}, if {@code s} is case-insensitively equal to a Java boolean literal; {@code false}, if not.
192      */
193     static boolean isBooleanLiteral( final String s )
194     {
195         for ( final String literal : BOOLEAN_LITERALS )
196         {
197             if ( literal.equalsIgnoreCase( s ) )
198             {
199                 return true;
200             }
201         }
202 
203         return false;
204     }
205 
206     /**
207      * Tests whether a given string is case-insensitively equal to the Java {@code null} literal.
208      *
209      * @param s The string to test.
210      *
211      * @return {@code true}, if {@code s} is case-insensitively equal to the Java ยท{@code null} literal; {@code false},
212      * if not.
213      */
214     static boolean isNullLiteral( final String s )
215     {
216         return NULL_LITERAL.equalsIgnoreCase( s );
217     }
218 
219 }