org.testifyproject.apache.commons.lang.CharSet Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in org.testifyproject.testifyprojectpliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org.testifyproject/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.testifyproject.apache.org.testifyproject.testifyprojectmons.lang;
import java.org.testifyproject.testifyproject.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* A set of characters.
*
* Instances are immutable, but instances of subclasses may not be.
*
* #ThreadSafe#
* @author Apache Software Foundation
* @author Phil Steitz
* @author Pete Gieser
* @author Gary Gregory
* @since 1.0
* @version $Id: CharSet.java 1056988 2011-01-09 17:58:53Z niallp $
*/
public class CharSet implements Serializable {
/**
* Required for serialization support. Lang version 2.0.
*
* @see java.org.testifyproject.testifyproject.Serializable
*/
private static final long serialVersionUID = 5947847346149275958L;
/**
* A CharSet org.testifyproject.testifyprojectfining no characters.
* @since 2.0
*/
public static final CharSet EMPTY = new CharSet((String) null);
/**
* A CharSet org.testifyproject.testifyprojectfining ASCII alphabetic characters "a-zA-Z".
* @since 2.0
*/
public static final CharSet ASCII_ALPHA = new CharSet("a-zA-Z");
/**
* A CharSet org.testifyproject.testifyprojectfining ASCII alphabetic characters "a-z".
* @since 2.0
*/
public static final CharSet ASCII_ALPHA_LOWER = new CharSet("a-z");
/**
* A CharSet org.testifyproject.testifyprojectfining ASCII alphabetic characters "A-Z".
* @since 2.0
*/
public static final CharSet ASCII_ALPHA_UPPER = new CharSet("A-Z");
/**
* A CharSet org.testifyproject.testifyprojectfining ASCII alphabetic characters "0-9".
* @since 2.0
*/
public static final CharSet ASCII_NUMERIC = new CharSet("0-9");
/**
* A Map of the org.testifyproject.testifyprojectmon cases used in the factory.
* Subclasses can add more org.testifyproject.testifyprojectmon patterns if org.testifyproject.testifyprojectsired
* @since 2.0
*/
protected static final Map COMMON = Collections.synchronizedMap(new HashMap());
static {
COMMON.put(null, EMPTY);
COMMON.put("", EMPTY);
COMMON.put("a-zA-Z", ASCII_ALPHA);
COMMON.put("A-Za-z", ASCII_ALPHA);
COMMON.put("a-z", ASCII_ALPHA_LOWER);
COMMON.put("A-Z", ASCII_ALPHA_UPPER);
COMMON.put("0-9", ASCII_NUMERIC);
}
/** The set of CharRange objects. */
private final Set set = Collections.synchronizedSet(new HashSet());
//-----------------------------------------------------------------------
/**
* Factory method to create a new CharSet using a special syntax.
*
*
* null
or empty string ("")
* - set containing no characters
* - Single character, such as "a"
* - set containing just that character
* - Multi character, such as "a-e"
* - set containing characters from one character to the other
* - Negated, such as "^a" or "^a-e"
* - set containing all characters except those org.testifyproject.testifyprojectfined
* - Combinations, such as "abe-g"
* - set containing all the characters from the individual sets
*
*
* The matching order is:
*
* - Negated multi character range, such as "^a-e"
*
- Ordinary multi character range, such as "a-e"
*
- Negated single character, such as "^a"
*
- Ordinary single character, such as "a"
*
* Matching works left to right. Once a match is found the
* search starts again from the next character.
*
* If the same range is org.testifyproject.testifyprojectfined twice using the same syntax, only
* one range will be kept.
* Thus, "a-ca-c" creates only one range of "a-c".
*
* If the start and end of a range are in the wrong order,
* they are reversed. Thus "a-e" is the same as "e-a".
* As a result, "a-ee-a" would create only one range,
* as the "a-e" and "e-a" are the same.
*
* The set of characters represented is the union of the specified ranges.
*
* All CharSet objects returned by this method will be immutable.
*
* @param setStr the String org.testifyproject.testifyprojectscribing the set, may be null
* @return a CharSet instance
* @since 2.0
*/
public static CharSet getInstance(String setStr) {
Object set = COMMON.get(setStr);
if (set != null) {
return (CharSet) set;
}
return new CharSet(setStr);
}
/**
* Constructs a new CharSet using the set syntax.
* Each string is merged in with the set.
*
* @param setStrs Strings to merge into the initial set, may be null
* @return a CharSet instance
* @since 2.4
*/
public static CharSet getInstance(String[] setStrs) {
if (setStrs == null) {
return null;
}
return new CharSet(setStrs);
}
//-----------------------------------------------------------------------
/**
* Constructs a new CharSet using the set syntax.
*
* @param setStr the String org.testifyproject.testifyprojectscribing the set, may be null
* @since 2.0
*/
protected CharSet(String setStr) {
super();
add(setStr);
}
/**
* Constructs a new CharSet using the set syntax.
* Each string is merged in with the set.
*
* @param set Strings to merge into the initial set
* @throws NullPointerException if set is null
*/
protected CharSet(String[] set) {
super();
int sz = set.length;
for (int i = 0; i < sz; i++) {
add(set[i]);
}
}
//-----------------------------------------------------------------------
/**
* Add a set org.testifyproject.testifyprojectfinition string to the CharSet
.
*
* @param str set org.testifyproject.testifyprojectfinition string
*/
protected void add(String str) {
if (str == null) {
return;
}
int len = str.length();
int pos = 0;
while (pos < len) {
int remainder = (len - pos);
if (remainder >= 4 && str.charAt(pos) == '^' && str.charAt(pos + 2) == '-') {
// negated range
set.add(CharRange.isNotIn(str.charAt(pos + 1), str.charAt(pos + 3)));
pos += 4;
} else if (remainder >= 3 && str.charAt(pos + 1) == '-') {
// range
set.add(CharRange.isIn(str.charAt(pos), str.charAt(pos + 2)));
pos += 3;
} else if (remainder >= 2 && str.charAt(pos) == '^') {
// negated char
set.add(CharRange.isNot(str.charAt(pos + 1)));
pos += 2;
} else {
// char
set.add(CharRange.is(str.charAt(pos)));
pos += 1;
}
}
}
//-----------------------------------------------------------------------
/**
* Gets the internal set as an array of CharRange objects.
*
* @return an array of immutable CharRange objects
* @since 2.0
*/
public CharRange[] getCharRanges() {
return (CharRange[]) set.toArray(new CharRange[set.size()]);
}
//-----------------------------------------------------------------------
/**
* Does the CharSet
contain the specified
* character ch
.
*
* @param ch the character to check for
* @return true
if the set contains the characters
*/
public boolean contains(char ch) {
for (Iterator it = set.iterator(); it.hasNext();) {
CharRange range = (CharRange) it.next();
if (range.contains(ch)) {
return true;
}
}
return false;
}
// Basics
//-----------------------------------------------------------------------
/**
* Compares two CharSet objects, returning true if they represent
* exactly the same set of characters org.testifyproject.testifyprojectfined in the same way.
*
* The two sets abc
and a-c
are not
* equal according to this method.
*
* @param obj the object to org.testifyproject.testifyprojectpare to
* @return true if equal
* @since 2.0
*/
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof CharSet == false) {
return false;
}
CharSet other = (CharSet) obj;
return set.equals(other.set);
}
/**
* Gets a hashCode org.testifyproject.testifyprojectpatible with the equals method.
*
* @return a suitable hashCode
* @since 2.0
*/
public int hashCode() {
return 89 + set.hashCode();
}
/**
* Gets a string representation of the set.
*
* @return string representation of the set
*/
public String toString() {
return set.toString();
}
}