001    /*
002     * FirstWordFormatter.java
003     * 
004     * Copyright (C) 2005 Anupam Sengupta (anupamsg@users.sourceforge.net) 
005     * 
006     * This program is free software; you can redistribute it and/or 
007     * modify it under the terms of the GNU General Public License 
008     * as published by the Free Software Foundation; either version 2 
009     * of the License, or (at your option) any later version. 
010     * 
011     * This program is distributed in the hope that it will be useful, 
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of 
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
014     * GNU General Public License for more details. 
015     * 
016     * You should have received a copy of the GNU General Public License
017     * along with this program; if not, write to the Free Software 
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 
019     *
020     * Version: $Revision: 1.3 $
021     */
022    package net.sf.anupam.csv.formatters;
023    
024    import org.apache.commons.lang.StringUtils;
025    
026    /**
027     * A {@link CSVFieldFormatter formatter} that returns the first word
028     * of the specified CSV value. This is useful in situations such as
029     * extracting the first name.
030     *
031     * @author Anupam Sengupta
032     * @version $Revision: 1.3 $
033     * @csv.formatter-mapping name="firstWord"
034     * @see LastWordFormatter
035     * @since 1.5
036     */
037    final class FirstWordFormatter
038            implements CSVFieldFormatter {
039    
040        /**
041         * Constructor for FirstWordFormatter.
042         */
043        public FirstWordFormatter() {
044            super();
045        }
046    
047        /**
048         * Formats the value and returns the first word.
049         *
050         * @param value the value to be transformed
051         * @return the first word from the input value
052         * @see CSVFieldFormatter#format(String)
053         */
054        public String format(final String value) {
055    
056            if (value == null) {
057                return null;
058            }
059            return StringUtils.trim(value.split(" ")[0]);
060        }
061    
062    }