001 /* 002 * Created on Jan 13, 2008 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. 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 distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 * 014 * Copyright @2008-2010 the original author or authors. 015 */ 016 package org.fest.swing.util; 017 018 import static org.fest.util.Objects.areEqual; 019 import static org.fest.util.Strings.isEmpty; 020 021 import java.util.regex.Pattern; 022 import java.util.regex.PatternSyntaxException; 023 024 /** 025 * Understands utility methods related to <code>String</code>s. 026 * 027 * @author Alex Ruiz 028 * @author Uli Schrempp 029 */ 030 public final class Strings { 031 032 /** 033 * Returns whether the given <code>String</code> is the default <code>toString()</code> implementation of an 034 * <code>Object</code>. 035 * @param s the given <code>String</code>. 036 * @return <code>true</code> if the given <code>String</code> is the default <code>toString()</code> implementation, 037 * <code>false</code> otherwise. 038 */ 039 public static boolean isDefaultToString(String s) { 040 if (isEmpty(s)) return false; 041 int at = s.indexOf("@"); 042 if (at == -1) return false; 043 String hash = s.substring(at + 1, s.length()); 044 try { 045 Integer.parseInt(hash, 16); 046 return true; 047 } catch (NumberFormatException e) { 048 return false; 049 } 050 } 051 052 /** 053 * Indicates if the given <code>String</code>s match. To match, one of the following conditions needs to be true: 054 * <ul> 055 * <li>both <code>String</code>s have to be equal</li> 056 * <li><code>s</code> matches the regular expression in <code>pattern</code></li> 057 * </ul> 058 * @param pattern a <code>String</code> to match (it can be a regular expression.) 059 * @param s the <code>String</code> to verify. 060 * @return <code>true</code> if the given <code>String</code>s match, <code>false</code> otherwise. 061 */ 062 public static boolean areEqualOrMatch(String pattern, String s) { 063 if (areEqual(pattern, s)) return true; 064 if (pattern != null && s != null) { 065 try { 066 return s.matches(pattern); 067 } catch (PatternSyntaxException invalidRegex) { 068 return s.contains(pattern); 069 } 070 } 071 return false; 072 } 073 074 /** 075 * Indicates if the given <code>String</code> matches the given regular expression pattern. 076 * @param p the given regular expression pattern. 077 * @param s the <code>String</code> to evaluate. 078 * @return <code>true</code> if the given <code>String</code> matches the given regular expression pattern, 079 * <code>false</code> otherwise. It also returns <code>false</code> if the given <code>String</code> is 080 * <code>null</code>. 081 * @throws NullPointerException if the given regular expression pattern is <code>null</code>. 082 */ 083 public static boolean match(Pattern p, String s) { 084 return match(p, (CharSequence)s); 085 } 086 087 /** 088 * Indicates if the given <code>CharSequence</code> matches the given regular expression pattern. 089 * @param p the given regular expression pattern. 090 * @param s the <code>CharSequence</code> to evaluate. 091 * @return <code>true</code> if the given <code>CharSequence</code> matches the given regular expression pattern, 092 * <code>false</code> otherwise. It also returns <code>false</code> if the given <code>CharSequence</code> is 093 * <code>null</code>. 094 * @throws NullPointerException if the given regular expression pattern is <code>null</code>. 095 */ 096 public static boolean match(Pattern p, CharSequence s) { 097 if (p == null) throw new NullPointerException("The pattern to match should not be null"); 098 if (s == null) return false; 099 return p.matcher(s).matches(); 100 } 101 102 private Strings() {} 103 }