Package com.mobixess.jodb.util

Source Code of com.mobixess.jodb.util.Utils

/*
Copyright (C) 2007  Mobixess Inc. http://www.java-objects-database.com

This file is part of the JODB (Java Objects Database) open source project.

JODB is free software; you can redistribute it and/or modify it under
the terms of version 2 of the GNU General Public License as published
by the Free Software Foundation.

JODB is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package com.mobixess.jodb.util;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.mobixess.jodb.core.io.IRandomAccessDataBuffer;
//TODO move primitive methods to corresponding util class
public class Utils {

    private static boolean _logInit;
    private static Handler _logHandler;
    private static Formatter _logFormatter;
   
    public static void fatalError(Throwable th){
        th.printStackTrace();
        System.exit(-1);
    }


    public static void writePrimitive(Object obj, Field field, DataOutput out) throws IllegalArgumentException, IOException, IllegalAccessException{
        String type = field.getType().getName();
        if(type.equals("byte")){
            out.writeByte(field.getByte(obj) );
        }else if (type.equals("int")){
            out.writeInt(field.getInt(obj) );
        } else if (type.equals("short")){
            out.writeShort(field.getShort(obj) );
        } else if (type.equals("boolean")){
            out.writeBoolean(field.getBoolean(obj) );
        } else if (type.equals("long")){
            out.writeLong(field.getLong(obj) );
        } else if (type.equals("char")){
            out.writeChar(field.getChar(obj) );
        } else if (type.equals("float")){
            out.writeFloat(field.getFloat(obj) );
        } else if (type.equals("double")){
            out.writeDouble(field.getDouble(obj) );
        }
    }
   
    public static void writePrimitiveArray(Object array, Class arrayType, int from, int to, DataOutput out) throws IllegalArgumentException, IOException, IllegalAccessException{
        String type = arrayType.getName();
        if(type.equals("byte")){
            for (int i = from; i < to; i++){
                out.writeByte(Array.getByte(array,i) );
            }
        }else if (type.equals("int")){
            for (int i = from; i < to; i++){
                out.writeInt(Array.getInt(array,i) );
            }
        } else if (type.equals("short")){
            for (int i = from; i < to; i++){
                out.writeShort(Array.getShort(array,i) );
            }
        } else if (type.equals("boolean")){
            for (int i = from; i < to; i++){
                out.writeBoolean(Array.getBoolean(array,i) );
            }
        } else if (type.equals("long")){
            for (int i = from; i < to; i++){
                out.writeLong(Array.getLong(array,i) );
            }
        } else if (type.equals("char")){
            for (int i = from; i < to; i++){
                out.writeChar(Array.getChar(array,i) );
            }
        } else if (type.equals("float")){
            for (int i = from; i < to; i++){
                out.writeFloat(Array.getFloat(array,i) );
            }
        } else if (type.equals("double")){
            for (int i = from; i < to; i++){
                out.writeDouble(Array.getDouble(array,i) );
            }
        }
    }
   
    public static boolean readPrimitiveAndCompare(Object obj, Field field, DataInput in) throws IOException{
        String type = field.getType().getName();
        try {
            if(type.equals("byte")){
                return in.readByte() == field.getByte(obj);
            }else if (type.equals("int")){
                return in.readInt()==field.getInt(obj);
            } else if (type.equals("short")){
                return in.readShort()==field.getShort(obj);
            } else if (type.equals("boolean")){
                return in.readBoolean()==field.getBoolean(obj);
            } else if (type.equals("long")){
                return in.readLong()==field.getLong(obj);
            } else if (type.equals("char")){
                return in.readChar()==field.getChar(obj);
            } else if (type.equals("float")){
                return in.readFloat()==field.getFloat(obj);
            } else if (type.equals("double")){
                return in.readDouble()==field.getDouble(obj);
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            throw new IOException(e.getMessage());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            throw new IOException(e.getMessage());
        }
        throw new IOException("unknown type "+field.getType().getName());
    }
   
    public static Object readPrimitive(String type, DataInput in) throws IOException{
        //TODO rework based on enumerated types
        if(type.equals("byte")){
            return in.readByte();
        }else if (type.equals("int")){
            return in.readInt();
        } else if (type.equals("short")){
            return in.readShort();
        } else if (type.equals("boolean")){
            return in.readBoolean();
        } else if (type.equals("long")){
            return in.readLong();
        } else if (type.equals("char")){
            return in.readChar();
        } else if (type.equals("float")){
            return in.readFloat();
        } else  if (type.equals("double")){
            return in.readDouble();
        }
        throw new IOException("unknown primitive type "+type);
    }
   
    /**
     * Bob Jenkins' hash.
     * See http://burtleburtle.net/bob/hash/doobs.html
     *
     *
     * @return a 32-bit hash number
     * @throws IOException
     */
    public final static int oathash(IRandomAccessDataBuffer data, long len) throws IOException
    {
       int hash;
       long i;
       long offset = data.getCursorOffset();
       len+=offset;
       for(hash = 0, i = offset; i < len; ++i)
       {
          hash += data.readByte() & 0xFF;
          hash += (hash << 10);
          hash ^= (hash >> 6);
       }

       hash += (hash << 3);
       hash ^= (hash >> 11);
       hash += (hash << 15);

       return hash;
    }
   
    public static boolean primitiveEquals(Field field, Object obj, long value) throws IOException{
        String type = field.getType().getName();
        try {
            if(type.equals("byte")){
                return value==field.getByte(obj);
            }else if (type.equals("int")){
                return value==field.getInt(obj);
            } else if (type.equals("short")){
                return value==field.getShort(obj);
            } else if (type.equals("boolean")){
                return (value==0 ? false:true) == field.getBoolean(obj);
            } else if (type.equals("long")){
                return value==field.getLong(obj);
            } else if (type.equals("char")){
                return value==field.getChar(obj);
            } else if (type.equals("float")){
                return value==field.getFloat(obj);
            } else if (type.equals("double")){
                return value==field.getDouble(obj);
            }else{
                throw new IOException("unknown type "+field.getType().getName());
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            throw new IOException(e.getMessage());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            throw new IOException(e.getMessage());
        }
    }
   
    public static int comparePrimitive(Field field, Object obj, ByteBuffer value) throws IOException{
        String type = field.getType().getName();
        try {
            if(type.equals("byte")){
                return field.getByte(obj)-value.get();
            }else if (type.equals("int")){
                return field.getInt(obj) - value.getInt();
            } else if (type.equals("short")){
                return field.getShort(obj) - value.getShort();
            } else if (type.equals("boolean")){
                boolean val1 = field.getBoolean(obj);
                boolean val2 = value.get() != 0;
                return val1 == val2 ? 0 : (val1 ? 1 : -1);
            } else if (type.equals("long")){
                long val1 = field.getLong(obj);
                long val2 = value.getLong();
                return (val1<val2 ? -1 : (val1==val2 ? 0 : 1));
            } else if (type.equals("char")){
                return field.getChar(obj)-value.getChar();
            } else if (type.equals("float")){
                float val1 = field.getFloat(obj);
                float val2 = value.getFloat()
                return Float.compare(val1, val2);
            } else if (type.equals("double")){
                double val1 = field.getDouble(obj);
                double val2 = value.getDouble()
                return Double.compare(val1, val2);
            }else{
                throw new IOException("unknown type "+field.getType().getName());
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            throw new IOException(e.getMessage());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            throw new IOException(e.getMessage());
        }
    }
   

    public static void skipPrimitive(Field field, DataInput in) throws IOException{
        String type = field.getType().getName();
        if(type.equals("byte")){
            in.readByte();
        }else if (type.equals("int")){
            in.readInt();
        } else if (type.equals("short")){
            in.readShort();
        } else if (type.equals("boolean")){
            in.readBoolean();
        } else if (type.equals("long")){
            in.readLong();
        } else if (type.equals("char")){
            in.readChar();
        } else if (type.equals("float")){
            in.readFloat();
        } else if (type.equals("double")){
            in.readDouble();
        }else{
            throw new IOException("unknown type "+field.getType().getName());
        }
    }


    public static Logger getLogger(String name){
        if(!_logInit && _logHandler!=null && _logFormatter != null ){
            Logger main = Logger.getLogger("");
            Handler[] handlers = main.getHandlers();
            for (int i = 0; i < handlers.length; i++) {
                main.removeHandler(handlers[i]);
            }
            synchronized (main) {
                if(!_logInit){
                    main.setLevel(Level.INFO);
                    try {
                        main.addHandler(_logHandler);
                        _logHandler.setFormatter(_logFormatter);
                        _logHandler.setLevel(Level.FINEST);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    _logInit = true;
                }
            }
        }
        return Logger.getLogger(name);
    }
   
    public static void initLogger(Handler handler, Formatter formatter){
        _logHandler = handler;
        _logFormatter = formatter;
    }
}
TOP

Related Classes of com.mobixess.jodb.util.Utils

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.