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.editor;
043:
044: /** Mostly used acceptors
045: *
046: * @author Miloslav Metelka
047: * @version 1.00
048: */
049:
050: public class AcceptorFactory {
051:
052: public static final Acceptor TRUE = new Fixed(true);
053:
054: public static final Acceptor FALSE = new Fixed(false);
055:
056: public static final Acceptor NL = new Char('\n');
057:
058: public static final Acceptor SPACE_NL = new TwoChar(' ', '\n');
059:
060: public static final Acceptor WHITESPACE = new Acceptor() {
061: public final boolean accept(char ch) {
062: return Character.isWhitespace(ch);
063: }
064:
065: public @Override
066: String toString() {
067: return "o.n.e.AcceptorFactory.WHITESPACE"; //NOI18N
068: }
069: };
070:
071: public static final Acceptor LETTER_DIGIT = new Acceptor() {
072: public final boolean accept(char ch) {
073: return Character.isLetterOrDigit(ch);
074: }
075:
076: public @Override
077: String toString() {
078: return "o.n.e.AcceptorFactory.LETTER_DIGIT"; //NOI18N
079: }
080: };
081:
082: public static final Acceptor JAVA_IDENTIFIER = new Acceptor() {
083: public final boolean accept(char ch) {
084: return Character.isJavaIdentifierPart(ch);
085: }
086:
087: public @Override
088: String toString() {
089: return "o.n.e.AcceptorFactory.JAVA_IDENTIFIER"; //NOI18N
090: }
091: };
092:
093: public static final Acceptor NON_JAVA_IDENTIFIER = new Acceptor() {
094: public final boolean accept(char ch) {
095: return !Character.isJavaIdentifierPart(ch);
096: }
097:
098: public @Override
099: String toString() {
100: return "o.n.e.AcceptorFactory.NON_JAVA_IDENTIFIER"; //NOI18N
101: }
102: };
103:
104: private static final class Fixed implements Acceptor {
105: private boolean state;
106:
107: public Fixed(boolean state) {
108: this .state = state;
109: }
110:
111: public final boolean accept(char ch) {
112: return state;
113: }
114:
115: public @Override
116: String toString() {
117: return "o.n.e.AcceptorFactory.Fixed@"
118: + Integer
119: .toHexString(System.identityHashCode(this ))
120: + " : state = " + state; //NOI18N
121: }
122: }
123:
124: private static final class Char implements Acceptor {
125: private char hit;
126:
127: public Char(char hit) {
128: this .hit = hit;
129: }
130:
131: public final boolean accept(char ch) {
132: return ch == hit;
133: }
134:
135: public @Override
136: String toString() {
137: return "o.n.e.AcceptorFactory.Char@"
138: + Integer
139: .toHexString(System.identityHashCode(this ))
140: + " : hit = " + hit; //NOI18N
141: }
142: }
143:
144: private static final class TwoChar implements Acceptor {
145: private char hit1, hit2;
146:
147: public TwoChar(char hit1, char hit2) {
148: this .hit1 = hit1;
149: this .hit2 = hit2;
150: }
151:
152: public final boolean accept(char ch) {
153: return ch == hit1 || ch == hit2;
154: }
155:
156: public @Override
157: String toString() {
158: return "o.n.e.AcceptorFactory.TwoChar@"
159: + Integer
160: .toHexString(System.identityHashCode(this ))
161: + " : hit1 = " + hit1 + ", hit2 = " + hit2; //NOI18N
162: }
163: }
164:
165: }
|