1 /*
2 * Copyright (c) 2003, Henri Yandell
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or
6 * without modification, are permitted provided that the
7 * following conditions are met:
8 *
9 * + Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * + Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * + Neither the name of Genjava-Core nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32 package com.generationjava.io;
33
34 import java.util.Collection;
35 import java.util.Iterator;
36 import java.util.Arrays;
37 import java.util.List;
38 import java.util.ArrayList;
39
40 /***
41 * A TableRow contains one or many objects.
42 */
43 public class TableRow implements Iterator {
44
45 private List stringList;
46 private int[] colsizes;
47 private Iterator myIterator;
48 private int ptr;
49
50 public TableRow(Object obj) {
51 ptr = 0;
52 if(obj != null) {
53 stringList = new ArrayList();
54
55 // convert all arrays to Collection
56 if(obj instanceof Object[]) {
57 obj = Arrays.asList( (Object[])obj );
58 }
59
60 if(obj instanceof Collection) {
61 Collection coll = (Collection)obj;
62 colsizes = new int[coll.size()];
63 int i=0;
64 Iterator iterator = coll.iterator();
65 while(iterator.hasNext()) {
66 Object nested_obj = iterator.next();
67 String str = "";
68 if(nested_obj instanceof Object[]) {
69 str = arrayToString( (Object[])nested_obj );
70 } else {
71 str = ""+nested_obj;
72 }
73 stringList.add( str );
74 colsizes[i] = str.length();
75 i++;
76 }
77 } else {
78 colsizes = new int[1];
79 String str = obj.toString();
80 colsizes[0] = str.length();
81 stringList.add(str);
82 }
83 myIterator = stringList.iterator();
84 }
85 }
86
87 public boolean hasNext() {
88 if(myIterator != null) {
89 return myIterator.hasNext();
90 } else {
91 return false;
92 }
93 }
94
95 public void remove() {}
96
97 public Object next() {
98 if(myIterator != null) {
99 ptr++;
100 return myIterator.next();
101 } else {
102 // throw an exception???
103 return null;
104 }
105 }
106
107 public void reset() {
108 myIterator = null;
109 }
110
111 public boolean firstColumn() {
112 return (ptr==1);
113 }
114
115 public boolean lastColumn() {
116 return (ptr==getWidth());
117 }
118
119 public int getColumnWidth() {
120 if(ptr == 0) {
121 return 0;
122 } else {
123 return colsizes[ptr-1];
124 }
125 }
126
127 public int getWidth() {
128 if(stringList == null) {
129 return 0;
130 } else {
131 return stringList.size();
132 }
133 }
134
135 public int[] getColumnSizes() {
136 return colsizes;
137 }
138
139 private String arrayToString(Object[] arr) {
140 if(arr == null) {
141 return null;
142 }
143 /*
144 StringBuffer buffer = new StringBuffer();
145 buffer.append("{");
146 int sz = arr.length;
147 for(int i=0;i<sz;i++) {
148 buffer.append(""+arr[i]);
149 if(i != sz-1) {
150 buffer.append(", ");
151 }
152 }
153 buffer.append("}");
154 return buffer.toString();
155 */
156 return Arrays.asList(arr).toString();
157 }
158 }