Source Code Cross Referenced for Misc.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) 


001:        package com.jamonapi.utils;
002:
003:        /** Difficult to group Utilities **/
004:
005:        import java.util.*;
006:        import java.text.*;
007:
008:        import com.jamonapi.Monitor;
009:
010:        public class Misc extends java.lang.Object {
011:            /** Returns an Objects ClassName minus the package name
012:             *  Sample Call:
013:             *      String className=Misc.getClassName("My Object"); // returns "String"
014:             **/
015:            public static String getClassName(Object object) {
016:                String className = (object == null ? "null" : object.getClass()
017:                        .getName()); // gov.gsa.fss.fim.Misc
018:                return className.substring(className.lastIndexOf(".") + 1); // Misc
019:            }
020:
021:            // Return Exception information as a row (1 dim array)
022:            public static String getExceptionTrace(Throwable exception) {
023:
024:                // each line of the stack trace will be returned in the array.
025:                StackTraceElement elements[] = exception.getStackTrace();
026:                StringBuffer trace = new StringBuffer().append(exception)
027:                        .append("\n");
028:
029:                for (int i = 0; i < elements.length; i++) {
030:                    trace.append(elements[i]).append("\n");
031:                }
032:
033:                return trace.toString();
034:            }
035:
036:            /** Note if the passed object is a Colleciton itself or an Object[] it will be expanded*/
037:            public static void addTo(Collection coll, Object objToAdd) {
038:                if (objToAdd instanceof  Collection)
039:                    coll.addAll((Collection) objToAdd);
040:                else if (objToAdd instanceof  Object[])
041:                    coll.addAll(Arrays.asList((Object[]) objToAdd));
042:                else if (objToAdd instanceof  ToArray)
043:                    coll.addAll(Arrays.asList(((ToArray) objToAdd).toArray()));
044:                else
045:                    coll.add(objToAdd);
046:            }
047:
048:            public static String getAsString(Object obj) {
049:                if (obj == null)
050:                    return null;
051:                else if (obj instanceof  Collection)
052:                    return getCollAsString((Collection) obj);
053:                else if (obj instanceof  Object[])
054:                    return getArrAsString((Object[]) obj);
055:                else if (obj instanceof  ToArray)
056:                    return getArrAsString(((ToArray) obj).toArray());
057:                else if (obj instanceof  Throwable)
058:                    return getExceptionTrace((Throwable) obj);
059:                else
060:                    return obj.toString();
061:            }
062:
063:            private static String getCollAsString(Collection coll) {
064:                int currentElement = 1;
065:                int lastElement = coll.size();
066:                Iterator iter = coll.iterator();
067:                StringBuffer buff = new StringBuffer();
068:
069:                // loop through elements creating a comma delimeted list of the values
070:                while (iter.hasNext()) {
071:                    Object obj = iter.next();
072:                    if (obj instanceof  Throwable)
073:                        obj = getExceptionTrace((Throwable) obj);
074:
075:                    buff.append(obj);
076:                    if (currentElement != lastElement)
077:                        buff.append(",\n");
078:
079:                    currentElement++;
080:                }
081:
082:                return buff.toString();
083:
084:            }
085:
086:            private static String getArrAsString(Object[] arr) {
087:                int lastElement = arr.length - 1;
088:                StringBuffer buff = new StringBuffer();
089:
090:                for (int i = 0; i < arr.length; i++) {
091:                    Object obj = arr[i];
092:                    if (obj instanceof  Throwable)
093:                        obj = getExceptionTrace((Throwable) obj);
094:
095:                    buff.append(obj);
096:                    if (i != lastElement)
097:                        buff.append(",\n");
098:                }
099:
100:                return buff.toString();
101:
102:            }
103:
104:            public static void isObjectString(Object arg) {
105:                if (!(arg instanceof  String))
106:                    throw new IllegalArgumentException(
107:                            "Illegal Argument exception: This object must be of type String.");
108:            }
109:
110:            /** Sort a 2 dimensional array based on 1 columns data in either ascending or descending order.
111:             *  array - Array to be sorted
112:             *  sortCol - column to sort by
113:             *  sortOrder - sort the column in ascending or descending order.  Valid arguments are "asc" and "desc".
114:             **/
115:            public static Object[][] sort(Object[][] array, int sortCol,
116:                    String sortOrder) {
117:                ArraySorter sorter = new ArraySorter(array, sortCol, sortOrder);
118:                return sorter.sort();
119:            }
120:
121:            public static void disp(Object[][] data) {
122:                for (int i = 0; i < data.length; i++) {
123:                    System.out.println();
124:                    System.out.print("row=" + i + ", data=");
125:                    for (int j = 0; j < data[i].length; j++)
126:                        System.out.print(data[i][j] + ",");
127:                }
128:
129:                System.out.println();
130:
131:            }
132:
133:            public static Object[][] allocateArray(int rows, int cols) {
134:                if (rows <= 0 || cols <= 0)
135:                    return null;
136:
137:                Object[][] data = new Object[rows][];
138:                for (int i = 0; i < cols; i++)
139:                    data[i] = new Object[cols];
140:
141:                return data;
142:            }
143:
144:            /** Formats todays date with the passed in date format String.  See the main method for sample Formats */
145:            public static String getFormattedDateNow(String format) {
146:                return getFormattedDate(format, new Date());
147:            }
148:
149:            /** Formats the passed in date with the passed in date format String.  See the main method for 
150:             * sample Formats.  */
151:            public static String getFormattedDate(String format, Date date) {
152:                return new SimpleDateFormat(format).format(date); // 02
153:
154:            }
155:
156:            private static Format monthFormat = new SimpleDateFormat("MM");
157:
158:            /** Get the month out of the passed in Date.  It would return 05 if you passed in the date
159:             *  05/15/2004
160:             */
161:            public static String getMonth(Date date) {
162:                return monthFormat.format(date);
163:            }
164:
165:            /** Return the 2 digit month of todays date     */
166:            public static String getMonth() {
167:                return getMonth(new Date());
168:            }
169:
170:            private static Format dayOfWeekFormat = new SimpleDateFormat("E");
171:
172:            /** Return the day of week from the passed in Date.  For example Mon for Monday. */
173:            public static String getDayOfWeek(Date date) {
174:                return dayOfWeekFormat.format(date);
175:            }
176:
177:            /** Get the day of week for today.  For example Mon for Monday. */
178:            public static String getDayOfWeek() {
179:                return getDayOfWeek(new Date());
180:            }
181:
182:            private static Format shortDateFormat = new SimpleDateFormat(
183:                    "MM/dd/yy");
184:
185:            /** Get the short date for the passed in day.  i.e 05/15/04 */
186:            public static String getShortDate(Date date) {
187:                return shortDateFormat.format(date);
188:            }
189:
190:            /** Get the short date for Today.  i.e 05/15/04 */
191:            public static String getShortDate() {
192:                return getShortDate(new Date());
193:            }
194:
195:            /** Create a case insenstive Tree Map.  More of a standard implementation that I wasn't aware
196:             * of when creating AppMap.  It may only be able to handle Strings as a key.  That isn't clear */
197:            public static Map createCaseInsensitiveMap() {
198:                return new TreeMap(String.CASE_INSENSITIVE_ORDER);
199:            }
200:
201:            public static void main(String[] args) {
202:                Format formatter;
203:                // Get today's date
204:                Date date = new Date();
205:
206:                // examples using shorter functions
207:                System.out.println("month=" + getMonth(date));
208:                System.out.println("dayofweek=" + getDayOfWeek(date));
209:                System.out.println("shortdate=" + getShortDate(date));
210:                System.out.println("formatteddate="
211:                        + getFormattedDate("dd-MMM-yy", date));
212:                System.out.println("month=" + getMonth());
213:                System.out.println("dayofweek=" + getDayOfWeek());
214:                System.out.println("shortdate=" + getShortDate());
215:                // The year
216:                formatter = new SimpleDateFormat("yy"); // 02
217:                System.out.println("yy=" + formatter.format(date));
218:
219:                formatter = new SimpleDateFormat("yyyy"); // 2002
220:                System.out.println("yyyy=" + formatter.format(date));
221:
222:                // The month
223:                formatter = new SimpleDateFormat("M"); // 1
224:                System.out.println("M=" + formatter.format(date));
225:
226:                formatter = new SimpleDateFormat("MM"); // 01
227:                System.out.println("MM=" + formatter.format(date));
228:
229:                formatter = new SimpleDateFormat("MMM"); // Jan
230:                System.out.println("MMM=" + formatter.format(date));
231:
232:                formatter = new SimpleDateFormat("MMMM"); // January
233:                System.out.println("MMMM=" + formatter.format(date));
234:
235:                // The day
236:                formatter = new SimpleDateFormat("d"); // 9
237:                System.out.println("d=" + formatter.format(date));
238:
239:                formatter = new SimpleDateFormat("dd"); // 09
240:                System.out.println("dd=" + formatter.format(date));
241:
242:                // The day in week
243:                formatter = new SimpleDateFormat("E"); // Wed
244:                System.out.println("E=" + formatter.format(date));
245:
246:                formatter = new SimpleDateFormat("EEEE"); // Wednesday
247:                System.out.println("EEEE=" + formatter.format(date));
248:
249:                // Some examples
250:                formatter = new SimpleDateFormat("MM/dd/yy");
251:                System.out.println("MM/dd/yy=" + formatter.format(date));
252:                // 01/09/02
253:
254:                formatter = new SimpleDateFormat("dd-MMM-yy");
255:                System.out.println("dd-MMM-yy=" + formatter.format(date));
256:                // 29-Jan-02
257:
258:                // Examples with date and time; see also
259:                // e316 Formatting the Time Using a Custom Format
260:                formatter = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
261:                System.out.println("yyyy.MM.dd.HH.mm.ss="
262:                        + formatter.format(date));
263:                // 2002.01.29.08.36.33
264:
265:                formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
266:                System.out.println("E, dd MMM yyyy HH:mm:ss Z="
267:                        + formatter.format(date));
268:                // Tue, 09 Jan 2002 22:14:02 -0500
269:
270:                Map m = createCaseInsensitiveMap();
271:                m.put("Steve", "Souza");
272:                m.put("STEVE", "Souza");
273:                System.out.println("Should return 'Souza': " + m.get("StEvE"));
274:                System.out.println("Map=" + m);
275:
276:            }
277:
278:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.