Source code: joelib/util/ArrayHelper.java
1 ///////////////////////////////////////////////////////////////////////////////
2 // Filename: $RCSfile: ArrayHelper.java,v $
3 // Purpose: Atom representation.
4 // Language: Java
5 // Compiler: JDK 1.4
6 // Authors: Joerg K. Wegner
7 // Version: $Revision: 1.18 $
8 // $Date: 2003/08/22 15:56:21 $
9 // $Author: wegner $
10 //
11 // Copyright (c) Dept. Computer Architecture, University of Tuebingen, Germany
12 //
13 // This program is free software; you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation version 2 of the License.
16 //
17 // This program is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
21 ///////////////////////////////////////////////////////////////////////////////
22 package joelib.util;
23
24 import wsi.ra.text.DecimalFormatter;
25
26 /*==========================================================================*
27 * IMPORTS
28 *========================================================================== */
29 import java.util.StringTokenizer;
30 import java.util.Vector;
31
32 import org.apache.log4j.Category;
33
34
35 /*==========================================================================*
36 * CLASS DECLARATION
37 *========================================================================== */
38
39 /**
40 * Array helper methods for writing and loading arrays.
41 *
42 * @author wegnerj
43 * @license GPL
44 * @cvsversion $Revision: 1.18 $, $Date: 2003/08/22 15:56:21 $
45 */
46 public class ArrayHelper
47 {
48 //~ Static fields/initializers /////////////////////////////////////////////
49
50 /*-------------------------------------------------------------------------*
51 * public static member variables
52 *------------------------------------------------------------------------- */
53
54 /**
55 * Description of the Field
56 */
57 public static String DEFAULT_SEPARATOR = new String(",");
58
59 /*-------------------------------------------------------------------------*
60 * private static member variables
61 *------------------------------------------------------------------------- */
62
63 // Obtain a suitable logger.
64 private static Category logger = Category.getInstance(
65 "joelib.util.ArrayHelper");
66 private static ArrayHelper arrayHelper;
67
68 //~ Instance fields ////////////////////////////////////////////////////////
69
70 /*-------------------------------------------------------------------------*
71 * private member variables
72 *------------------------------------------------------------------------- */
73 private String separator;
74
75 //~ Constructors ///////////////////////////////////////////////////////////
76
77 /*-------------------------------------------------------------------------*
78 * constructor
79 *------------------------------------------------------------------------- */
80
81 /**
82 * Constructor for the JOERandom object
83 */
84 private ArrayHelper()
85 {
86 separator = new String(DEFAULT_SEPARATOR);
87 }
88
89 //~ Methods ////////////////////////////////////////////////////////////////
90
91 /*-------------------------------------------------------------------------*
92 * public methods
93 *------------------------------------------------------------------------- */
94
95 /**
96 * Description of the Method
97 *
98 * @return Description of the Return Value
99 */
100 public static synchronized ArrayHelper instance()
101 {
102 if (arrayHelper == null)
103 {
104 arrayHelper = new ArrayHelper();
105 }
106
107 return arrayHelper;
108 }
109
110 /**
111 * Sets the separator attribute of the ArrayHelper object
112 *
113 * @param _separator The new separator value
114 */
115 public void setSeparator(String _separator)
116 {
117 separator = _separator;
118 }
119
120 /**
121 * Gets the separator attribute of the ArrayHelper object
122 *
123 * @return The separator value
124 */
125 public String getSeparator()
126 {
127 return separator;
128 }
129
130 public boolean[] booleanArrayFromTrueFalseString(String sArrays)
131 {
132 return booleanArrayFromTrueFalseString(sArrays, separator);
133 }
134
135 /**
136 * Loads boolean array from <tt>String</tt> . Format i_1,i_2,...,i_n. ','
137 * is here the default separator. It's slower than using
138 * <code>booleanArrayFromString</code>.
139 *
140 * @param separator Description of the Parameter
141 * @param sArrays Description of the Parameter
142 * @param size Description of the Parameter
143 * @return Description of the Return Value
144 */
145 public static boolean[] booleanArrayFromTrueFalseString(String sArrays,
146 String separator)
147 {
148 StringTokenizer st = new StringTokenizer(sArrays, separator);
149
150 // start with a vector of size 50
151 Vector tmpVector = new Vector(100);
152
153 // get integer values from String
154 String tmpString;
155
156 while (st.hasMoreTokens())
157 {
158 tmpString = st.nextToken();
159
160 if (tmpString.equals("true"))
161 {
162 tmpVector.add(Boolean.TRUE);
163 }
164 else if (tmpString.equals("false"))
165 {
166 tmpVector.add(Boolean.FALSE);
167 }
168 else
169 {
170 logger.error(tmpString +
171 " is not a valid token in a bit string.");
172
173 return null;
174 }
175 }
176
177 // copy values to integer array
178 int size = tmpVector.size();
179 boolean[] array = new boolean[size];
180
181 // System.out.print("bits:");
182 for (int i = 0; i < size; i++)
183 {
184 array[i] = ((Boolean) tmpVector.get(i)).booleanValue();
185
186 // System.out.print(""+(array[i]==true?'1':'0'));
187 }
188
189 // System.out.println("");
190 return array;
191 }
192
193 public boolean[] booleanArrayFromSimpleString(String sArrays)
194 {
195 return booleanArrayFromSimpleString(sArrays, separator);
196 }
197
198 /**
199 * Loads boolean array from <tt>String</tt> . Format i_1,i_2,...,i_n. ','
200 * is here the default separator. It's slower than using
201 * <code>booleanArrayFromString</code>.
202 *
203 * @param separator Description of the Parameter
204 * @param sArrays Description of the Parameter
205 * @param size Description of the Parameter
206 * @return Description of the Return Value
207 */
208 public static boolean[] booleanArrayFromSimpleString(String sArrays,
209 String separator)
210 {
211 StringTokenizer st = new StringTokenizer(sArrays, separator);
212
213 // start with a vector of size 50
214 Vector tmpVector = new Vector(100);
215
216 // get integer values from String
217 String tmpString;
218
219 while (st.hasMoreTokens())
220 {
221 tmpString = st.nextToken();
222
223 if (tmpString.equals("1"))
224 {
225 tmpVector.add(Boolean.TRUE);
226 }
227 else if (tmpString.equals("0"))
228 {
229 tmpVector.add(Boolean.FALSE);
230 }
231 else
232 {
233 logger.error(tmpString +
234 " is not a valid token in a bit string.");
235
236 return null;
237 }
238 }
239
240 // copy values to integer array
241 int size = tmpVector.size();
242 boolean[] array = new boolean[size];
243
244 // System.out.print("bits:");
245 for (int i = 0; i < size; i++)
246 {
247 array[i] = ((Boolean) tmpVector.get(i)).booleanValue();
248
249 // System.out.print(""+(array[i]==true?'1':'0'));
250 }
251
252 // System.out.println("");
253 return array;
254 }
255
256 /**
257 * Loads integer array from <tt>String</tt> . Format i_1,i_2,...,i_n. ','
258 * is here the default separator. It's slower than using
259 * <code>intArrayFromString</code>.
260 *
261 * @param sArrays Description of the Parameter
262 * @return Description of the Return Value
263 */
264 public int[] intArrayFromSimpleString(String sArrays)
265 {
266 return intArrayFromSimpleString(sArrays, separator);
267 }
268
269 /**
270 * Loads integer array from <tt>String</tt> . Format i_1,i_2,...,i_n. ','
271 * is here the default separator. It's slower than using
272 * <code>intArrayFromString</code>.
273 *
274 * @param separator Description of the Parameter
275 * @param sArrays Description of the Parameter
276 * @param size Description of the Parameter
277 * @return Description of the Return Value
278 */
279 public static int[] intArrayFromSimpleString(String sArrays,
280 String separator)
281 {
282 StringTokenizer st = new StringTokenizer(sArrays, separator);
283
284 // start with a vector of size 50
285 Vector tmpVector = new Vector(50);
286
287 // get integer values from String
288 String tmpString;
289
290 while (st.hasMoreTokens())
291 {
292 tmpString = st.nextToken();
293 tmpVector.add(new Integer(tmpString));
294 }
295
296 // copy values to integer array
297 int size = tmpVector.size();
298 int[] array = new int[size];
299
300 for (int i = 0; i < size; i++)
301 {
302 array[i] = ((Integer) tmpVector.get(i)).intValue();
303 }
304
305 return array;
306 }
307
308 /**
309 * Loads integer array from <tt>String</tt> . Format n<i_1,i_2,...,i_n>. ','
310 * is here the default separator. It's faster to use this method than using
311 * <code>intArrayFromSimpleString</code>.
312 *
313 * @param sArrays Description of the Parameter
314 * @return Description of the Return Value
315 */
316 public Vector intArrayFromString(String sArrays)
317 {
318 return intArrayFromString(sArrays, separator, -1);
319 }
320
321 /**
322 * Loads integer array from <tt>String</tt> . Format n<i_1,i_2,...,i_n>. ','
323 * is here the default separator. It's faster to use this method than using
324 * <code>intArrayFromSimpleString</code>.
325 *
326 * @param separator Description of the Parameter
327 * @param sArrays Description of the Parameter
328 * @param size Description of the Parameter
329 * @return Description of the Return Value
330 */
331 public static Vector intArrayFromString(String sArrays, String separator,
332 int size)
333 {
334 String tmpString;
335 int endOfArray;
336 int startOfArray;
337 int arrayLength = 0;
338
339 startOfArray = sArrays.indexOf('<');
340
341 if (startOfArray == -1)
342 {
343 return null;
344 }
345
346 if (size == -1)
347 {
348 tmpString = sArrays.substring(0, startOfArray);
349 arrayLength = Integer.parseInt(tmpString);
350 }
351 else
352 {
353 arrayLength = size;
354 }
355
356 tmpString = sArrays.substring(startOfArray, sArrays.length());
357
358 StringTokenizer st = new StringTokenizer(tmpString, separator);
359 int arrayIndex = 0;
360 int index = 0;
361 int[] array = new int[arrayLength];
362 Vector tmpVector = new Vector();
363 tmpVector.add(array);
364
365 while (st.hasMoreTokens())
366 {
367 tmpString = st.nextToken();
368
369 if (tmpString.charAt(0) == '<')
370 {
371 tmpString = tmpString.substring(1, tmpString.length());
372 }
373 else if ((endOfArray = tmpString.lastIndexOf('>')) != -1)
374 {
375 // add last index entry from this array
376 array[index] = Integer.parseInt(tmpString.substring(0,
377 endOfArray));
378
379 // and get the first entry from the new one
380 startOfArray = tmpString.indexOf('<');
381
382 if (startOfArray == -1)
383 {
384 return tmpVector;
385 }
386
387 arrayIndex++;
388 arrayLength = Integer.parseInt(tmpString.substring(endOfArray +
389 1, startOfArray).trim());
390 array = new int[arrayLength];
391 tmpVector.add(array);
392 index = 0;
393 tmpString = tmpString.substring(startOfArray + 1,
394 tmpString.length());
395 }
396
397 if (index < array.length)
398 {
399 array[index] = Integer.parseInt(tmpString);
400 }
401 else
402 {
403 // throw new ArrayIndexOutOfBoundsException("int array at Vector index "+arrayIndex+" is out of range ("+index+").");
404 logger.error("int array at Vector index " + arrayIndex +
405 " is out of range.");
406
407 return null;
408 }
409
410 index++;
411 }
412
413 return tmpVector;
414 }
415
416 /**
417 * Loads boolean array from <tt>String</tt> . Format n<b_1,b_2,...,b_n>. ','
418 * is here the default separator. It's faster to use this method than using
419 * <code>booleanArrayFromSimpleString</code>.
420 *
421 * @param sArrays Description of the Parameter
422 * @return Description of the Return Value
423 */
424 public Vector booleanArrayFromString(String sArrays)
425 {
426 return booleanArrayFromString(sArrays, separator, -1);
427 }
428
429 /**
430 * Loads boolean array from <tt>String</tt> . Format n<b_1,b_2,...,b_n>. ','
431 * is here the default separator. It's faster to use this method than using
432 * <code>booleanArrayFromSimpleString</code>.
433 *
434 * @param separator Description of the Parameter
435 * @param sArrays Description of the Parameter
436 * @param size Description of the Parameter
437 * @return Description of the Return Value
438 */
439 public static Vector booleanArrayFromString(String sArrays,
440 String separator, int size)
441 {
442 String tmpString;
443 int endOfArray;
444 int startOfArray;
445 int arrayLength = 0;
446
447 startOfArray = sArrays.indexOf('<');
448
449 if (startOfArray == -1)
450 {
451 return null;
452 }
453
454 if (size == -1)
455 {
456 tmpString = sArrays.substring(0, startOfArray);
457 arrayLength = Integer.parseInt(tmpString);
458 }
459 else
460 {
461 arrayLength = size;
462 }
463
464 tmpString = sArrays.substring(startOfArray, sArrays.length());
465
466 StringTokenizer st = new StringTokenizer(tmpString, separator);
467 int arrayIndex = 0;
468 int index = 0;
469 boolean[] array = new boolean[arrayLength];
470 Vector tmpVector = new Vector();
471 tmpVector.add(array);
472
473 while (st.hasMoreTokens())
474 {
475 tmpString = st.nextToken();
476
477 if (tmpString.charAt(0) == '<')
478 {
479 tmpString = tmpString.substring(1, tmpString.length());
480 }
481 else if ((endOfArray = tmpString.lastIndexOf('>')) != -1)
482 {
483 // add last index entry from this array
484 array[index] = (tmpString.substring(0, endOfArray).equals("1")
485 ? true : false);
486
487 // and get the first entry from the new one
488 startOfArray = tmpString.indexOf('<');
489
490 if (startOfArray == -1)
491 {
492 return tmpVector;
493 }
494
495 arrayIndex++;
496 arrayLength = Integer.parseInt(tmpString.substring(endOfArray +
497 1, startOfArray).trim());
498 array = new boolean[arrayLength];
499 tmpVector.add(array);
500 index = 0;
501 tmpString = tmpString.substring(startOfArray + 1,
502 tmpString.length());
503 }
504
505 if (index < array.length)
506 {
507 array[index] = (tmpString.equals("1") ? true : false);
508 }
509 else
510 {
511 // throw new ArrayIndexOutOfBoundsException("int array at Vector index "+arrayIndex+" is out of range ("+index+").");
512 logger.error("boolean array at Vector index " + arrayIndex +
513 " is out of range.");
514
515 return null;
516 }
517
518 index++;
519 }
520
521 return tmpVector;
522 }
523
524 /**
525 * Loads integer array from <tt>String</tt> . Format i_1,i_2,...,i_n. ','
526 * is here the default separator. It's slower than using
527 * <code>intArrayFromString</code>.
528 *
529 * @param sArrays Description of the Parameter
530 * @return Description of the Return Value
531 */
532 public double[] doubleArrayFromSimpleString(String sArrays)
533 {
534 return doubleArrayFromSimpleString(sArrays, separator);
535 }
536
537 /**
538 * Loads integer array from <tt>String</tt> . Format i_1,i_2,...,i_n. ','
539 * is here the default separator. It's slower than using
540 * <code>intArrayFromString</code>.
541 *
542 * @param separator Description of the Parameter
543 * @param sArrays Description of the Parameter
544 * @param size Description of the Parameter
545 * @return Description of the Return Value
546 */
547 public static double[] doubleArrayFromSimpleString(String sArrays,
548 String separator)
549 {
550 StringTokenizer st = new StringTokenizer(sArrays, separator);
551
552 // start with a vector of size 50
553 Vector tmpVector = new Vector(50);
554
555 // get integer values from String
556 String tmpString;
557
558 while (st.hasMoreTokens())
559 {
560 tmpString = st.nextToken();
561 tmpVector.add(new Double(tmpString));
562 }
563
564 // copy values to integer array
565 int size = tmpVector.size();
566 double[] array = new double[size];
567
568 for (int i = 0; i < size; i++)
569 {
570 array[i] = ((Double) tmpVector.get(i)).doubleValue();
571 }
572
573 return array;
574 }
575
576 /**
577 * Loads double array from <tt>String</tt> . Format n<d_1,d_2,...,d_n>. ','
578 * is here the default separator. It's faster to use this method than using
579 * <code>doubleArrayFromSimpleString</code>.
580 *
581 * @param sArrays Description of the Parameter
582 * @return Description of the Return Value
583 */
584 public Vector doubleArrayFromString(String sArrays)
585 {
586 return doubleArrayFromString(sArrays, separator, -1);
587 }
588
589 /**
590 * Loads double array from <tt>String</tt> . Format n<d_1,d_2,...,d_n>. ','
591 * is here the default separator. It's faster to use this method than using
592 * <code>doubleArrayFromSimpleString</code>.
593 *
594 *
595 * @param separator Description of the Parameter
596 * @param sArrays Description of the Parameter
597 * @param size Description of the Parameter
598 * @return Description of the Return Value
599 */
600 public static Vector doubleArrayFromString(String sArrays,
601 String separator, int size)
602 {
603 String tmpString;
604 int endOfArray;
605 int startOfArray;
606 int arrayLength = 0;
607
608 startOfArray = sArrays.indexOf('<');
609
610 if (startOfArray == -1)
611 {
612 return null;
613 }
614
615 if (size == -1)
616 {
617 tmpString = sArrays.substring(0, startOfArray);
618 arrayLength = Integer.parseInt(tmpString);
619 }
620 else
621 {
622 arrayLength = size;
623 }
624
625 tmpString = sArrays.substring(startOfArray, sArrays.length());
626
627 StringTokenizer st = new StringTokenizer(tmpString, separator);
628 int arrayIndex = 0;
629 int index = 0;
630 double[] array = new double[arrayLength];
631 Vector tmpVector = new Vector();
632 tmpVector.add(array);
633
634 while (st.hasMoreTokens())
635 {
636 tmpString = st.nextToken();
637
638 if (tmpString.charAt(0) == '<')
639 {
640 tmpString = tmpString.substring(1, tmpString.length());
641 }
642 else if ((endOfArray = tmpString.lastIndexOf('>')) != -1)
643 {
644 // add last index entry from this array
645 array[index] = Double.parseDouble(tmpString.substring(0,
646 endOfArray));
647
648 // and get the first entry from the new one
649 startOfArray = tmpString.indexOf('<');
650
651 if (startOfArray == -1)
652 {
653 return tmpVector;
654 }
655
656 arrayIndex++;
657 arrayLength = Integer.parseInt(tmpString.substring(endOfArray +
658 1, startOfArray).trim());
659 array = new double[arrayLength];
660 tmpVector.add(array);
661 index = 0;
662 tmpString = tmpString.substring(startOfArray + 1,
663 tmpString.length());
664 }
665
666 if (index < array.length)
667 {
668 array[index] = Double.parseDouble(tmpString);
669 }
670 else
671 {
672 // throw new ArrayIndexOutOfBoundsException("int array at Vector index "+arrayIndex+" is out of range ("+index+").");
673 logger.error("double array at Vector index " + arrayIndex +
674 " is out of range.");
675
676 return null;
677 }
678
679 index++;
680 }
681
682 return tmpVector;
683 }
684
685 /**
686 * Write integer array to <tt>String</tt> . Format i_1,i_2,...,i_n. ','
687 * is here the default separator.
688 *
689 * @param sb Description of the Parameter
690 * @param arrayrray Description of the Parameter
691 * @return Description of the Return Value
692 */
693 public StringBuffer toSimpleString(StringBuffer sb, int[] arrayrray)
694 {
695 return toSimpleString(sb, arrayrray, separator);
696 }
697
698 /**
699 * Write integer array to <tt>String</tt> . Format i_1,i_2,...,i_n. ','
700 * is here the default separator.
701 *
702 * @param sb Description of the Parameter
703 * @param arrayrray Description of the Parameter
704 * @return Description of the Return Value
705 */
706 public StringBuffer toSimpleString(StringBuffer sb, double[] arrayrray)
707 {
708 return toSimpleString(sb, arrayrray, separator);
709 }
710
711 /**
712 * Write integer array to <tt>String</tt> . Format i_1,i_2,...,i_n. ','
713 * is here the default separator.
714 *
715 * @param sb Description of the Parameter
716 * @param arrayrray Description of the Parameter
717 * @return Description of the Return Value
718 */
719 public StringBuffer toSimpleString(StringBuffer sb, double[] arrayrray,
720 DecimalFormatter format)
721 {
722 return toSimpleString(sb, arrayrray, separator, format);
723 }
724
725 /**
726 * Write boolean array to <tt>String</tt> . Format i_1,i_2,...,i_n. ','
727 * is here the default separator.
728 *
729 * @param sb Description of the Parameter
730 * @param arrayrray Description of the Parameter
731 * @return Description of the Return Value
732 */
733 public StringBuffer toSimpleString(StringBuffer sb, boolean[] arrayrray)
734 {
735 return toSimpleString(sb, arrayrray, separator);
736 }
737
738 /**
739 * Write integer array to <tt>String</tt> . Format i_1,i_2,...,i_n. ','
740 * is here the default separator.
741 *
742 * @param sb Description of the Parameter
743 * @param array Description of the Parameter
744 * @param separator Description of the Parameter
745 * @param writeLength Description of the Parameter
746 * @return Description of the Return Value
747 */
748 public static StringBuffer toSimpleString(StringBuffer sb, int[] array,
749 String separator)
750 {
751 if (array == null)
752 {
753 logger.warn("Empty array.");
754
755 return sb;
756 }
757
758 for (int i = 0; i < array.length; i++)
759 {
760 sb.append(array[i]);
761
762 if (i < (array.length - 1))
763 {
764 sb.append(separator);
765 }
766 }
767
768 return sb;
769 }
770
771 /**
772 * Write integer array to <tt>String</tt> . Format i_1,i_2,...,i_n. ','
773 * is here the default separator.
774 *
775 * @param sb Description of the Parameter
776 * @param array Description of the Parameter
777 * @param separator Description of the Parameter
778 * @param writeLength Description of the Parameter
779 * @return Description of the Return Value
780 */
781 public static StringBuffer toSimpleString(StringBuffer sb, double[] array,
782 String separator)
783 {
784 return toSimpleString(sb, array, separator, null);
785 }
786
787 /**
788 * Write integer array to <tt>String</tt> . Format i_1,i_2,...,i_n. ','
789 * is here the default separator.
790 *
791 * @param sb Description of the Parameter
792 * @param array Description of the Parameter
793 * @param separator Description of the Parameter
794 * @param writeLength Description of the Parameter
795 * @return Description of the Return Value
796 */
797 public static StringBuffer toSimpleString(StringBuffer sb, double[] array,
798 String separator, DecimalFormatter format)
799 {
800 if (array == null)
801 {
802 logger.warn("Empty array.");
803
804 return sb;
805 }
806
807 for (int i = 0; i < array.length; i++)
808 {
809 if (format == null)
810 {
811 sb.append(array[i]);
812 }
813 else
814 {
815 sb.append(format.format(array[i]));
816 }
817
818 if (i < (array.length - 1))
819 {
820 sb.append(separator);
821 }
822 }
823
824 return sb;
825 }
826
827 /**
828 * Write boolean array to <tt>String</tt> . Format i_1,i_2,...,i_n. ','
829 * is here the default separator.
830 *
831 * @param sb Description of the Parameter
832 * @param array Description of the Parameter
833 * @param separator Description of the Parameter
834 * @param writeLength Description of the Parameter
835 * @return Description of the Return Value
836 */
837 public static StringBuffer toSimpleString(StringBuffer sb, boolean[] array,
838 String separator)
839 {
840 if (array == null)
841 {
842 logger.warn("Empty array.");
843
844 return sb;
845 }
846
847 for (int i = 0; i < array.length; i++)
848 {
849 if (array[i])
850 {
851 sb.append('1');
852 }
853 else
854 {
855 sb.append('0');
856 }
857
858 if (i < (array.length - 1))
859 {
860 sb.append(separator);
861 }
862 }
863
864 return sb;
865 }
866
867 /**
868 * Write integer array to <tt>String</tt> . Format n<i_1,i_2, ...,i_n>. ','
869 * is here the default separator.
870 *
871 * @param arrayrray Description of the Parameter
872 * @param sb Description of the Parameter
873 * @return Description of the Return Value
874 */
875 public StringBuffer toString(StringBuffer sb, int[] arrayrray)
876 {
877 return toString(sb, arrayrray, separator, true);
878 }
879
880 /**
881 * Write integer array to <tt>String</tt> . Format n<i_1,i_2,...,i_n>. ','
882 * is here the default separator.
883 *
884 * @param separator Description of the Parameter
885 * @param sb Description of the Parameter
886 * @param array Description of the Parameter
887 * @param writeLength Description of the Parameter
888 * @return Description of the Return Value
889 */
890 public static StringBuffer toString(StringBuffer sb, int[] array,
891 String separator, boolean writeLength)
892 {
893 if (array == null)
894 {
895 logger.warn("Empty array.");
896
897 return sb;
898 }
899
900 if (writeLength)
901 {
902 sb.append(array.length);
903 }
904
905 sb.append("<");
906
907 for (int i = 0; i < array.length; i++)
908 {
909 sb.append(array[i]);
910
911 if (i < (array.length - 1))
912 {
913 sb.append(separator);
914 }
915 }
916
917 sb.append(">");
918
919 return sb;
920 }
921
922 /**
923 * Write boolean array to <tt>String</tt> . Format i_1,i_2,...,i_n. ','
924 * is here the default separator.
925 *
926 * @param sb Description of the Parameter
927 * @param array Description of the Parameter
928 * @param separator Description of the Parameter
929 * @param writeLength Description of the Parameter
930 * @return Description of the Return Value
931 */
932 public static StringBuffer toTrueFalseString(StringBuffer sb,
933 boolean[] array, String separator)
934 {
935 for (int i = 0; i < array.length; i++)
936 {
937 sb.append(array[i]);
938
939 if (i < (array.length - 1))
940 {
941 sb.append(separator);
942 }
943 }
944
945 return sb;
946 }
947
948 /**
949 * Description of the Method
950 *
951 * @param dArray Description of the Parameter
952 * @param sb Description of the Parameter
953 * @return Description of the Return Value
954 */
955 public StringBuffer toString(StringBuffer sb, double[] dArray)
956 {
957 return toString(sb, dArray, separator, true);
958 }
959
960 /**
961 * Description of the Method
962 *
963 * @param dArray Description of the Parameter
964 * @param sb Description of the Parameter
965 * @return Description of the Return Value
966 */
967 public StringBuffer toString(StringBuffer sb, double[] dArray,
968 DecimalFormatter format)
969 {
970 return toString(sb, dArray, separator, true, format);
971 }
972
973 /**
974 * Description of the Method
975 *
976 * @param separator Description of the Parameter
977 * @param sb Description of the Parameter
978 * @param array Description of the Parameter
979 * @param writeLength Description of the Parameter
980 * @return Description of the Return Value
981 */
982 public static StringBuffer toString(StringBuffer sb, double[] array,
983 String separator, boolean writeLength)
984 {
985 return toString(sb, array, separator, writeLength, null);
986 }
987
988 /**
989 * Description of the Method
990 *
991 * @param separator Description of the Parameter
992 * @param sb Description of the Parameter
993 * @param array Description of the Parameter
994 * @param writeLength Description of the Parameter
995 * @return Description of the Return Value
996 */
997 public static StringBuffer toString(StringBuffer sb, double[] array,
998 String separator, boolean writeLength, DecimalFormatter format)
999 {
1000 if (array == null)
1001 {
1002 logger.warn("Empty array.");
1003
1004 return sb;
1005 }
1006
1007 if (writeLength)
1008 {
1009 sb.append(array.length);
1010 }
1011
1012 sb.append("<");
1013
1014 for (int i = 0; i < array.length; i++)
1015 {
1016 if (format == null)
1017 {
1018 sb.append(array[i]);
1019 }
1020 else
1021 {
1022 sb.append(format.format(array[i]));
1023 }
1024
1025 if (i < (array.length - 1))
1026 {
1027 sb.append(separator);
1028 }
1029 }
1030
1031 sb.append(">");
1032
1033 return sb;
1034 }
1035
1036 /**
1037 * Description of the Method
1038 *
1039 * @param sb Description of the Parameter
1040 * @param dArray Description of the Parameter
1041 * @return Description of the Return Value
1042 */
1043 public StringBuffer toString(StringBuffer sb, boolean[] dArray)
1044 {
1045 return toString(sb, dArray, separator, true);
1046 }
1047
1048 /**
1049 * Description of the Method
1050 *
1051 * @param sb Description of the Parameter
1052 * @param array Description of the Parameter
1053 * @param separator Description of the Parameter
1054 * @param writeLength Description of the Parameter
1055 * @return Description of the Return Value
1056 */
1057 public static StringBuffer toString(StringBuffer sb, boolean[] array,
1058 String separator, boolean writeLength)
1059 {
1060 if (array == null)
1061 {
1062 logger.warn("Empty array.");
1063
1064 return sb;
1065 }
1066
1067 if (writeLength)
1068 {
1069 sb.append(array.length);
1070 }
1071
1072 sb.append("<");
1073
1074 for (int i = 0; i < array.length; i++)
1075 {
1076 sb.append((array[i]) ? '1' : '0');
1077
1078 if (i < (array.length - 1))
1079 {
1080 sb.append(separator);
1081 }
1082 }
1083
1084 sb.append(">");
1085
1086 return sb;
1087 }
1088}
1089///////////////////////////////////////////////////////////////////////////////
1090// END OF FILE.
1091///////////////////////////////////////////////////////////////////////////////