KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensugar > cube > Util


1 /*
2  * JEFFREE: Java(TM) Embedded Framework FREE
3  * Copyright (C) 2002, 2003 - Opensugar
4  *
5  * The contents of this file are subject to the Jeffree Public License,
6  * as defined by the file JEFFREE_LICENSE.TXT
7  *
8  * You may not use this file except in compliance with the License.
9  * You may obtain a copy of the License on the Objectweb web site
10  * (www.objectweb.org).
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
14  * the specific terms governing rights and limitations under the License.
15  *
16  * The Original Code is JEFFREE, including the java package com.opensugar.cube,
17  * released January 1, 2003.
18  *
19  * The Initial Developer of the Original Code is Opensugar.
20  * The Original Code is Copyright Opensugar.
21  * All Rights Reserved.
22  *
23  * Initial developer(s): Pierre Scokaert (Opensugar)
24  * Contributor(s):
25  */

26  
27 package com.opensugar.cube;
28
29 import java.io.File JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.OutputStream JavaDoc;
32 import java.io.BufferedInputStream JavaDoc;
33 import java.io.BufferedOutputStream JavaDoc;
34 import java.io.FileInputStream JavaDoc;
35 import java.io.FileOutputStream JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.util.Vector JavaDoc;
38 import java.util.Hashtable JavaDoc;
39 import java.util.Enumeration JavaDoc;
40
41 // Utility class that contains utility methods used by other classes
42
public abstract class Util {
43
44    // Check if a parameter value is null, and throw an IllegalArgumentException if it is.
45
protected static void checkNullParameter( Object JavaDoc parameter, String JavaDoc className, String JavaDoc methodName, String JavaDoc parameterName ) {
46       if ( parameter == null ) {
47          throw new IllegalArgumentException JavaDoc( "Cannot call " + className + "." + methodName + " with null " + parameterName );
48       }
49    }
50
51    // Check if an array parameter value is null, and throw an IllegalArgumentException if it is.
52
// Also check the elements of the array, and throw an IllegalArgumentException if one of them is.
53
protected static void checkNullArrayParameter( Object JavaDoc[] parameter, String JavaDoc className, String JavaDoc methodName, String JavaDoc parameterName ) {
54       if ( parameter == null ) {
55          throw new IllegalArgumentException JavaDoc( "Cannot call " + className + "." + methodName + " with null " + parameterName );
56       }
57       for ( int i = 0; i < parameter.length; i++ ) {
58          if ( parameter[ i ] == null ) {
59             throw new IllegalArgumentException JavaDoc( "Cannot call " + className + "." + methodName + " with a " + parameterName + " parameter that contains null values" );
60          }
61       }
62    }
63
64    // Delete a file or directory.
65
// If the argument is a directory, delete the whole file tree below the directory.
66
// Return true if deletion successful, false if not.
67
public static boolean recursiveFileDelete( File JavaDoc file ) {
68       if ( !file.exists() ) {
69          return true;
70       }
71
72       if ( file.isDirectory() ) {
73          String JavaDoc[] listing = file.list();
74          for ( int i = 0; i < listing.length; i++ ) {
75             if ( !recursiveFileDelete( new File JavaDoc( file, listing[ i ] ) ) ) {
76                return false;
77             }
78          }
79       }
80       return file.delete();
81    }
82
83    // Recursively copy a directory
84
public static boolean recursiveFileCopy( File JavaDoc source, File JavaDoc target ) {
85       if ( !source.exists() ) {
86          return false;
87       }
88
89       if ( source.isDirectory() ) {
90          if ( !target.exists() ) {
91             target.mkdirs();
92          }
93          String JavaDoc[] listing = source.list();
94          for ( int i = 0; i < listing.length; i++ ) {
95             if ( !recursiveFileCopy( new File JavaDoc( source, listing[ i ] ), new File JavaDoc( target, listing[ i ] ) ) ) {
96                return false;
97             }
98          }
99       }
100       else {
101          try {
102             transferData( new FileInputStream JavaDoc( source ), new FileOutputStream JavaDoc( target ) );
103          }
104          catch ( IOException JavaDoc e ) {
105             return false;
106          }
107       }
108
109       return true;
110    }
111
112    // Copy data from an input stream into an output stream.
113
// Close both streams when done, even if an exception occurs during data transfer.
114
public static void transferData( InputStream JavaDoc source, OutputStream JavaDoc destination ) throws IOException JavaDoc {
115       BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc( source );
116       BufferedOutputStream JavaDoc bos = new BufferedOutputStream JavaDoc( destination );
117       byte[] buffer = new byte[ 4096 ];
118       int n;
119       IOException JavaDoc exception = null;
120       try {
121          while ( ( n = bis.read( buffer ) ) != -1 ) {
122             bos.write( buffer, 0, n );
123          }
124       }
125       catch( IOException JavaDoc e ) {
126          exception = e;
127       }
128       finally {
129          bis.close();
130          bos.close();
131       }
132
133       if ( exception != null ) {
134          throw exception;
135       }
136    }
137
138    public static Number JavaDoc[] sortNumbers( Number JavaDoc[] unsorted ) {
139       Vector JavaDoc tmp = new Vector JavaDoc();
140       for ( int i = 0; i < unsorted.length; i++ ) {
141          tmp.addElement( unsorted[ i ] );
142       }
143       tmp = sortNumbers( tmp );
144       Number JavaDoc[] sorted = new Number JavaDoc[ unsorted.length ];
145       tmp.copyInto( sorted );
146       return sorted;
147    }
148
149    private static Vector JavaDoc sortNumbers( Vector JavaDoc v ) {
150       Vector JavaDoc sorted = new Vector JavaDoc();
151       int n;
152       while ( v.size() > 0 ) {
153          n = findIndexOfMinNumber( v );
154          sorted.addElement( v.elementAt( n ) );
155          v.removeElementAt( n );
156       }
157       return sorted;
158    }
159
160    private static int findIndexOfMinNumber( Vector JavaDoc v ) {
161       int index = 0;
162       Number JavaDoc minVal;
163       Number JavaDoc val;
164       minVal = (Number JavaDoc)v.elementAt( 0 );
165       for ( int i = 1; i < v.size(); i++ ) {
166          val = (Number JavaDoc)v.elementAt( i );
167          if ( val.doubleValue() < minVal.doubleValue() ) {
168             index = i;
169             minVal = val;
170          }
171       }
172       return index;
173    }
174
175 }
Popular Tags