KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > database > utility > P


1 package com.daffodilwoods.database.utility;
2
3 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator._Iterator;
4
5 import java.util.* ;
6 import com.daffodilwoods.database.resource.*;
7 import java.lang.reflect.*;
8 import java.sql.ResultSet JavaDoc;
9 import java.sql.ResultSetMetaData JavaDoc;
10 import java.sql.SQLException JavaDoc;
11 import java.io.*;
12 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator._Iterator ;
13 import com.daffodilwoods.daffodildb.server.datasystem.interfaces._TableIterator;
14 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*;
15 import com.daffodilwoods.daffodildb.server.sql99.common.ColumnDetails;
16 import com.daffodilwoods.daffodildb.server.sql99.dql.listenerevents.*;
17 import com.daffodilwoods.daffodildb.utils.BufferRange;
18 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator._SelectQueryIterator;
19 import com.daffodilwoods.daffodildb.server.serversystem._Server;
20 public class P {
21   static Stack time = new Stack();
22   static Stack name = new Stack();
23
24   public P() throws DException {
25   }
26
27   public static void p(String JavaDoc s){
28       System.out.print(s);
29   }
30   public static void p(Object JavaDoc s){
31       System.out.print(s);
32   }
33   public static void p(int s) {
34       System.out.print(s);
35   }
36   public static void p(char s) {
37       System.out.print(s);
38   }
39   public static void p(boolean s) {
40       System.out.print(s);
41   }
42
43   public static void pln(String JavaDoc s){
44       System.out.println(s);
45   }
46   public static void pln(Object JavaDoc s){
47       System.out.println(s);
48   }
49   public static void pln(int s) {
50       System.out.println(s);
51   }
52   public static void pln(char s) {
53       System.out.println(s);
54   }
55   public static void pln(boolean s) {
56       System.out.println(s);
57   }
58   public static void time(String JavaDoc s) {
59      System.out.println(s+" Time "+System.currentTimeMillis ());
60   }
61
62   public static void pushTime(String JavaDoc s) {
63     name.push(s);
64     time.push(new Long JavaDoc(System.currentTimeMillis () ) );
65   }
66
67   public static void popTime() {
68     if(name.isEmpty ()){
69       System.out.println("No time is saved ");
70       return;
71     }
72
73     String JavaDoc s = (String JavaDoc)name.pop ();
74     long t = System.currentTimeMillis () - ((Long JavaDoc)time.pop()).longValue();
75     System.out.println("Time taken for [" + s + "] is ["+ t + "]");
76   }
77
78   public static String JavaDoc print(Object JavaDoc array){
79     if(array == null )
80        return "[null]";
81     int length = Array.getLength (array);
82     String JavaDoc line = "";
83     for(int i = 0 ; i < length;i++){
84       Object JavaDoc obj = Array.get(array,i);
85       line += "["+ (check(obj) ? print(obj) : (""+obj)) +"]";
86     }
87     return line;
88   }
89
90   private static boolean check(Object JavaDoc array) {
91       if(array == null)
92         return false;
93       try{
94          Array.getLength (array);
95          return true;
96       }catch(IllegalArgumentException JavaDoc ia){return false;}
97   }
98
99   private static void printLine(Object JavaDoc array ) {
100     if(array == null ){
101        System.out.println("null");
102        return;
103     }
104     int length = Array.getLength (array);
105     String JavaDoc line = "";
106     for(int i = 0 ;i < length;i++){
107       Object JavaDoc obj = Array.get(array,i);
108       line += "["+ (check(obj) ? print(obj) : (""+obj)) +"]" + "\t";
109     }
110     System.out.println(line);
111   }
112
113   public static void showArray(Object JavaDoc array)
114   {
115     System.out.println("Showing array ");
116     printLine(array);
117     System.out.println("Array show complete");
118   }
119
120   public static void show2DArray(Object JavaDoc array){
121      if(array == null ){
122        System.out.println(" null");
123        return;
124     }
125     int length = Array.getLength (array);
126     System.out.println("Showing 2D array ");
127     for(int i = 0 ;i < length;i++)
128       printLine( Array.get(array,i));
129     System.out.println("2D Array show complete");
130   }
131
132   public static int indexOf(Object JavaDoc array,Object JavaDoc value)
133   {
134     int length = Array.getLength (array);
135     for(int i=0; i < length; i++){
136       if(value.equals (Array.get (array,i)) )
137         return i;
138     }
139     return -1;
140   }
141
142   public static int indexOf(Object JavaDoc array,int value)
143   {
144     int length = Array.getLength (array);
145     for(int i=0; i < length; i++)
146       if( value == Array.getInt (array,i) )
147         return i;
148     return -1;
149   }
150
151   public static int indexOf(int[] array,int value)
152   {
153     if ( array == null )
154       return -1;
155     return indexOf(array,array.length,value);
156
157   }
158
159   public static int indexOf(int[][] array,int position,int value)
160   {
161     if ( array == null )
162       return -1;
163     return indexOf(array,array.length,position,value);
164   }
165
166   public static int indexOf(int[][] array,int length,int position, int value)
167   {
168     if (array == null)
169       return -1;
170     for(int i=0;i<length;i++)
171       if( array[i][position] == value)
172         return i;
173     return -1;
174   }
175
176   public static int indexOf(String JavaDoc[][] array,int position,String JavaDoc value)
177   {
178     if ( array == null )
179       return -1;
180     return indexOf(array,array.length,position,value);
181   }
182
183   public static int indexOf(String JavaDoc[][] array,int length,int position, String JavaDoc value)
184   {
185     if (array == null)
186       return -1;
187     for(int i=0;i<length;i++)
188       if( array[i][position].equals(value))
189         return i;
190     return -1;
191   }
192
193   public static int indexOf(int[] array,int position, int value)
194   {
195     if (array == null)
196       return -1;
197     for(int i=0;i<position;i++)
198       if( array[i] == value)
199         return i;
200     return -1;
201   }
202   public static String JavaDoc makeDelimitedIdentifier(String JavaDoc identifier){
203     if( identifier == null ) return null;
204
205     char firstChar = identifier.charAt(0);
206     char lastChar = identifier.charAt(identifier.length()-1);
207     if( firstChar == '"' && lastChar == '"' )
208        return identifier;
209
210     StringBuffer JavaDoc st = new StringBuffer JavaDoc(identifier.length()+2);
211     st.append('"');
212     st.append(identifier);
213     st.append('"');
214
215     return st.toString();
216   }
217
218   public static int indexOfIgnoreCase(String JavaDoc[] array,String JavaDoc value)
219   {
220     for(int i=0;i<array.length;i++)
221       if( array[i].equalsIgnoreCase(value) )
222         return i;
223     return -1;
224   }
225
226   public static int indexOf(String JavaDoc[] array,String JavaDoc value)
227   {
228     for(int i=0;i<array.length;i++)
229       if( array[i].equals(value) )
230         return i;
231     return -1;
232   }
233
234   public static boolean isContains(Object JavaDoc array,Object JavaDoc value)
235   {
236     int length = Array.getLength (array);
237     for(int i=0; i < length; i++)
238       if(value.equals ( Array.get (array,i) ) )
239         return true;
240     return false;
241   }
242
243   public static boolean isContains(Object JavaDoc array,int value)
244   {
245     int length = Array.getLength (array);
246     for(int i=0; i < length; i++)
247       if( value == Array.getInt(array,i) )
248         return true;
249     return false;
250   }
251
252   public static Object JavaDoc getDifference(Object JavaDoc first,Object JavaDoc second)
253   {
254      Object JavaDoc ob = Array.newInstance(first.getClass(),Array.getLength(first));
255      int sLength = Array.getLength(first);
256      for(int i =0 ; i < sLength ; i++)
257      {
258         Object JavaDoc o = Array.get(first,i) ;
259         if( !isContains(second,o))
260           Array.set(ob,i,o);
261      }
262      return ob;
263   }
264
265   public static int compareArrays( Object JavaDoc[] arr1 , Object JavaDoc[] arr2 ){
266       if( arr1 == null )
267         if( arr2 == null )
268             return 0;
269         else
270             return -1;
271       else
272         if( arr2 == null )
273           return 1;
274         else{
275           int compare = 0;
276           for( int i=0 ;compare == 0 && i < arr1.length ; i++ ){
277             compare = arr1[i] == null ? arr2[i] == null ? 0 : -1 :
278                         arr2[i] == null ? 1 : ((Comparable JavaDoc)arr1[i]).compareTo(arr2[i]);
279           }
280           return compare;
281         }
282   }
283
284   public static void showResultSet(ResultSet JavaDoc rs) throws SQLException JavaDoc{
285     ResultSetMetaData JavaDoc metaData = rs.getMetaData();
286     int columnCount = metaData.getColumnCount();
287     Object JavaDoc[] displayColumn = new Object JavaDoc[columnCount];
288     for(int i=1;i <= columnCount;i++)
289       displayColumn[i-1] = metaData.getColumnName(i);
290     System.out.println( Arrays.asList(displayColumn) );
291     while(rs.next()){
292       Object JavaDoc[] columnValues = new Object JavaDoc[columnCount];
293       for(int i=1;i<=columnCount;i++)
294         columnValues[i-1] = rs.getObject(i);
295       System.out.println(Arrays.asList(columnValues) );
296     }
297   }
298
299   public static String JavaDoc getAbsolutePath(String JavaDoc path){
300      File file = new File(path);
301      String JavaDoc absolutePath = file.getAbsolutePath();
302      return absolutePath;
303   }
304
305   public static void showIterator( _Iterator iterator)throws DException{
306
307 int i = 0;
308       if(! iterator.first() ){
309       System.out.println("<<<<<<< NO DATA INSIDE ITERATOR >>>>>>>>>");
310       return;
311    }
312    System.out.println(" <<<<<<< SHOW Iterator start >>>>>> hashcode "+ iterator.hashCode() );
313    do{
314       System.out.println(++i+" THREAD :: "+Thread.currentThread().getName()+"##########"+Arrays.asList((Object JavaDoc[])iterator.getColumnValues()));
315    }while( iterator.next() );
316    System.out.println(" <<<<<<< SHOW iterator OVER >>>>>> hashcode " + iterator.hashCode() );
317
318   }
319
320   public static void showIterator( _SelectQueryIterator iterator)throws DException{
321
322     if(! iterator.first() ){
323       System.out.println("<<<<<<< NO DATA INSIDE ITERATOR >>>>>>>>>");
324       return;
325    }
326    System.out.println(" <<<<<<< SHOW Iterator start >>>>>> hashcode "+ iterator.hashCode() );
327    do{
328       System.out.println(Thread.currentThread().getName()+"##########"+Arrays.asList((Object JavaDoc[])iterator.getObject()));
329    }while( iterator.next() );
330    System.out.println(" <<<<<<< SHOW iterator OVER >>>>>> hashcode " + iterator.hashCode() );
331   }
332
333   public static void showIterator( _IndexIterator iterator)throws DException{
334
335       int cnt = 0;
336       if(! iterator.first() ){
337           System.out.println("<<<<<<< NO DATA INSIDE ITERATOR >>>>>>>>>");
338           return;
339       }
340       System.out.println(" <<<<<<< SHOW Iterator start >>>>>> hashcode "+ iterator.hashCode() );
341       do{
342           System.out.println(++cnt + "##########"+Arrays.asList((Object JavaDoc[])iterator.getColumnValues((int[])null)));
343       }while( iterator.next() );
344       System.out.println(" <<<<<<< SHOW iterator OVER >>>>>> hashcode " + iterator.hashCode() );
345   }
346
347   public static void showIterator( _TableIterator iterator)throws DException{
348
349 if(!iterator.first() ){
350           System.out.println("<<<<<<< NO DATA INSIDE ITERATOR >>>>>>>>>");
351           return;
352       }
353       int cnt = 0;
354       System.out.println(" <<<<<<< SHOW Iterator start >>>>>> hashcode "+ iterator.hashCode() );
355       do{
356           System.out.print(++cnt+ "***");
357           System.out.println( "######### "+ Arrays.asList((Object JavaDoc[])iterator.getColumnValues()));
358       }while( iterator.next() );
359       System.out.println(" <<<<<<< SHOW iterator OVER >>>>>> hashcode " + iterator.hashCode() );
360   }
361
362   public static int getIndexOFColumnDetails( ColumnDetails[] cdArray , ColumnDetails cd ) throws DException {
363       if( cdArray != null && cd != null ){
364           int len = cdArray.length;
365           for (int i = 0; i < len; i++) {
366               if( cdArray[i].getColumn().equalsIgnoreCase(cd.getColumn()) )
367                   return i;
368           }
369       }
370       return -1;
371   }
372
373   public static Properties convertDaffodilDBKeyssToUpperCase(Properties prop) {
374     if (prop == null)
375       return null;
376     Properties changedProperties = new Properties();
377     Set JavaDoc entrySet = prop.entrySet();
378     Iterator iter = entrySet.iterator();
379     while (iter.hasNext()) {
380       Map.Entry entry = (Map.Entry) iter.next();
381       String JavaDoc key = (String JavaDoc) entry.getKey();
382       int daffodilPropertyIndex = P.indexOfIgnoreCase(_Server.DAFFODILDBPROPERTIES, key);
383       changedProperties.put(daffodilPropertyIndex == -1 ? key : key.toUpperCase(),
384                             entry.getValue());
385     }
386     return changedProperties;
387   }
388   public static void showMemory(){
389         Runtime JavaDoc rt = Runtime.getRuntime();
390         System.out.println("Total Memory " + rt.totalMemory());
391         System.out.println("Free Memory " + rt.freeMemory());
392         System.out.println("Used Memory " + (rt.totalMemory()-rt.freeMemory()));
393     }
394
395 }
396
Popular Tags