001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020: package com.salmonllc.sql;
021:
022: /////////////////////////
023: //$Archive: /SOFIA/SourceCode/com/salmonllc/sql/DSQuickSort.java $
024: //$Author: Srufle $
025: //$Revision: 17 $
026: //$Modtime: 10/29/04 11:36a $
027: /////////////////////////
028:
029: import java.util.*;
030: import java.sql.Timestamp;
031: import java.sql.Time;
032:
033: /**
034: * This class is public as an implementation detail. It should not be used outside the framework.
035: */
036:
037: public class DSQuickSort {
038: public static String ROW = "ROW";
039: public static String SORTVALUE = "SORTVALUE";
040: Vector _list;
041: int _cols[];
042: int _dirs[];
043: DataStoreEvaluator _eval;
044: Object _objs[];
045: Stack _stack = new Stack();
046: Hashtable _hashmaps = new Hashtable();
047:
048: private class DataStoreSort extends DataStoreBuffer {
049: DataStoreSort() {
050: super ();
051: addBucket(ROW, DATATYPE_INT);
052: addBucket(SORTVALUE, DATATYPE_STRING);
053: }
054:
055: void setColumnDataType(int column, int type)
056: throws DataStoreException {
057: if (column < 0 || column >= _desc.getColumnCount())
058: throw new DataStoreException("Specified column ("
059: + column + ") is out of range.");
060:
061: DSColumnDescriptor des = _desc.getColumn(column);
062: des.setType(type);
063: }
064:
065: void setColumnDataType(String column, int type)
066: throws DataStoreException {
067: int col = getColumnIndex(column);
068: setColumnDataType(col, type);
069: }
070:
071: }
072:
073: DSQuickSort(Vector list, int cols[], int dirs[]) {
074: this (list, cols, dirs, false);
075: }
076:
077: DSQuickSort(Vector list, int cols[], int dirs[], boolean bUseQSort) {
078: _cols = cols;
079: _dirs = dirs;
080: _list = list;
081: if (_list.size() < 500 || bUseQSort)
082: sort(0, _list.size() - 1);
083: else
084: sort();
085: }
086:
087: DSQuickSort(Vector list, Object eval[], int dirs[]) {
088: this (list, eval, dirs, false);
089: }
090:
091: DSQuickSort(Vector list, Object eval[], int dirs[],
092: boolean bUseQSort) {
093: _objs = eval;
094: _dirs = dirs;
095: _list = list;
096: if (_list.size() < 500 || bUseQSort)
097: sort(0, _list.size() - 1);
098: else
099: sort();
100: }
101:
102: DSQuickSort(Vector list, DataStoreEvaluator eval, int dir) {
103: this (list, eval, dir, false);
104: }
105:
106: DSQuickSort(Vector list, DataStoreEvaluator eval, int dir,
107: boolean bUseQSort) {
108: _eval = eval;
109: _dirs = new int[1];
110: _dirs[0] = dir;
111: _list = list;
112: if (_list.size() < 500 || bUseQSort)
113: sort(0, _list.size() - 1);
114: else
115: sort();
116: }
117:
118: private int divide(int low, int high) {
119: int i, ptr;
120:
121: DSDataRow key = (DSDataRow) _list.elementAt(low);
122: ptr = low;
123:
124: for (i = low + 1; i <= high; i++) {
125: if (_eval != null) {
126: if (((DSDataRow) _list.elementAt(i))
127: .compareExpressions(_eval, i, low, _dirs[0]))
128: swap(++ptr, i);
129: } else if (_objs != null) {
130: if (((DSDataRow) _list.elementAt(i)).compareRows(i,
131: low, key, _objs, _dirs))
132: swap(++ptr, i);
133: } else if (((DSDataRow) _list.elementAt(i)).compareRows(
134: key, _cols, _dirs)) {
135: swap(++ptr, i);
136: }
137:
138: }
139: swap(low, ptr);
140: return ptr;
141: }
142:
143: private void sort(int low, int high) {
144: int ptr;
145: _stack.ensureCapacity(high - low);
146:
147: _stack.push(new int[] { low, high });
148: do {
149: int[] hl = (int[]) _stack.pop();
150: low = hl[0];
151: high = hl[1];
152: while (low < high) {
153: ptr = divide(low, high);
154: if ((ptr - low) < (high - ptr)) {
155: _stack.push(new int[] { ptr + 1, high });
156: high = ptr - 1;
157: } else {
158: _stack.push(new int[] { low, ptr - 1 });
159: low = ptr + 1;
160: }
161: }
162: Thread.yield();
163: } while (_stack.size() > 0);
164: /* if (low < high) {
165: ptr = divide(low, high);
166: sort(low, ptr - 1);
167: sort(ptr + 1, high);
168: }*/
169: }
170:
171: private void swap(int a, int b) {
172: if (a == b)
173: return;
174: Object tmp = _list.elementAt(a);
175: _list.setElementAt(_list.elementAt(b), a);
176: _list.setElementAt(tmp, b);
177: }
178:
179: private void createHashMaps() {
180: int iType = -1;
181: int iOptStringHashMapLength = 1;
182: int iOptNumberDivider = 100;
183: for (int i = 0; i < _list.size(); i++) {
184: Object o = null;
185: if (_eval != null) {
186: try {
187: o = _eval.evaluateRow(i);
188: } catch (DataStoreException dse) {
189: ;
190: }
191: } else if (_objs != null) {
192:
193: if (_objs[i] instanceof DataStoreEvaluator) {
194: DataStoreEvaluator de = (DataStoreEvaluator) _objs[0];
195: try {
196: o = de.evaluateRow(i);
197: } catch (Exception e) {
198: o = null;
199: }
200:
201: } else {
202: DSDataRow dsRow = (DSDataRow) _list.elementAt(i);
203: o = dsRow.getData(((Integer) _objs[0]).intValue());
204: }
205:
206: } else {
207: DSDataRow dsRow = (DSDataRow) _list.elementAt(i);
208: Object value;
209:
210: value = dsRow.getData(_cols[0]);
211: o = value;
212: }
213: if (iType == -1 && o != null) {
214: if (o instanceof Number)
215: iOptNumberDivider = optimalNumberHashMapDivider(
216: _list, _eval, _objs == null ? null
217: : _objs[0], _cols == null ? 0
218: : _cols[0]);
219: // System.out.println("iOptNumberDivider="+iOptNumberDivider);
220: if (o instanceof String) {
221: iType = DataStoreBuffer.DATATYPE_STRING;
222: iOptStringHashMapLength = optimalStringHashMapLength(
223: _list, _eval, _objs == null ? null
224: : _objs[0], _cols == null ? 0
225: : _cols[0]);
226: // System.out.println("iOptStringHashMapLength="+iOptStringHashMapLength);
227: } else if (o instanceof Short) {
228: iType = DataStoreBuffer.DATATYPE_SHORT;
229: } else if (o instanceof Integer) {
230: iType = DataStoreBuffer.DATATYPE_INT;
231: } else if (o instanceof Long) {
232: iType = DataStoreBuffer.DATATYPE_LONG;
233: } else if (o instanceof Float) {
234: iType = DataStoreBuffer.DATATYPE_FLOAT;
235: } else if (o instanceof Double) {
236: iType = DataStoreBuffer.DATATYPE_DOUBLE;
237: } else if (o instanceof Timestamp) {
238: iType = DataStoreBuffer.DATATYPE_DATETIME;
239: } else if (o instanceof Date) {
240: iType = DataStoreBuffer.DATATYPE_DATE;
241: } else if (o instanceof Time) {
242: iType = DataStoreBuffer.DATATYPE_TIME;
243: } else
244: iType = DataStoreBuffer.DATATYPE_BYTEARRAY;
245: }
246: if (o == null)
247: o = "";
248: Object oMapKey = o;
249: if (!o.equals("")) {
250: switch (iType) {
251: case DataStoreBuffer.DATATYPE_STRING:
252: String sValue = o.toString();
253: oMapKey = sValue
254: .substring(0,
255: iOptStringHashMapLength > sValue
256: .length() ? sValue.length()
257: : iOptStringHashMapLength);
258: break;
259: case DataStoreBuffer.DATATYPE_SHORT:
260: short s = ((Short) o).shortValue();
261: s = (short) (s / iOptNumberDivider);
262: oMapKey = new Short(s);
263: break;
264: case DataStoreBuffer.DATATYPE_INT:
265: int in = ((Integer) o).intValue();
266: in = (int) (in / iOptNumberDivider);
267: oMapKey = new Integer(in);
268: break;
269: case DataStoreBuffer.DATATYPE_LONG:
270: long l = ((Long) o).longValue();
271: l = (long) (l / iOptNumberDivider);
272: oMapKey = new Long(l);
273: break;
274: case DataStoreBuffer.DATATYPE_FLOAT:
275: float f = ((Float) o).floatValue();
276: f = (float) ((long) (f / iOptNumberDivider));
277: oMapKey = new Float(f);
278: break;
279: case DataStoreBuffer.DATATYPE_DOUBLE:
280: double d = ((Double) o).doubleValue();
281: d = (double) ((long) (d / iOptNumberDivider));
282: oMapKey = new Double(d);
283: break;
284: case DataStoreBuffer.DATATYPE_DATETIME:
285: GregorianCalendar gco = new GregorianCalendar();
286: gco.setTime((Timestamp) o);
287: gco.set(Calendar.DATE, 1);
288: gco.set(Calendar.HOUR_OF_DAY, 0);
289: gco.set(Calendar.MINUTE, 0);
290: gco.set(Calendar.SECOND, 0);
291: gco.set(Calendar.MILLISECOND, 0);
292: oMapKey = new Timestamp(gco.getTime().getTime());
293: break;
294: case DataStoreBuffer.DATATYPE_DATE:
295: gco = new GregorianCalendar();
296: gco.setTime((Timestamp) o);
297: gco.set(Calendar.MONTH, 0);
298: gco.set(Calendar.DATE, 1);
299: gco.set(Calendar.HOUR_OF_DAY, 0);
300: gco.set(Calendar.MINUTE, 0);
301: gco.set(Calendar.SECOND, 0);
302: gco.set(Calendar.MILLISECOND, 0);
303: oMapKey = new Date(gco.getTime().getTime());
304: break;
305: case DataStoreBuffer.DATATYPE_TIME:
306: gco = new GregorianCalendar();
307: gco.setTime((Timestamp) o);
308: gco.set(Calendar.MINUTE, 0);
309: gco.set(Calendar.SECOND, 0);
310: gco.set(Calendar.MILLISECOND, 0);
311: oMapKey = new Time(gco.getTime().getTime());
312: break;
313: }
314: }
315: DataStoreSort dss = (DataStoreSort) _hashmaps.get(oMapKey);
316: if (dss == null) {
317: dss = new DataStoreSort();
318: if (_cols != null) {
319: for (int j = 0; j < _cols.length; j++) {
320: dss.addBucket("COL" + j,
321: DataStoreBuffer.DATATYPE_STRING);
322: }
323: }
324: if (_objs != null) {
325: for (int j = 0; j < _objs.length; j++) {
326: dss.addBucket("COL" + j,
327: DataStoreBuffer.DATATYPE_STRING);
328: }
329: }
330: _hashmaps.put(oMapKey, dss);
331: }
332: dss.insertRow();
333: try {
334: dss.setInt(ROW, i);
335: if (_eval != null) {
336: if (!o.toString().equals("")
337: && dss.getColumnDataType(SORTVALUE) == DataStoreBuffer.DATATYPE_STRING) {
338: switch (iType) {
339: case DataStoreBuffer.DATATYPE_STRING:
340: break;
341: case DataStoreBuffer.DATATYPE_SHORT:
342: dss.setColumnDataType(SORTVALUE,
343: DataStoreBuffer.DATATYPE_SHORT);
344: break;
345: case DataStoreBuffer.DATATYPE_INT:
346: dss.setColumnDataType(SORTVALUE,
347: DataStoreBuffer.DATATYPE_INT);
348: break;
349: case DataStoreBuffer.DATATYPE_LONG:
350: dss.setColumnDataType(SORTVALUE,
351: DataStoreBuffer.DATATYPE_LONG);
352: break;
353: case DataStoreBuffer.DATATYPE_FLOAT:
354: dss.setColumnDataType(SORTVALUE,
355: DataStoreBuffer.DATATYPE_FLOAT);
356: break;
357: case DataStoreBuffer.DATATYPE_DOUBLE:
358: dss.setColumnDataType(SORTVALUE,
359: DataStoreBuffer.DATATYPE_DOUBLE);
360: break;
361: case DataStoreBuffer.DATATYPE_DATETIME:
362: dss.setColumnDataType(SORTVALUE,
363: DataStoreBuffer.DATATYPE_DATETIME);
364: break;
365: case DataStoreBuffer.DATATYPE_DATE:
366: dss.setColumnDataType(SORTVALUE,
367: DataStoreBuffer.DATATYPE_DATE);
368: break;
369: case DataStoreBuffer.DATATYPE_TIME:
370: dss.setColumnDataType(SORTVALUE,
371: DataStoreBuffer.DATATYPE_TIME);
372: break;
373:
374: }
375: }
376: dss.setAny(SORTVALUE, o);
377: } else if (_objs != null) {
378: DSDataRow dsRow = (DSDataRow) _list.elementAt(i);
379: for (int j = 0; j < _objs.length; j++) {
380: Object value = null;
381: if (_objs[j] instanceof DataStoreEvaluator) {
382: DataStoreEvaluator de = (DataStoreEvaluator) _objs[j];
383: try {
384: value = de.evaluateRow(i);
385: } catch (Exception e) {
386: value = null;
387: }
388:
389: } else {
390: value = dsRow.getData(((Integer) _objs[j])
391: .intValue());
392: }
393: if (value != null
394: && dss.getColumnDataType("COL" + j) == DataStoreBuffer.DATATYPE_STRING) {
395: if (value instanceof String) {
396: } else if (value instanceof Short) {
397: dss.setColumnDataType("COL" + j,
398: DataStoreBuffer.DATATYPE_SHORT);
399: } else if (value instanceof Integer) {
400: dss.setColumnDataType("COL" + j,
401: DataStoreBuffer.DATATYPE_INT);
402: } else if (value instanceof Float) {
403: dss.setColumnDataType("COL" + j,
404: DataStoreBuffer.DATATYPE_FLOAT);
405: } else if (value instanceof Long) {
406: dss.setColumnDataType("COL" + j,
407: DataStoreBuffer.DATATYPE_LONG);
408: } else if (value instanceof Double) {
409: dss
410: .setColumnDataType(
411: "COL" + j,
412: DataStoreBuffer.DATATYPE_DOUBLE);
413: } else if (value instanceof Timestamp) {
414: dss
415: .setColumnDataType(
416: "COL" + j,
417: DataStoreBuffer.DATATYPE_DATETIME);
418: } else if (value instanceof java.sql.Date) {
419: dss.setColumnDataType("COL" + j,
420: DataStoreBuffer.DATATYPE_DATE);
421: } else if (value instanceof java.sql.Time) {
422: dss.setColumnDataType("COL" + j,
423: DataStoreBuffer.DATATYPE_TIME);
424: } else if (value instanceof byte[]) {
425: dss
426: .setColumnDataType(
427: "COL" + j,
428: DataStoreBuffer.DATATYPE_BYTEARRAY);
429: }
430: }
431: dss.setAny("COL" + j, value);
432: }
433: } else {
434: DSDataRow dsRow = (DSDataRow) _list.elementAt(i);
435: for (int j = 0; j < _cols.length; j++) {
436: Object value = null;
437: value = dsRow.getData(_cols[j]);
438: if (value != null
439: && dss.getColumnDataType("COL" + j) == DataStoreBuffer.DATATYPE_STRING) {
440: if (value instanceof String) {
441: } else if (value instanceof Short) {
442: dss.setColumnDataType("COL" + j,
443: DataStoreBuffer.DATATYPE_INT);
444: } else if (value instanceof Integer) {
445: dss.setColumnDataType("COL" + j,
446: DataStoreBuffer.DATATYPE_INT);
447: } else if (value instanceof Float) {
448: dss.setColumnDataType("COL" + j,
449: DataStoreBuffer.DATATYPE_FLOAT);
450: } else if (value instanceof Long) {
451: dss.setColumnDataType("COL" + j,
452: DataStoreBuffer.DATATYPE_LONG);
453: } else if (value instanceof Double) {
454: dss
455: .setColumnDataType(
456: "COL" + j,
457: DataStoreBuffer.DATATYPE_DOUBLE);
458: } else if (value instanceof Timestamp) {
459: dss
460: .setColumnDataType(
461: "COL" + j,
462: DataStoreBuffer.DATATYPE_DATETIME);
463: } else if (value instanceof java.sql.Date) {
464: dss.setColumnDataType("COL" + j,
465: DataStoreBuffer.DATATYPE_DATE);
466: } else if (value instanceof java.sql.Time) {
467: dss.setColumnDataType("COL" + j,
468: DataStoreBuffer.DATATYPE_TIME);
469: } else if (value instanceof byte[]) {
470: dss
471: .setColumnDataType(
472: "COL" + j,
473: DataStoreBuffer.DATATYPE_BYTEARRAY);
474: }
475: }
476: dss.setAny("COL" + j, value);
477: }
478: }
479: } catch (DataStoreException dse) {
480: dse.printStackTrace();
481: }
482: Thread.yield();
483: }
484: // System.out.println("The number of maps="+_hashmaps.size());
485: }
486:
487: private void sort() {
488:
489: // long lStart=System.currentTimeMillis();
490: createHashMaps();
491: // long lEnd=System.currentTimeMillis();
492: // System.out.println("create Hash maps time take="+(lEnd-lStart));
493: DataStoreBuffer dsKeys = new DataStoreBuffer();
494: Enumeration enumMaps = _hashmaps.keys();
495: while (enumMaps.hasMoreElements()) {
496: Object oKey = enumMaps.nextElement();
497: if (!(oKey instanceof String)
498: || (oKey instanceof String && !oKey.equals(""))) {
499: try {
500: if (dsKeys.getColumnCount() == 0) {
501: if (oKey instanceof String) {
502: dsKeys.addBucket("KEY",
503: DataStoreBuffer.DATATYPE_STRING);
504: } else if (oKey instanceof Short) {
505: dsKeys.addBucket("KEY",
506: DataStoreBuffer.DATATYPE_SHORT);
507: } else if (oKey instanceof Integer) {
508: dsKeys.addBucket("KEY",
509: DataStoreBuffer.DATATYPE_INT);
510: } else if (oKey instanceof Float) {
511: dsKeys.addBucket("KEY",
512: DataStoreBuffer.DATATYPE_FLOAT);
513: } else if (oKey instanceof Long) {
514: dsKeys.addBucket("KEY",
515: DataStoreBuffer.DATATYPE_LONG);
516: } else if (oKey instanceof Double) {
517: dsKeys.addBucket("KEY",
518: DataStoreBuffer.DATATYPE_DOUBLE);
519: } else if (oKey instanceof Timestamp) {
520: dsKeys.addBucket("KEY",
521: DataStoreBuffer.DATATYPE_DATETIME);
522: } else if (oKey instanceof java.sql.Date) {
523: dsKeys.addBucket("KEY",
524: DataStoreBuffer.DATATYPE_DATE);
525: } else if (oKey instanceof java.sql.Time) {
526: dsKeys.addBucket("KEY",
527: DataStoreBuffer.DATATYPE_TIME);
528: } else if (oKey instanceof byte[]) {
529: dsKeys.addBucket("KEY",
530: DataStoreBuffer.DATATYPE_BYTEARRAY);
531: }
532: // System.out.println("KEY type "+dsKeys.getColumnDataType("KEY"));
533: }
534: dsKeys.insertRow();
535: // System.out.println("KEY added "+oKey);
536: dsKeys.setAny("KEY", oKey);
537: } catch (DataStoreException dse) {
538: dse.printStackTrace();
539: }
540: }
541: DataStoreSort dss = (DataStoreSort) _hashmaps.get(oKey);
542: // System.out.println("dss.getRowCount()="+dss.getRowCount());
543: DSQuickSort dqs = null;
544: if (!(oKey instanceof String)
545: || (oKey instanceof String && !oKey.equals(""))) {
546: int[] cols = null;
547: if (_eval != null) {
548: cols = new int[] { 1 };
549: } else if (_objs != null) {
550: cols = new int[_objs.length];
551: for (int i = 0; i < _objs.length; i++)
552: cols[i] = i + 2;
553: } else {
554: cols = new int[_cols.length];
555: for (int i = 0; i < _cols.length; i++)
556: cols[i] = i + 2;
557: }
558: try {
559: if (!areRowsEqual((Vector) dss.getRows().elementAt(
560: 0), cols))
561: if (dss.getRowCount() > 500
562: && _hashmaps.size() != 1) {
563: dss.sort(cols, _dirs);
564: } else
565: dss.sort(cols, _dirs, true);
566: } catch (DataStoreException dse) {
567: dse.printStackTrace();
568: }
569: } else {
570: int[] cols = null;
571: int[] dirs = null;
572: if (_eval == null) {
573: if (_objs != null) {
574: dirs = new int[_objs.length - 1];
575: cols = new int[_objs.length - 1];
576: for (int i = 0; i < _objs.length - 1; i++) {
577: cols[i] = i + 3;
578: dirs[i] = _dirs[i + 1];
579: }
580: } else {
581: dirs = new int[_cols.length - 1];
582: cols = new int[_cols.length - 1];
583: for (int i = 0; i < _cols.length - 1; i++) {
584: cols[i] = i + 3;
585: dirs[i] = _dirs[i + 1];
586: }
587: }
588: try {
589: if (!areRowsEqual((Vector) dss.getRows()
590: .elementAt(0), cols))
591: if (dss.getRowCount() > 500
592: && _hashmaps.size() != 1) {
593: dss.sort(cols, dirs);
594: } else
595: dss.sort(cols, dirs, true);
596: } catch (DataStoreException dse) {
597: dse.printStackTrace();
598: }
599: }
600: }
601: }
602: try {
603: if (dsKeys.getRowCount() > 0)
604: dsKeys.sort(new int[] { 0 }, new int[] { _dirs[0] });
605: // for (int i=0;i<dsKeys.getRowCount();i++) {
606: // System.out.println("Sorted Key "+i+" is "+dsKeys.getAny(i,"KEY"));
607: // }
608: } catch (DataStoreException dse) {
609: dse.printStackTrace();
610: }
611: Vector vList = (Vector) _list.clone();
612: _list.removeAllElements();
613: if (_dirs[0] == DataStoreBuffer.SORT_ASC) {
614: DataStoreSort dssNull = (DataStoreSort) _hashmaps.get("");
615: if (dssNull != null) {
616: try {
617: for (int i = 0; i < dssNull.getRowCount(); i++) {
618: _list.addElement(vList.elementAt(dssNull
619: .getInt(i, ROW)));
620: }
621: } catch (DataStoreException dse) {
622: dse.printStackTrace();
623: }
624: }
625: try {
626: for (int j = 0; j < dsKeys.getRowCount(); j++) {
627: // System.out.println("Key is "+dsKeys.getAny(j,"KEY"));
628: DataStoreSort dssKey = (DataStoreSort) _hashmaps
629: .get(dsKeys.getAny(j, "KEY"));
630: for (int i = 0; i < dssKey.getRowCount(); i++) {
631: // System.out.println("Row No is "+dssKey.getInt(i,ROW));
632: _list.addElement(vList.elementAt(dssKey.getInt(
633: i, ROW)));
634: }
635: Thread.yield();
636: }
637: } catch (DataStoreException dse) {
638: dse.printStackTrace();
639: }
640: } else {
641: try {
642: for (int j = 0; j < dsKeys.getRowCount(); j++) {
643: DataStoreSort dssKey = (DataStoreSort) _hashmaps
644: .get(dsKeys.getAny(j, "KEY"));
645: for (int i = 0; i < dssKey.getRowCount(); i++) {
646: _list.addElement(vList.elementAt(dssKey.getInt(
647: i, ROW)));
648: }
649: Thread.yield();
650: }
651: } catch (DataStoreException dse) {
652: dse.printStackTrace();
653: }
654: DataStoreSort dssNull = (DataStoreSort) _hashmaps.get("");
655: if (dssNull != null) {
656: try {
657: for (int i = 0; i < dssNull.getRowCount(); i++) {
658: _list.addElement(vList.elementAt(dssNull
659: .getInt(i, ROW)));
660: }
661: } catch (DataStoreException dse) {
662: dse.printStackTrace();
663: }
664: }
665: }
666: /* for (int i=0;i<_list.size();i++) {
667: System.out.println("_list["+i+"] col(0) is "+((DSDataRow)_list.elementAt(i)).getData(_cols[0]));
668: }*/
669: }
670:
671: private boolean areRowsEqual(Vector vRows, int[] cols) {
672:
673: DSDataRow key = (DSDataRow) vRows.elementAt(0);
674:
675: for (int i = 1; i < vRows.size(); i++) {
676: if (!((DSDataRow) vRows.elementAt(i)).equalRows(key, cols)) {
677: return false;
678: }
679: }
680: return true;
681: }
682:
683: private int optimalStringHashMapLength(Vector vRows,
684: DataStoreEvaluator eval, Object object, int column) {
685: String sValue = null;
686: DSDataRow key = null;
687: int i = 0;
688: while (sValue == null && i < vRows.size()) {
689: key = (DSDataRow) vRows.elementAt(i);
690: if (eval != null) {
691: try {
692: sValue = (String) eval.evaluateRow(i);
693: } catch (DataStoreException dse) {
694: dse.printStackTrace();
695: }
696: } else if (object != null) {
697: if (object instanceof DataStoreEvaluator) {
698: try {
699: sValue = (String) ((DataStoreEvaluator) object)
700: .evaluateRow(i);
701: } catch (DataStoreException dse) {
702: dse.printStackTrace();
703: }
704: } else
705: sValue = (String) key.getData(((Integer) object)
706: .intValue());
707: } else
708: sValue = (String) key.getData(column);
709: i++;
710: }
711: if (sValue == null)
712: return 1;
713: int iOptSize = sValue.length();
714: outer: for (; i < vRows.size(); i++) {
715: key = (DSDataRow) vRows.elementAt(i);
716: String sValue2 = null;
717: if (eval != null) {
718: try {
719: sValue2 = (String) eval.evaluateRow(i);
720: } catch (DataStoreException dse) {
721: dse.printStackTrace();
722: }
723: } else if (object != null) {
724: if (object instanceof DataStoreEvaluator) {
725: try {
726: sValue2 = (String) ((DataStoreEvaluator) object)
727: .evaluateRow(i);
728: } catch (DataStoreException dse) {
729: dse.printStackTrace();
730: }
731: } else
732: sValue2 = (String) key.getData(((Integer) object)
733: .intValue());
734: } else
735: sValue2 = (String) key.getData(column);
736: if (sValue2 == null)
737: continue;
738: if (sValue2.length() < iOptSize)
739: iOptSize = sValue2.length();
740: while (!sValue.regionMatches(0, sValue2, 0, iOptSize)) {
741: iOptSize--;
742: if (iOptSize == 1)
743: break outer;
744: }
745: }
746: return iOptSize <= 1 ? 2 : iOptSize + 1;
747: }
748:
749: private int optimalNumberHashMapDivider(Vector vRows,
750: DataStoreEvaluator eval, Object object, int column) {
751: double dMin = Double.MAX_VALUE;
752: double dMax = Double.MIN_VALUE;
753: DSDataRow key = null;
754: for (int i = 0; i < vRows.size(); i++) {
755: Number nValue = null;
756: key = (DSDataRow) vRows.elementAt(i);
757: if (eval != null) {
758: try {
759: nValue = (Number) eval.evaluateRow(i);
760: } catch (DataStoreException dse) {
761: dse.printStackTrace();
762: }
763: } else if (object != null) {
764: if (object instanceof DataStoreEvaluator) {
765: try {
766: nValue = (Number) ((DataStoreEvaluator) object)
767: .evaluateRow(i);
768: } catch (DataStoreException dse) {
769: dse.printStackTrace();
770: }
771: } else
772: nValue = (Number) key.getData(((Integer) object)
773: .intValue());
774: } else
775: nValue = (Number) key.getData(column);
776: if (nValue == null)
777: continue;
778: if (nValue.doubleValue() < dMin)
779: dMin = nValue.doubleValue();
780: if (nValue.doubleValue() > dMax)
781: dMax = nValue.doubleValue();
782: }
783: // System.out.println("dMax is "+dMax);
784: // System.out.println("dMin is "+dMin);
785: int iDivider = 100;
786: if (Math.abs(dMax - dMin) != 0 && Math.abs(dMax - dMin) < 20)
787: iDivider = 1;
788: else
789: iDivider = (int) (Math.abs(dMax - dMin) / 20);
790: return iDivider == 0 ? 100 : iDivider;
791: }
792:
793: }
|