View Javadoc

1   /*
2    * 
3    * 
4    */
5   package net.sf.diaz;
6   
7   import java.util.*;
8   import java.math.BigDecimal;
9   import java.sql.Time;
10  import java.sql.Timestamp;
11  import java.sql.Blob;
12  
13  /**
14   * 
15   * @author Sean C. Sullivan
16   *
17   */
18  public class Row
19  {
20  	private IndexedMap<String, Object> data = null;
21  	
22  	protected Row(IndexedMap<String, Object> columnData)
23  	{
24  		this.data = columnData;
25  	}
26  	
27  	public String getString(String colName)
28  	{
29  		return (String) this.getObject(colName);
30  	}
31  
32  	public String getString(int colIndex)
33  	{
34  		return (String) this.getObject(colIndex);
35  	}
36  
37  	public Date getDate(String colName)
38  	{
39  		return (Date) this.getObject(colName);
40  	}
41  
42  	public Date getDate(int colIndex)
43  	{
44  		return (Date) this.getObject(colIndex);
45  	}
46  
47  	public Time getTime(String colName)
48  	{
49  		return (Time) this.getObject(colName);
50  	}
51  
52  	public Time getTime(int colIndex)
53  	{
54  		return (Time) this.getObject(colIndex);
55  	}
56  
57  	public Timestamp getTimestamp(String colName)
58  	{
59  		return (Timestamp) this.getObject(colName);
60  	}
61  
62  	public Timestamp getTimestamp(int colIndex)
63  	{
64  		return (Timestamp) this.getObject(colIndex);
65  	}
66  
67  	public long getLong(String colName)
68  	{
69  		return getBigDecimal(colName).longValue();
70  	}
71  
72  	public long getLong(int colIndex)
73  	{
74  		return getBigDecimal(colIndex).longValue();
75  	}
76  
77  	public int getInt(String colName)
78  	{
79  		return getNumber(colName).intValue();
80  	}
81  
82  	public long getInt(int colIndex)
83  	{
84  		return getNumber(colIndex).intValue();
85  	}
86  
87  	public double getDouble(String colName)
88  	{
89  		return getNumber(colName).doubleValue();
90  	}
91  
92  	public double getDouble(int colIndex)
93  	{
94  		return getNumber(colIndex).doubleValue();
95  	}
96  
97  	public float getFloat(int colIndex)
98  	{
99  		return getNumber(colIndex).floatValue();
100 	}
101 
102 	public float getFloat(String colName)
103 	{
104 		return getNumber(colName).floatValue();
105 	}
106 
107 	public BigDecimal getBigDecimal(int colIndex)
108 	{
109 		Number n = this.getNumber(colIndex);
110 		return getBigDecimal(n);
111 	}
112 
113 	protected BigDecimal getBigDecimal(Number n)
114 	{
115 		BigDecimal bd = null;
116 		if (n instanceof BigDecimal)
117 		{
118 			bd = (BigDecimal) n;
119 		}
120 		else
121 		{
122 			bd = new BigDecimal(n.doubleValue());
123 		}
124 		return bd;
125 	}
126 	
127 	public BigDecimal getBigDecimal(String colName)
128 	{
129 		Number n = this.getNumber(colName);
130 		return getBigDecimal(n);
131 	}
132 
133 	public boolean getBoolean(int colIndex)
134 	{
135 		return ((Boolean) this.getObject(colIndex)).booleanValue();
136 	}
137 
138 	public boolean getBoolean(String colName)
139 	{
140 		return ((Boolean) this.getObject(colName)).booleanValue();
141 	}
142 
143 	public Blob getBlob(int colIndex)
144 	{
145 		return ((Blob) this.getObject(colIndex));
146 	}
147 
148 	public Blob getBlob(String colName)
149 	{
150 		return ((Blob) this.getObject(colName));
151 	}
152 
153 	public Class getColumnType(int colIndex)
154 	{
155 		Object o = this.getObject(colIndex);
156 		return o.getClass();
157 	}
158 
159 	public Object getObject(String colName)
160 	{
161 		return this.data.get(colName.toUpperCase());
162 	}
163 	
164 	public Object getObject(int colIndex)
165 	{
166 		return this.data.get(colIndex);
167 	}
168 	
169 	public Number getNumber(int colIndex)
170 	{
171 		Number n = (Number) this.getObject(colIndex);
172 		return n;
173 	}
174 	
175 	public Number getNumber(String colName)
176 	{
177 		Number n = (Number) this.getObject(colName);
178 		return n;
179 	}
180 	
181 	public int getColumnCount()
182 	{
183 		return this.data.keySet().size();
184 	}
185 	
186 	public Set<String> getColumnNames()
187 	{
188 		return this.data.keySet();
189 	}
190 	
191 	public Class[] getColumnTypes()
192 	{
193 		Class[] classArray = new Class[this.getColumnCount()];
194 		for (int i = 0; i < classArray.length; i++)
195 		{
196 			classArray[i] = this.getColumnType(i);
197 		}
198 		return classArray;
199 	}
200 	
201 	public String toString()
202 	{
203 		StringBuffer sb = new StringBuffer();
204 		sb.append("ColumnCount=");
205 		sb.append(getColumnCount());
206 		sb.append(" [ ");
207 		Set<String> colNames = getColumnNames();
208 		for (String name : colNames)
209 		{
210 			sb.append(name);
211 			sb.append(" ");
212 		}
213 		sb.append("]");
214 		return sb.toString();
215 	}
216 }