KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgrapht > util > ArrayUtil


1 /* ==========================================
2  * JGraphT : a free Java graph-theory library
3  * ==========================================
4  *
5  * Project Info: http://jgrapht.sourceforge.net/
6  * Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
7  *
8  * (C) Copyright 2003-2006, by Barak Naveh and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18  * License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this library; if not, write to the Free Software Foundation,
22  * Inc.,
23  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
24  */

25 /* -----------------
26  * ArrayUtil.java
27  * -----------------
28  * (C) Copyright 2005-2006, by Assaf Lehr and Contributors.
29  *
30  * $Id: ArrayUtil.java 504 2006-07-03 02:37:26Z perfecthash $
31  *
32  * Original Author: Assaf Lehr
33  * Contributor(s): -
34  *
35  * Changes
36  * -------
37  */

38 package org.jgrapht.util;
39
40 /**
41  * Utility class to iterate over arrays and use toString() on their elements. In
42  * jdk1.5 , a few very useful methods were added to the system.Arrays class.
43  * JGraphT should work on jdk1.4, so we re-implement them here, as utility
44  * methods.
45  *
46  * @author Assaf_Lehr
47  */

48 public class ArrayUtil
49 {
50
51     //~ Methods ---------------------------------------------------------------
52

53     /**
54      * Prints the contents of an array (not the array ref itself).
55      *
56      * @param array
57      */

58     public static String JavaDoc toString(int [] array)
59     {
60         StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc("[");
61         stringBuffer.append(array[0]);
62         for (int i = 1; i < array.length; i++) {
63             stringBuffer.append(",").append(array[i]);
64         }
65         stringBuffer.append("]");
66         return stringBuffer.toString();
67     }
68
69     /**
70      * Print the contents of an array (not the array ref itself).
71      *
72      * @param array
73      */

74     public static String JavaDoc toString(Object JavaDoc [] array, ToStringFunctor functor)
75     {
76         StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc("[");
77         stringBuffer.append(array[0]);
78         for (int i = 0; i < array.length; i++) {
79             stringBuffer.append(",").append(functor.toString(array[i]));
80         }
81         stringBuffer.append("]");
82         return stringBuffer.toString();
83     }
84
85     /**
86      * Prints the contents of an array (not the array ref itself).
87      *
88      * @param array
89      */

90     public static String JavaDoc toString(Object JavaDoc [] array)
91     {
92         StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc("[");
93         stringBuffer.append(array[0]);
94         for (int i = 1; i < array.length; i++) {
95             stringBuffer.append(",").append(array[i]);
96         }
97         stringBuffer.append("]");
98         return stringBuffer.toString();
99     }
100
101     //~ Inner Interfaces ------------------------------------------------------
102

103     public interface ToStringFunctor
104     {
105         public String JavaDoc toString(Object JavaDoc arrayElement);
106     }
107 }
108
Popular Tags