001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.editor.lib2;
043:
044: /** Mostly used acceptors
045: *
046: * @author Miloslav Metelka
047: * @version 1.00
048: */
049: public class AcceptorFactory {
050:
051: public static final Acceptor TRUE = new Fixed(true);
052:
053: public static final Acceptor FALSE = new Fixed(false);
054:
055: public static final Acceptor NL = new Char('\n');
056:
057: public static final Acceptor SPACE_NL = new TwoChar(' ', '\n');
058:
059: public static final Acceptor WHITESPACE = new Acceptor() {
060: public final boolean accept(char ch) {
061: return Character.isWhitespace(ch);
062: }
063:
064: public @Override
065: String toString() {
066: return "o.n.m.e.lib2.AcceptorFactory.WHITESPACE"; //NOI18N
067: }
068: };
069:
070: public static final Acceptor LETTER_DIGIT = new Acceptor() {
071: public final boolean accept(char ch) {
072: return Character.isLetterOrDigit(ch);
073: }
074:
075: public @Override
076: String toString() {
077: return "o.n.m.e.lib2.AcceptorFactory.LETTER_DIGIT"; //NOI18N
078: }
079: };
080:
081: public static final Acceptor JAVA_IDENTIFIER = new Acceptor() {
082: public final boolean accept(char ch) {
083: return Character.isJavaIdentifierPart(ch);
084: }
085:
086: public @Override
087: String toString() {
088: return "o.n.m.e.lib2.AcceptorFactory.JAVA_IDENTIFIER"; //NOI18N
089: }
090: };
091:
092: public static final Acceptor NON_JAVA_IDENTIFIER = new Acceptor() {
093: public final boolean accept(char ch) {
094: return !Character.isJavaIdentifierPart(ch);
095: }
096:
097: public @Override
098: String toString() {
099: return "o.n.m.e.lib2.AcceptorFactory.NON_JAVA_IDENTIFIER"; //NOI18N
100: }
101: };
102:
103: private static final class Fixed implements Acceptor {
104: private boolean state;
105:
106: public Fixed(boolean state) {
107: this .state = state;
108: }
109:
110: public final boolean accept(char ch) {
111: return state;
112: }
113:
114: public @Override
115: String toString() {
116: return "o.n.m.e.lib2.AcceptorFactory.Fixed@"
117: + Integer
118: .toHexString(System.identityHashCode(this ))
119: + " : state = " + state; //NOI18N
120: }
121: }
122:
123: private static final class Char implements Acceptor {
124: private char hit;
125:
126: public Char(char hit) {
127: this .hit = hit;
128: }
129:
130: public final boolean accept(char ch) {
131: return ch == hit;
132: }
133:
134: public @Override
135: String toString() {
136: return "o.n.m.e.lib2.AcceptorFactory.Char@"
137: + Integer
138: .toHexString(System.identityHashCode(this ))
139: + " : hit = " + hit; //NOI18N
140: }
141: }
142:
143: private static final class TwoChar implements Acceptor {
144: private char hit1, hit2;
145:
146: public TwoChar(char hit1, char hit2) {
147: this .hit1 = hit1;
148: this .hit2 = hit2;
149: }
150:
151: public final boolean accept(char ch) {
152: return ch == hit1 || ch == hit2;
153: }
154:
155: public @Override
156: String toString() {
157: return "o.n.m.e.lib2.AcceptorFactory.TwoChar@"
158: + Integer
159: .toHexString(System.identityHashCode(this ))
160: + " : hit1 = " + hit1 + ", hit2 = " + hit2; //NOI18N
161: }
162: }
163: }
|