/*
* Created on Jan 13, 2008
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/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.
*
* Copyright @2008-2009 the original author or authors.
*/
package org.fest.swing.util;
import static org.fest.util.Objects.areEqual;
import static org.fest.util.Strings.isEmpty;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
/**
* Understands utility methods related to Strings.
*
* @author Alex Ruiz
*/
public final class Strings {
/**
* Returns whether the given String is the default toString() implementation of an
* Object.
* @param s the given String.
* @return true if the given String is the default toString() implementation,
* false otherwise.
*/
public static boolean isDefaultToString(String s) {
if (isEmpty(s)) return false;
int at = s.indexOf("@");
if (at == -1) return false;
String hash = s.substring(at + 1, s.length());
try {
Integer.parseInt(hash, 16);
return true;
} catch (NumberFormatException e) {
return false;
}
}
/**
* Indicates if the given Strings match. To match, one of the following conditions needs to be true:
*
Strings have to be equals matches the regular expression in patternString to match (it can be a regular expression.)
* @param s the String to verify.
* @return true if the given Strings match, false otherwise.
*/
public static boolean areEqualOrMatch(String pattern, String s) {
if (areEqual(pattern, s)) return true;
if (pattern != null && s != null) {
try {
return s.matches(pattern);
} catch(PatternSyntaxException noValidRegex) {
return s.contains(pattern);
}
}
return false;
}
/**
* Indicates if the given String matches the given regular expression pattern.
* @param p the given regular expression pattern.
* @param s the String to evaluate.
* @return true if the given String matches the given regular expression pattern,
* false otherwise. It also returns false if the given String is
* null.
* @throws NullPointerException if the given regular expression pattern is null.
*/
public static boolean match(Pattern p, String s) {
return match(p, (CharSequence)s);
}
/**
* Indicates if the given CharSequence matches the given regular expression pattern.
* @param p the given regular expression pattern.
* @param s the CharSequence to evaluate.
* @return true if the given CharSequence matches the given regular expression pattern,
* false otherwise. It also returns false if the given CharSequence is
* null.
* @throws NullPointerException if the given regular expression pattern is null.
*/
public static boolean match(Pattern p, CharSequence s) {
if (p == null) throw new NullPointerException("The pattern to match should not be null");
if (s == null) return false;
return p.matcher(s).matches();
}
private Strings() {}
}