Source Code Cross Referenced for ArrayHelper.java in  » Template-Engine » ostermillerutils » com » Ostermiller » util » 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 » Template Engine » ostermillerutils » com.Ostermiller.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Convenience methods for working with Java arrays.
003:         * Copyright (C) 2005 Stephen Ostermiller
004:         * http://ostermiller.org/contact.pl?regarding=Java+Utilities
005:         *
006:         * This program is free software; you can redistribute it and/or modify
007:         * it under the terms of the GNU General Public License as published by
008:         * the Free Software Foundation; either version 2 of the License, or
009:         * (at your option) any later version.
010:         *
011:         * This program is distributed in the hope that it will be useful,
012:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014:         * GNU General Public License for more details.
015:         *
016:         * See COPYING.TXT for details.
017:         */
018:        package com.Ostermiller.util;
019:
020:        import java.io.IOException;
021:        import java.lang.reflect.Array;
022:
023:        /**
024:         * Convenience methods for working with Java arrays.
025:         * More information about this class is available from <a target="_top" href=
026:         * "http://ostermiller.org/utils/ArrayHelper.html">ostermiller.org</a>.
027:         *
028:         * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
029:         * @since ostermillerutils 1.06.00
030:         */
031:        public final class ArrayHelper {
032:
033:            /**
034:             * Concatenates the given arrays into a single long array.
035:             * The returned array will be of the common super type of the
036:             * two given arrays.
037:             * <p>
038:             * For example it can be called like this:<br>
039:             * <pre>
040:             * String[] arr = (String[])ArrayHelper.cat(new String[]{"one","two"}, new String[]{"three","four"});
041:             * </pre>
042:             *
043:             * @param arr1 first array
044:             * @param arr2 second array
045:             * @return an array whose length is the sum of the given array's lengths and contains all the elements in the given arrays.
046:             * @since ostermillerutils 1.06.00
047:             */
048:            public static Object[] cat(Object[] arr1, Object[] arr2) {
049:                // Use reflection to find the super class of both arrays
050:                Class<?> commonSuperClass = Object.class;
051:                boolean foundcommonSuperClass = false;
052:                for (Class<?> c1 = arr1.getClass().getComponentType(); !foundcommonSuperClass
053:                        && !c1.equals(Object.class); c1 = c1.getSuperclass()) {
054:                    for (Class<?> c2 = arr2.getClass().getComponentType(); !foundcommonSuperClass
055:                            && !c2.equals(Object.class); c2 = c2
056:                            .getSuperclass()) {
057:                        if (c2.equals(c1)) {
058:                            foundcommonSuperClass = true;
059:                            commonSuperClass = c1;
060:                        }
061:                    }
062:                }
063:                // Create a new array of the correct type
064:                Object[] result = (Object[]) Array.newInstance(
065:                        commonSuperClass, arr1.length + arr2.length);
066:                // Copy the two arrays into the large array
067:                System.arraycopy(arr1, 0, result, 0, arr1.length);
068:                System.arraycopy(arr2, 0, result, arr1.length, arr2.length);
069:                return result;
070:            }
071:
072:            /**
073:             * Prints out a comma separated list of all the objects
074:             * in the array on a single line in CSV format.
075:             *
076:             * @param arr Array to print in comma separated value format
077:             * @since ostermillerutils 1.06.00
078:             */
079:            public static void print(Object[] arr) {
080:                try {
081:                    CSVPrinter csvp = new CSVPrinter(System.out);
082:                    for (Object element : arr) {
083:                        csvp.write(element.toString());
084:                    }
085:                    csvp.writeln();
086:                    csvp.flush();
087:                } catch (IOException iox) {
088:                    iox.printStackTrace(System.err);
089:                }
090:            }
091:
092:            /**
093:             * Tests two arrays to see if the arrays are equal.
094:             * Two arrays will be equal only if they are the same length
095:             * and contain objects that are equal in the same order.
096:             *
097:             * @param arr1 first array
098:             * @param arr2 second array
099:             * @return true iff two arguments are equal
100:             * @since ostermillerutils 1.06.00
101:             */
102:            public static boolean equal(Object[] arr1, Object[] arr2) {
103:                if (arr1 == null && arr2 == null)
104:                    return true;
105:                if (arr1 == null || arr2 == null)
106:                    return false;
107:                if (arr1.length != arr2.length)
108:                    return false;
109:                for (int i = 0; i < arr1.length; i++) {
110:                    if (!equalObjects(arr1[i], arr2[i]))
111:                        return false;
112:                }
113:                return true;
114:            }
115:
116:            /**
117:             * Tests if the two objects are equal
118:             *
119:             * @param o1 first object
120:             * @param o2 second object
121:             * @since ostermillerutils 1.06.00
122:             */
123:            private static boolean equalObjects(Object o1, Object o2) {
124:                if (o1 == null && o2 == null)
125:                    return true;
126:                if (o1 == null || o2 == null)
127:                    return false;
128:                return o1.equals(o2);
129:            }
130:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.