Source Code Cross Referenced for ArraySorter.java in  » UML » MetaBoss » com » jamonapi » utils » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » UML » MetaBoss » com.jamonapi.utils 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.jamonapi.utils;
002:
003:        import java.util.*;
004:
005:        /**
006:         * ArraySorter is used to sort 2 dimensional arrays of objects by one of the columns in the array.  Right now this class
007:         * sorts only for the monitor report, but could be made more generic.  Look in the main method to see how this class is used.
008:         *
009:         **/
010:        class ArraySorter {
011:
012:            private static void display(Object[][] array) {
013:
014:                int rows = array.length;
015:                int cols = array[0].length;
016:
017:                for (int i = 0; i < rows; i++) {
018:                    String rowData = "";
019:                    for (int j = 0; j < cols; j++) {
020:                        rowData += array[i][j] + " ";
021:                    }
022:
023:                    System.out.println(rowData);
024:                }
025:            }
026:
027:            public ArraySorter(Object[][] array, int sortCol, String sortOrder) {
028:                this .array = array;
029:                this .sortCol = sortCol;
030:                this .sortOrder = sortOrder;
031:            }
032:
033:            private Object[][] array;
034:            private int sortCol;
035:            private String sortOrder;
036:
037:            private int getRows() {
038:                return array.length;
039:            }
040:
041:            private int getCols() {
042:                return array[0].length;
043:            }
044:
045:            private Object[] getArrayToSort() {
046:
047:                Object[] arrayToSort = new Object[getRows()];
048:                for (int i = 0; i < getRows(); i++) {
049:                    arrayToSort[i] = array[i];
050:                }
051:
052:                return arrayToSort;
053:
054:            }
055:
056:            private ArraySorterEntry[] getArraySorterEntries() {
057:                Object[] arrayToSort = getArrayToSort();
058:
059:                ArraySorterEntry[] arraySorterEntries = new ArraySorterEntry[getRows()];
060:
061:                for (int i = 0; i < getRows(); i++) {
062:                    arraySorterEntries[i] = new ArraySorterEntry(
063:                            arrayToSort[i], (Comparable) array[i][sortCol]);
064:                }
065:
066:                return arraySorterEntries;
067:            }
068:
069:            public Object[][] sort() {
070:
071:                ArraySorterEntry[] arraySorterEntries = getArraySorterEntries();
072:                Arrays.sort(arraySorterEntries);
073:
074:                Object[][] returnArray = new Object[getRows()][getCols()];
075:                for (int i = 0; i < getRows(); i++) {
076:                    returnArray[i] = (Object[]) arraySorterEntries[i]
077:                            .getSortedObject();
078:                }
079:
080:                return returnArray;
081:
082:            }
083:
084:            /**
085:             * inner class ArraySorterEntry
086:             **/
087:            private static final Float zero = new Float(0);
088:
089:            private class ArraySorterEntry implements  Comparable {
090:                private Object arrayValueToSort;
091:                private Comparable valueToSortBy;
092:
093:                public ArraySorterEntry(Object arrayValueToSort,
094:                        Comparable valueToSortBy) {
095:                    this .arrayValueToSort = arrayValueToSort;
096:                    this .valueToSortBy = convert((String) valueToSortBy);
097:                }
098:
099:                // THIS IS UGLY.  FIX THIS BY HAVING EACH COLUMN BE IN ITS NATIVE DATA TYPE NOT Strings.
100:                // THIS CLASS IS SUPPOSED TO BE GENERIC BUT RIGHT NOW IT WILL ONLY WORK FOR SORTING THE MONITOR REPORT
101:                private Comparable convert(String sortByStr) {
102:                    // 3,000 convert to 30000 and return as Float. Note a 0 replaces the comma, so values will be sorted in number
103:                    // order.  The extra 0's for commas will not affect the sorting order adversely.
104:                    // without doing this 2, 1000, 1 would be sorted 1,1000, 2 instead of 1,2,1000
105:
106:                    // sortCol has a 0 based index where 0 is the monitor label first column.
107:                    if (sortCol >= 1 && sortCol <= 9)
108:                        return Float.valueOf(sortByStr.replace(',', '0'));
109:                    else if (sortCol >= 13) { // range format 3,000/22/4,555 returns 3000 or &nbsp if there are no /
110:                        int index = sortByStr.indexOf("/");
111:                        if (index == -1)
112:                            return zero;
113:                        else
114:                            return Float.valueOf(sortByStr.substring(0, index)
115:                                    .replace(',', '0'));
116:                    } else
117:                        return sortByStr;
118:
119:                }
120:
121:                public int compareTo(Object o) {
122:                    ArraySorterEntry sorter = (ArraySorterEntry) o;
123:                    int compare = valueToSortBy.compareTo(sorter.valueToSortBy);
124:                    if (compare == 0 || "asc".equalsIgnoreCase(sortOrder))
125:                        return compare;
126:                    else if ("desc".equalsIgnoreCase(sortOrder))
127:                        return -compare;
128:                    else
129:                        throw new RuntimeException(
130:                                "Programming error: The only valid sort orders are 'asc' and 'desc', but '"
131:                                        + sortOrder + "' was passed");
132:                }
133:
134:                public Object getSortedObject() {
135:                    return arrayValueToSort;
136:                }
137:
138:            }
139:
140:            /******************** end inner class */
141:
142:            /** Test code for ArraySorter **/
143:
144:            public static void main(String[] args) throws Exception {
145:                Object[][] array = { { "7", "8", "9" }, { "1", "2", "3" },
146:                        { "4", "5", "6" }, };
147:                System.out.println("unsorted array");
148:                display(array);
149:
150:                System.out.println("sorted array: asc on col 0");
151:                ArraySorter sorter = new ArraySorter(array, 0, "asc");
152:                display(sorter.sort());
153:
154:                System.out.println("sorted array: desc on col 0");
155:                sorter = new ArraySorter(array, 0, "desc");
156:                display(sorter.sort());
157:
158:                System.out
159:                        .println("sorted array invalid - RuntimeException will be thrown");
160:                sorter = new ArraySorter(array, 0, "invalid");
161:                display(sorter.sort());
162:
163:            }
164:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.