Advertisement
MrLore

Java Keywords Utility Class v2

Dec 20th, 2012
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.HashSet;
  4.  
  5. /**
  6.  * JavaKeywords is a Utility class related to the reserved keywords
  7.  *
  8.  * @author James McManus
  9.  */
  10. public class JavaKeywords
  11. {
  12.     private static final HashSet<String> keywords = new HashSet<>(Arrays.asList(new String[]
  13.     {
  14.         //There are 50 keywords, and 3 literals; true, false and null.
  15.         "abstract",     "assert",        "boolean",      "break",           "byte",
  16.         "case",         "catch",         "char",         "class",           "const",
  17.         "continue",     "default",       "do",           "double",          "else",
  18.         "enum",         "extends",       "false",        "final",           "finally",
  19.         "float",        "for",           "goto",         "if",              "implements",
  20.         "import",       "instanceof",    "int",          "interface",       "long",
  21.         "native",       "new",           "null",         "package",         "private",
  22.         "protected",    "public",        "return",       "short",           "static",
  23.         "strictfp",     "super",         "switch",       "synchronized",    "this",
  24.         "throw",        "throws",        "transient",    "true",            "try",
  25.         "void",         "volatile",      "while"
  26.     }));
  27.    
  28.     public static boolean isKeyword(String toCheck)
  29.     {
  30.         return keywords.contains(toCheck);
  31.     }//End isKeyword()
  32.    
  33.     public static boolean containsKeyword(String toCheck)
  34.     {
  35.         toCheck = toCheck.toLowerCase();
  36.         for(String keyword : keywords)
  37.         {
  38.             if(toCheck.equals(keyword) || toCheck.endsWith("." + keyword) ||
  39.                toCheck.startsWith(keyword + ".") || toCheck.contains("." + keyword + "."))
  40.             {
  41.                 return true;
  42.             }//End if
  43.         }//End for
  44.         return false;
  45.     }//End containsKeyword()
  46.    
  47.     public static String[] getAsArray()
  48.     {
  49.         return keywords.toArray(new String[keywords.size()]);
  50.     }//End getAll()
  51.    
  52.     public static ArrayList<String> getAsArrayList()
  53.     {
  54.         return new ArrayList<>(keywords);
  55.     }//End getAsArrayList()
  56.    
  57.     public static HashSet<String> getAsHashSet()
  58.     {
  59.         return keywords;
  60.     }//End getAll()
  61. }//End JavaKeywords
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement