001: /* ====================================================================
002: The Jicarilla Software License
003:
004: Copyright (c) 2003 Leo Simons.
005: All rights reserved.
006:
007: Permission is hereby granted, free of charge, to any person obtaining
008: a copy of this software and associated documentation files (the
009: "Software"), to deal in the Software without restriction, including
010: without limitation the rights to use, copy, modify, merge, publish,
011: distribute, sublicense, and/or sell copies of the Software, and to
012: permit persons to whom the Software is furnished to do so, subject to
013: the following conditions:
014:
015: The above copyright notice and this permission notice shall be
016: included in all copies or substantial portions of the Software.
017:
018: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
019: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
020: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
021: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
022: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
023: TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
024: SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
025: ==================================================================== */
026: package org.jicarilla.lang;
027:
028: /**
029: * A {@link Selector} which matches the string representation of an object
030: * against the provided string.
031: *
032: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
033: * @version $Id: StringSelector.java,v 1.1 2004/03/23 13:37:58 lsimons Exp $
034: */
035: public class StringSelector implements CriterionExposingSelector {
036: /** The string to compare against. */
037: protected final String m_string;
038: /** Whether to ignore case in comparisons. */
039: protected final boolean m_ignoreCase;
040:
041: /**
042: * Create a new instance that matches against the specified string while
043: * not ignoring case.
044: *
045: * @param string the string to match against
046: */
047: public StringSelector(final String string) {
048: this (string, false);
049: }
050:
051: /**
052: * Create a new instance that matches against the specified string.
053: *
054: * @param string the string to match against
055: * @param ignoreCase whether to ignore case
056: */
057: public StringSelector(final String string, final boolean ignoreCase) {
058: Assert.assertNotNull("string argument may not be null", string);
059: m_ignoreCase = ignoreCase;
060:
061: if (m_ignoreCase)
062: m_string = string.toLowerCase();
063: else
064: m_string = string;
065: }
066:
067: /**
068: * Match the provided parameter against the string of this selector.
069: *
070: * @param object the object to test
071: * @return true if the object is selected, false otherwise
072: */
073: public boolean select(final Object object) {
074: if (object == null) {
075: return m_string.equals(object);
076: }
077:
078: if (m_ignoreCase) {
079: return m_string.equalsIgnoreCase(object.toString());
080: }
081:
082: return m_string.equals(object.toString());
083: }
084:
085: public Object getCriterion() {
086: return m_string;
087: }
088:
089: public boolean equals(Object o) {
090: if (!(o instanceof StringSelector))
091: return false;
092:
093: StringSelector es = (StringSelector) o;
094:
095: if (!(m_ignoreCase == es.m_ignoreCase))
096: return false;
097:
098: String c1 = (String) getCriterion();
099: String c2 = (String) es.getCriterion();
100:
101: /*if( c1 == null )
102: {
103: if( c2 != null )
104: return false;
105: else
106: return true;
107: }*/
108:
109: if (m_ignoreCase)
110: return c1.equalsIgnoreCase(c2);
111: else
112: return c1.equals(c2);
113: }
114:
115: public int hashCode() {
116: //if( getCriterion() == null )
117: //{
118: // if( m_ignoreCase )
119: // return 30;
120: // else
121: // return 40;
122: //}
123: //else
124: //{
125: int plus = (m_ignoreCase) ? 30 : 40;
126: return getCriterion().hashCode() + plus;
127: //}
128: }
129: }
|