*** empty log message ***
[IRC.git] / Robust / Transactions / TransactionalIO / src / TransactionalIO / Utilities / Conversions.java
1 package TransactionalIO.Utilities;
2
3 /* =============================================================
4  * SmallSQL : a free Java DBMS library for the Java(tm) platform
5  * =============================================================
6  *
7  * (C) Copyright 2004-2007, by Volker Berlin.
8  *
9  * Project Info:  http://www.smallsql.de/
10  *
11  * This library is free software; you can redistribute it and/or modify it 
12  * under the terms of the GNU Lesser General Public License as published by 
13  * the Free Software Foundation; either version 2.1 of the License, or 
14  * (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful, but 
17  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
18  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
19  * License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
24  * USA.  
25  *
26  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
27  * in the United States and other countries.]
28  *
29  * ---------------
30  * Utils.java
31  * ---------------
32  * Author: Volker Berlin
33  * 
34  */
35
36 import java.io.File;
37 import java.io.FileNotFoundException;
38 import java.io.RandomAccessFile;
39 import java.nio.channels.FileLock;
40 import java.sql.SQLException;
41
42
43 public class Conversions {
44
45         
46         
47         
48         public static int long2int(long value){
49                 if(value > Integer.MAX_VALUE)
50                         return Integer.MAX_VALUE;
51                 if(value < Integer.MIN_VALUE)
52                         return Integer.MIN_VALUE;
53                 return (int)value;
54         }
55         
56         public static long double2long(double value){
57                 if(value > Long.MAX_VALUE)
58                         return Long.MAX_VALUE;
59                 if(value < Long.MIN_VALUE)
60                         return Long.MIN_VALUE;
61                 return (long)value;
62         }
63
64
65
66     public static float bytes2float( byte[] bytes ){
67         return Float.intBitsToFloat( bytes2int( bytes ) );
68     }
69
70     public static double bytes2double( byte[] bytes ){
71         return Double.longBitsToDouble( bytes2long( bytes ) );
72     }
73
74     public static long bytes2long( byte[] bytes ){
75         long result = 0;
76         int length = Math.min( 8, bytes.length);
77         for(int i=0; i<length; i++){
78             result = (result << 8) | (bytes[i] & 0xFF);
79         }
80         return result;
81     }
82
83     public static int bytes2int( byte[] bytes ){
84         int result = 0;
85         int length = Math.min( 4, bytes.length);
86         for(int i=0; i<length; i++){
87             result = (result << 8) | (bytes[i] & 0xFF);
88         }
89         return result;
90     }
91
92     static byte[] double2bytes( double value ){
93         return long2bytes(Double.doubleToLongBits(value));
94     }
95
96     public static byte[] float2bytes( float value ){
97         return int2bytes(Float.floatToIntBits(value));
98     }
99
100     public static byte[] long2bytes( long value ){
101         byte[] result = new byte[8];
102         result[0] = (byte)(value >>> 56);
103         result[1] = (byte)(value >>> 48);
104         result[2] = (byte)(value >>> 40);
105         result[3] = (byte)(value >>> 32);
106         result[4] = (byte)(value >>> 24);
107         result[5] = (byte)(value >>> 16);
108         result[6] = (byte)(value >>> 8);
109         result[7] = (byte)(value);
110         return result;
111     }
112     
113     public static int money2int( long value ) {
114                 if (value < Integer.MIN_VALUE) return Integer.MIN_VALUE;
115                 else if (value > Integer.MAX_VALUE) return Integer.MAX_VALUE;
116                 else return (int) value;
117         }
118
119         public static byte[] int2bytes( int value ){
120                 byte[] result = new byte[4];
121                 result[0] = (byte)(value >>> 24);
122                 result[1] = (byte)(value >>> 16);
123                 result[2] = (byte)(value >>> 8);
124                 result[3] = (byte)(value >>> 0);
125                 return result;
126         }
127
128
129
130    
131
132     private static int hexDigit2int(char digit){
133         if(digit >= '0' && digit <= '9') return digit - '0';
134         digit |= 0x20;
135         if(digit >= 'a' && digit <= 'f') return digit - 'W'; // -'W'  ==  -'a' + 10
136         throw new RuntimeException();
137     }
138
139  
140
141
142     static boolean string2boolean( String val){
143         try{
144             return Double.parseDouble( val ) != 0;
145         }catch(NumberFormatException e){/*ignore it if it not a number*/}
146         return "true".equalsIgnoreCase( val ) || "yes".equalsIgnoreCase( val ) || "t".equalsIgnoreCase( val );
147     }
148         
149         
150         static long doubleToMoney(double value){
151                 if(value < 0)
152                         return (long)(value * 10000 - 0.5);
153                 return (long)(value * 10000 + 0.5);
154         }
155
156     static int indexOf( char value, char[] str, int offset, int length ){
157         value |= 0x20;
158         for(int end = offset+length;offset < end; offset++){
159             if((str[offset] | 0x20) == value) return offset;
160         }
161         return -1;
162     }
163
164     public static int indexOf( int value, int[] list ){
165         int offset = 0;
166         for(int end = list.length; offset < end; offset++){
167             if((list[offset]) == value) return offset;
168         }
169         return -1;
170     }
171
172     public static int indexOf( byte[] value, byte[] list, int offset ){
173         int length = value.length;
174         loop1:
175         for(int end = list.length-length; offset <= end; offset++){
176             for(int i=0; i<length; i++ ){
177                 if(list[offset+i] != value[i]){
178                     continue loop1;
179                 }
180             }
181             return offset;
182         }
183         return -1;
184     }
185
186     public static int compareBytes( byte[] leftBytes, byte[] rightBytes){
187         int length = Math.min( leftBytes.length, rightBytes.length );
188         int comp = 0;
189         for(int i=0; i<length; i++){
190             if(leftBytes[i] != rightBytes[i]){
191                 comp = leftBytes[i] < rightBytes[i] ? -1 : 1;
192                 break;
193             }
194         }
195         if(comp == 0 && leftBytes.length != rightBytes.length){
196             comp = leftBytes.length < rightBytes.length ? -1 : 1;
197         }
198         return comp;
199     }
200         
201
202 }