View Javadoc

1   /*
2    CollectionUtils.java
3    Creation date : 11/10/2013
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.collection;
26  
27  import static net.sourceforge.plantumldependency.common.constants.CharacterConstants.LEFT_BRACKET_CHAR;
28  import static net.sourceforge.plantumldependency.common.constants.CharacterConstants.RIGHT_BRACKET_CHAR;
29  
30  import java.util.ArrayList;
31  import java.util.Collection;
32  import java.util.Iterator;
33  
34  /**
35   * The class utilities simplifying collection operations.
36   *
37   * @author Benjamin Croizet (<a href="mailto:graffity2199@yahoo.fr>graffity2199@yahoo.fr</a>)
38   * @since 1.3.0
39   * @version 1.3.0
40   */
41  public abstract class CollectionUtils {
42  
43      /**
44       * Returns a string representation of this collection.
45       *
46       * @param collection
47       *            the collection to print, mustn't be <code>null</code>.
48       * @param separator
49       *            the separator to use between objects strings, can be <code>null</code>.
50       * @return a string representation of this collection. The string representation consists of a
51       *         list of the collection's elements in the order they are returned by its iterator,
52       *         enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements are separated by the
53       *         characters <tt>","</tt> (comma without space). Elements are converted to strings as
54       *         by {@link String#valueOf(Object)}.
55       * @since 1.3.0
56       */
57      public static String collectionToString(final Collection < ? > collection, final String separator) {
58          return collectionToString(collection, separator, LEFT_BRACKET_CHAR, RIGHT_BRACKET_CHAR);
59      }
60  
61      /**
62       * Returns a string representation of this collection.
63       *
64       * @param collection
65       *            the collection to print, mustn't be <code>null</code>.
66       * @param separator
67       *            the separator to use between objects strings, can be <code>null</code>.
68       * @param leftLimitString
69       *            the left limit {@link String}, can be <code>null</code>.
70       * @param rightLimitString
71       *            the right limit {@link String}, can be <code>null</code>.
72       * @return a string representation of this collection. The string representation consists of a
73       *         list of the collection's elements in the order they are returned by its iterator,
74       *         enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements are separated by the
75       *         characters <tt>","</tt> (comma without space). Elements are converted to strings as
76       *         by {@link String#valueOf(Object)}.
77       * @since 1.3.0
78       */
79      public static String collectionToString(final Collection < ? > collection, final String separator,
80              final String leftLimitString, final String rightLimitString) {
81          final Iterator < ? > it = collection.iterator();
82          if (!it.hasNext()) {
83              return leftLimitString + rightLimitString;
84          }
85  
86          final StringBuilder sb = new StringBuilder();
87          sb.append(leftLimitString);
88          for (;;) {
89              final Object e = it.next();
90              sb.append(e == collection ? "(this Collection)" : e);
91              if (!it.hasNext()) {
92                  return sb.append(rightLimitString).toString();
93              }
94              sb.append(separator);
95          }
96      }
97  
98      /**
99       * Gets the first element of a collection, if it exist.
100      *
101      * @param collection
102      *            the {@link Collection} where to get the first item from, mustn't be
103      *            <code>null</code>.
104      * @return the first element of the collection, if it exist, otherwise returns <code>null</code>
105      *         .
106      * @since 1.3.0
107      */
108     public static < T > T getCollectionFirstElement(final Collection < T > collection) {
109         T result = null;
110 
111         if (collection.isEmpty() == false) {
112             result = new ArrayList < T >(collection).get(0);
113         }
114 
115         return result;
116     }
117 
118     /**
119      * Private constructor to prevent from instantiation.
120      *
121      * @since 1.3.0
122      */
123     private CollectionUtils() {
124         super();
125     }
126 }