View Javadoc

1   /**********************************************************************
2    * ContentStringBuilder.java
3    * created on 18.07.2006 by netseeker
4    * $Source$
5    * $Date$
6    * $Revision$
7    *
8    * ====================================================================
9    *
10   *  Copyright 2006 netseeker aka Michael Manske
11   *
12   *  Licensed under the Apache License, Version 2.0 (the "License");
13   *  you may not use this file except in compliance with the License.
14   *  You may obtain a copy of the License at
15   *
16   *      http://www.apache.org/licenses/LICENSE-2.0
17   *
18   *  Unless required by applicable law or agreed to in writing, software
19   *  distributed under the License is distributed on an "AS IS" BASIS,
20   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21   *  See the License for the specific language governing permissions and
22   *  limitations under the License.
23   * ====================================================================
24   *
25   * This file is part of the EJOE framework.
26   * For more information on the author, please see
27   * <http://www.manskes.de/>.
28   *
29   *********************************************************************/
30  
31  package de.netseeker.ejoe.util;
32  
33  import java.util.Collection;
34  import java.util.Iterator;
35  
36  /***
37   * @author netseeker
38   * @since 0.3.9.1
39   */
40  public class ContentStringBuilder
41  {
42  
43      /***
44       * JDK 1.4 compatible toString for arrays
45       * 
46       * @param array the array to build a string representation from
47       * @return a string representation for the contents of the given array
48       */
49      public static String toString( Object[] array )
50      {
51          if ( array == null ) return "null";
52          if ( array.length == 0 ) return "[]";
53  
54          StringBuffer buf = new StringBuffer();
55  
56          for ( int i = 0; i < array.length; i++ )
57          {
58              if ( i == 0 )
59                  buf.append( '[' );
60              else
61                  buf.append( ", " );
62  
63              buf.append( String.valueOf( array[i] ) );
64          }
65  
66          buf.append( ']' );
67  
68          return buf.toString();
69      }
70  
71      /***
72       * JDK 1.4 compatible toString for collections
73       * 
74       * @param collection the collection to build a string representation from
75       * @return a string representation for the contents of the given collection
76       */
77      public static String toString( Collection collection )
78      {
79          if ( collection == null ) return "null";
80          if ( collection.size() == 0 ) return "[]";
81  
82          StringBuffer buf = new StringBuffer();
83          boolean first = true;
84  
85          for ( Iterator it = collection.iterator(); it.hasNext(); )
86          {
87              if ( first )
88              {
89                  buf.append( '[' );
90                  first = false;
91              }
92              else
93                  buf.append( ", " );
94  
95              buf.append( String.valueOf( it.next() ) );
96          }
97  
98          buf.append( ']' );
99  
100         return buf.toString();
101     }
102 }