KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > lib2 > AcceptorFactory


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.editor.lib2;
21
22 /** Mostly used acceptors
23 *
24 * @author Miloslav Metelka
25 * @version 1.00
26 */

27 public class AcceptorFactory {
28
29     public static final Acceptor TRUE = new Fixed(true);
30
31     public static final Acceptor FALSE = new Fixed(false);
32
33     public static final Acceptor NL = new Char('\n');
34
35     public static final Acceptor SPACE_NL = new TwoChar(' ', '\n');
36
37     public static final Acceptor WHITESPACE
38     = new Acceptor() {
39           public final boolean accept(char ch) {
40               return Character.isWhitespace(ch);
41           }
42       };
43
44     public static final Acceptor LETTER_DIGIT
45     = new Acceptor() {
46           public final boolean accept(char ch) {
47               return Character.isLetterOrDigit(ch);
48           }
49       };
50
51     public static final Acceptor JAVA_IDENTIFIER
52     = new Acceptor() {
53           public final boolean accept(char ch) {
54               return Character.isJavaIdentifierPart(ch);
55           }
56       };
57
58     public static final Acceptor NON_JAVA_IDENTIFIER
59     = new Acceptor() {
60           public final boolean accept(char ch) {
61               return !Character.isJavaIdentifierPart(ch);
62           }
63       };
64
65     private static final class Fixed implements Acceptor {
66     private boolean state;
67     
68     public Fixed(boolean state) {
69         this.state = state;
70     }
71         
72     public final boolean accept(char ch) {
73               return state;
74     }
75     }
76
77     private static final class Char implements Acceptor {
78     private char hit;
79     
80     public Char(char hit) {
81         this.hit = hit;
82     }
83         
84     public final boolean accept(char ch) {
85               return ch == hit;
86     }
87     }
88
89     private static final class TwoChar implements Acceptor {
90     private char hit1, hit2;
91     
92     public TwoChar(char hit1, char hit2) {
93         this.hit1 = hit1;
94         this.hit2 = hit2;
95     }
96         
97     public final boolean accept(char ch) {
98               return ch == hit1 || ch == hit2;
99     }
100     }
101 }
102
Popular Tags