View Javadoc

1   /*
2    StringUtils.java
3    Creation date : 04/05/2010
4    Copyright © Benjamin Croizet (graffity2199@yahoo.fr)
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    or GNU Lesser General Public License as published by the
9    Free Software Foundation; either version 3 of the License,
10   or (at your option) any later version.
11  
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16  
17   You should have received copies of the GNU General Public License
18   and GNU Lesser General Public License along with this program;
19   if not, write to the Free Software Foundation, Inc.,
20   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21   http://www.fsf.org/licensing/licenses/gpl.html
22   http://www.gnu.org/licenses/lgpl.html
23   */
24  
25  package net.sourceforge.plantumldependency.common.utils.string;
26  
27  import java.util.Set;
28  import java.util.TreeSet;
29  
30  /**
31   * The class utilities simplifying some {@link String} tasks.
32   *
33   * @author Benjamin Croizet (<a href="mailto:graffity2199@yahoo.fr>graffity2199@yahoo.fr</a>)
34   * @since 1.3.0
35   * @version 1.3.0
36   */
37  public abstract class StringUtils {
38  
39      /**
40       * Convert a {@link Set} of object into a {@link Set} of their string representation.
41       *
42       * @param objects
43       *            the {@link Set} of object to get the strings from, mustn't be <code>null</code>.
44       * @return a {@link Set} containing all string representation of the objects.
45       * @since 1.3.0
46       */
47      public static Set < String > convertObjectSetIntoStringSet(final Set < Object > objects) {
48          final Set < String > objectsString = new TreeSet < String >();
49  
50          for (final Object object : objects) {
51              objectsString.add(object.toString());
52          }
53  
54          return objectsString;
55      }
56  
57      /**
58       * Checks if a String is empty ("") or null.
59       *
60       * @param str
61       *            the string to check.
62       * @return <code>true</code> if the {@link String} is empty, <code>false</code> otherwise.
63       * @since 1.3.0
64       */
65      public static boolean isEmpty(final String str) {
66          return str == null || str.trim().length() == 0;
67      }
68  
69      /**
70       * Checks if a String is not empty ("") and not null.
71       *
72       * @param str
73       *            the string to check.
74       * @return <code>true</code> if the {@link String} isn't empty, <code>false</code> otherwise.
75       * @since 1.3.0
76       */
77      public static boolean isNotEmpty(final String str) {
78          return str != null && str.trim().length() != 0;
79      }
80  
81      /**
82       * Replaces all strings of the set in the original string by a unique replacement string.
83       * <i>Note : this method is case sensitive</i>
84       *
85       * @param str
86       *            the original string where to replace strings, mustn't be <code>null</code>.
87       * @param setOfStringsToReplace
88       *            the {@link Set} of string to be replaced, mustn't be <code>null</code>.
89       * @param replacementString
90       *            the replacement string, mustn't be <code>null</code>.
91       * @return the new string after replacements.
92       * @since 1.3.0
93       */
94      public static String replaceASetOfString(final String str, final Set < String > setOfStringsToReplace,
95              final String replacementString) {
96          String result = str;
97  
98          for (final String stringToReplace : setOfStringsToReplace) {
99              result = result.replaceAll(stringToReplace, replacementString);
100         }
101 
102         return result;
103     }
104 
105     /**
106      * Private constructor to prevent from instantiation.
107      *
108      * @since 1.3.0
109      */
110     private StringUtils() {
111         super();
112     }
113 }