001: // ============================================================================
002: // $Id: StringFunctors.java,v 1.2 2006/11/30 05:05:23 davidahall Exp $
003: // Copyright (c) 2006 David A. Hall
004: // ============================================================================
005: // The contents of this file are subject to the Common Development and
006: // Distribution License (CDDL), Version 1.0 (the License); you may not use this
007: // file except in compliance with the License. You should have received a copy
008: // of the the License along with this file: if not, a copy of the License is
009: // available from Sun Microsystems, Inc.
010: //
011: // http://www.sun.com/cddl/cddl.html
012: //
013: // From time to time, the license steward (initially Sun Microsystems, Inc.) may
014: // publish revised and/or new versions of the License. You may not use,
015: // distribute, or otherwise make this file available under subsequent versions
016: // of the License.
017: //
018: // Alternatively, the contents of this file may be used under the terms of the
019: // GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
020: // case the provisions of the LGPL are applicable instead of those above. If you
021: // wish to allow use of your version of this file only under the terms of the
022: // LGPL, and not to allow others to use your version of this file under the
023: // terms of the CDDL, indicate your decision by deleting the provisions above
024: // and replace them with the notice and other provisions required by the LGPL.
025: // If you do not delete the provisions above, a recipient may use your version
026: // of this file under the terms of either the CDDL or the LGPL.
027: //
028: // This library is distributed in the hope that it will be useful,
029: // but WITHOUT ANY WARRANTY; without even the implied warranty of
030: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
031: // ============================================================================
032:
033: package net.sf.jga.fn.string;
034:
035: import java.text.DateFormat;
036: import java.text.Format;
037: import java.text.MessageFormat;
038: import java.text.NumberFormat;
039: import java.util.Date;
040: import java.util.regex.Pattern;
041: import net.sf.jga.fn.UnaryFunctor;
042:
043: /**
044: * Static factory methods for the functors in the String package.
045: * <p>
046: * Copyright © 2006 David A. Hall
047: * @author <a href="mailto:davidahall@users.sf.net">David A. Hall</a>
048: */
049:
050: public final class StringFunctors {
051: /**
052: * Returns a functor that presents its argument in String form using the argument's
053: * toString() method.
054: */
055: static public <T> UnaryFunctor<T, String> defaultFormat() {
056: return new DefaultFormat<T>();
057: }
058:
059: /**
060: * Returns a functor that presents its argument in String form using the given format
061: */
062: static public UnaryFunctor<Object[], String> formatValue(
063: MessageFormat format) {
064: return new FormatValue<Object[]>(format);
065: }
066:
067: /**
068: * Returns a functor that presents its argument in String form using the given format
069: */
070: static public <T extends Date> UnaryFunctor<T, String> formatValue(
071: DateFormat format) {
072: return new FormatValue<T>(format);
073: }
074:
075: /**
076: * Returns a functor that presents its argument in String form using the given format
077: */
078: static public <T extends Number> UnaryFunctor<T, String> formatValue(
079: NumberFormat format) {
080: return new FormatValue<T>(format);
081: }
082:
083: /**
084: * Returns a functor that matches its String argument against the given regex
085: */
086: static public UnaryFunctor<String, Boolean> match(String regex) {
087: return new Match(regex);
088: }
089:
090: /**
091: * Returns a functor that matches its String argument against the given pattern
092: */
093: static public UnaryFunctor<String, Boolean> match(Pattern pattern) {
094: return new Match(pattern);
095: }
096:
097: /**
098: * Returns a functor that parses its argument using the given format and returns an object
099: * of the given class.
100: */
101: static public <R> UnaryFunctor<String, R> parseFormat(
102: Format format, UnaryFunctor<Object, R> converter) {
103: return new ParseFormat<R>(format, converter);
104: }
105:
106: /**
107: * Returns a functor that parses its argument using the given format and returns an object
108: * of the given class.
109: */
110: static public <R extends Date> UnaryFunctor<String, R> parseFormat(
111: Class<R> dateClass, DateFormat format) {
112: return new ParseFormat.Date<R>(dateClass, format);
113: }
114:
115: /**
116: * Returns a functor that parses its argument using the given format and returns an object
117: * of the given class.
118: */
119: static public <R extends Number> UnaryFunctor<String, R> parseFormat(
120: Class<R> numClass, NumberFormat format) {
121: return new ParseFormat.Number<R>(numClass, format);
122: }
123: }
|