一、前言
通过jfinal3.0的源码com.jfinal.kit.JavaKeyword关键词定义类通过Collections.unmodifiableSet将常用java关键词注入java.util.Set类集中不让修改,只允增加和删除等操作,详情参见源码说明
二、源码说明
package com.jfinal.kit;@b@@b@import java.util.Collections;@b@import java.util.HashSet;@b@import java.util.Set;@b@import com.jfinal.kit.StrKit;@b@@b@/**@b@ * JavaKeyword.@b@ */@b@public class JavaKeyword {@b@ @b@ private String[] keywordArray = {@b@ "abstract",@b@ "assert",@b@ "boolean",@b@ "break",@b@ "byte",@b@ "case",@b@ "catch",@b@ "char",@b@ "class",@b@ "const",@b@ "continue",@b@ "default",@b@ "do",@b@ "double",@b@ "else",@b@ "enum",@b@ "extends",@b@ "final",@b@ "finally",@b@ "float",@b@ "for",@b@ "goto",@b@ "if",@b@ "implements",@b@ "import",@b@ "instanceof",@b@ "int",@b@ "interface",@b@ "long",@b@ "native",@b@ "new",@b@ "package",@b@ "private",@b@ "protected",@b@ "public",@b@ "return",@b@ "strictfp",@b@ "short",@b@ "static",@b@ "super",@b@ "switch",@b@ "synchronized",@b@ "this",@b@ "throw",@b@ "throws",@b@ "transient",@b@ "try",@b@ "void",@b@ "volatile",@b@ "while"@b@ };@b@ @b@ private Set<String> set;@b@ @b@ public static final JavaKeyword me = createSharedInstance();@b@ @b@ private static JavaKeyword createSharedInstance() {@b@ JavaKeyword jk = new JavaKeyword();@b@ jk.set = Collections.unmodifiableSet(jk.set); // 共享对象不让修改@b@ return jk;@b@ }@b@ @b@ public JavaKeyword() {@b@ set = new HashSet<String>();@b@ for (String keyword : keywordArray) {@b@ set.add(keyword);@b@ }@b@ }@b@ @b@ public JavaKeyword addKeyword(String keyword) {@b@ if (StrKit.notBlank(keyword)) {@b@ set.add(keyword);@b@ }@b@ return this;@b@ }@b@ @b@ public JavaKeyword removeKeyword(String keyword) {@b@ set.remove(keyword);@b@ return this;@b@ }@b@ @b@ public boolean contains(String str) {@b@ return set.contains(str);@b@ }@b@}