001 // Copyright 2004, 2005 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry.util.text;
016
017 /**
018 * An object for matching a set of provided ASCII characters.
019 *
020 * @author mb
021 * @since 4.0
022 */
023 public class AsciiCharacterMatcher implements ICharacterMatcher
024 {
025 private boolean[] _charMap;
026
027 /**
028 * Create a new ASCII character matcher for identifying the set of provided ASCII characters.
029 *
030 * @param chars the character that this matcher should identify
031 */
032 public AsciiCharacterMatcher(String chars) {
033 _charMap = new boolean[128];
034 for (int i = 0; i < chars.length(); i++) {
035 char ch = chars.charAt(i);
036 if (ch > 127)
037 continue;
038 _charMap[ch] = true;
039 }
040 }
041
042 /**
043 * Match the characters provided in the constructor.
044 *
045 * @see org.apache.tapestry.util.text.ICharacterMatcher#matches(char)
046 */
047 public boolean matches(char ch)
048 {
049 if (ch > 127)
050 return false;
051 return _charMap[ch];
052 }
053 }