KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > anupam > csv > formatters > LastWordFormatter


1 /*
2  * FirstWordFormatter.java
3  *
4  * Copyright (C) 2005 Anupam Sengupta (anupamsg@users.sourceforge.net)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * Version: $Revision: 1.3 $
21  */

22 package net.sf.anupam.csv.formatters;
23
24 import org.apache.commons.lang.StringUtils;
25
26 /**
27  * A {@link CSVFieldFormatter formatter} that returns the last word
28  * of the specified CSV value. This is useful in situations such as
29  * extracting the last name.
30  *
31  * @author Anupam Sengupta
32  * @version $Revision: 1.3 $
33  * @csv.formatter-mapping name="lastWord"
34  * @see FirstWordFormatter
35  * @since 1.5
36  */

37 final class LastWordFormatter
38         implements CSVFieldFormatter {
39
40     /**
41      * Constructor for FirstWordFormatter.
42      */

43     public LastWordFormatter() {
44         super();
45     }
46
47     /**
48      * Formats the value and returns the last word.
49      *
50      * @param value the value to be transformed
51      * @return the last word from the input value
52      * @see CSVFieldFormatter#format(String)
53      */

54     public String JavaDoc format(final String JavaDoc value) {
55
56         if (value == null) {
57             return null;
58         }
59         final String JavaDoc [] splitValues = value.split(" ");
60
61         return StringUtils.trim(splitValues[splitValues.length - 1]);
62     }
63
64 }
65
Popular Tags