Source Code Cross Referenced for JAMonArrayComparator.java in  » Profiler » JAMon » 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 » Profiler » JAMon » com.jamonapi.utils 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


01:        package com.jamonapi.utils; // FormattedDataSet API
02:
03:        import java.util.*;
04:
05:        /** 
06:         * <p>This class allows you to compare Object[] arrays by multiple columns.  It can also compare any Object that implements the ToArray interface.  
07:         * Each column will use the underlying
08:         * Compareable interface in natural or reverse order depending on how it is added, or will use the passed in Comparator.
09:         * </p>
10:         * 
11:         */
12:
13:        /** Note I took this code from fdsapi.com, and would like to eventually merge these 2 projects, so
14:         * this class will eventually be replaced by the one in FDS.
15:         */
16:
17:        public class JAMonArrayComparator extends java.lang.Object implements 
18:                Comparator
19:
20:        {
21:            private List sortCols = new ArrayList();
22:            private static final Comparator defaultComparator = new JAMonComparator();
23:
24:            public JAMonArrayComparator() {
25:            }
26:
27:            /** Sort/compare the passed in col number starting at 0 in natural (true) or reverse (false)
28:             * order based on the columns Compareable interface being called.
29:             * @param sortCol
30:             * @param naturalOrder
31:             */
32:            public JAMonArrayComparator(int sortCol, boolean naturalOrder) {
33:                addCompareCol(sortCol, naturalOrder);
34:            }
35:
36:            /** Compare the passed in col in natural order */
37:            public void addCompareCol(int sortCol) {
38:                sortCols.add(new ArrayElementComparator(sortCol, true));
39:            }
40:
41:            /** Compare the passed in col in natural or reverse order */
42:
43:            public void addCompareCol(int sortCol, boolean naturalOrder) {
44:                sortCols.add(new ArrayElementComparator(sortCol, naturalOrder));
45:            }
46:
47:            /** Compare the passed in col based on the passed in Comparator */
48:
49:            public void addCompareCol(int sortCol, Comparator comparator) {
50:                sortCols.add(new ArrayElementComparator(sortCol, comparator));
51:            }
52:
53:            /**
54:             * Method used by the comparator interface. <br><br>
55:             *   o1 &lt; o2 - returns a negative integer<br>
56:             *   o1==o2 - returns zero<br>
57:             *   o1>o2 - returns a postitive integer<br><br>
58:             *  Iterate through all columns that should be compared (in the proper order) 
59:             *  and call the Comparator for each of the column elements.
60:             */
61:
62:            public int compare(Object o1, Object o2) {
63:                Object[] array1 = null;
64:                Object[] array2 = null;
65:                int compareVal = 0;
66:
67:                // Put data into array format or call the underlying defaultComparator if
68:                // the data isn't tabular
69:                if (o1 instanceof  ToArray && o2 instanceof  ToArray) {
70:                    array1 = ((ToArray) o1).toArray();
71:                    array2 = ((ToArray) o2).toArray();
72:                } else if (o1 instanceof  Object[] && o2 instanceof  Object[]) {
73:                    array1 = (Object[]) o1;
74:                    array2 = (Object[]) o2;
75:                } else {
76:                    return defaultComparator.compare(o1, o2);
77:                }
78:
79:                /** Iterate through the Arrays comparators to compare columns */
80:                Iterator iter = sortCols.iterator();
81:                while (iter.hasNext()) {
82:                    ArrayElementComparator elementComp = (ArrayElementComparator) iter
83:                            .next();
84:                    Object element1 = array1[elementComp.getSortCol()];
85:                    Object element2 = array2[elementComp.getSortCol()];
86:
87:                    compareVal = elementComp.compare(element1, element2);
88:
89:                    if (compareVal != 0) // if one of the elments is not equal we know all we need to know and can return
90:                        break;
91:                }
92:
93:                return compareVal;
94:
95:            }
96:
97:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.