All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ai.grakn.redismock.Utils Maven / Gradle / Ivy

There is a newer version: 0.1.6
Show newest version
package ai.grakn.redismock;

import ai.grakn.redismock.expecptions.InternalException;
import ai.grakn.redismock.expecptions.WrongNumberOfArgumentsException;
import ai.grakn.redismock.expecptions.WrongValueTypeException;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;

/**
 * Created by Xiaolu on 2015/4/21.
 */
public class Utils {

    public static void closeQuietly(Closeable closeable) {
        try {
            closeable.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void checkArgumentsNumberEquals(List args, int expect) throws WrongNumberOfArgumentsException {
        if (args.size() != expect) {
            throw new WrongNumberOfArgumentsException();
        }
    }

    public static void checkArgumentsNumberGreater(List args, int expect) throws WrongNumberOfArgumentsException {
        if (args.size() <= expect) {
            throw new WrongNumberOfArgumentsException();
        }
    }

    public static void checkArgumentsNumberFactor(List args, int factor) throws WrongNumberOfArgumentsException {
        if (args.size() % factor != 0) {
            throw new WrongNumberOfArgumentsException();
        }
    }

    public static Slice serializeObject(Object o){
        try {
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            ObjectOutputStream oo = new ObjectOutputStream(bo);
            oo.writeObject(o);
            Slice encode = new Slice(bo.toByteArray());
            oo.close();
            bo.close();
            return encode;
        } catch (IOException e){
            throw new InternalException(e.getMessage());
        }
    }

    public static  T deserializeObject(Slice data){
        try {
            ByteArrayInputStream bi = new ByteArrayInputStream(data.data());
            ObjectInputStream oi = new ObjectInputStream(bi);
            T ret = (T) oi.readObject();
            oi.close();
            bi.close();
            return ret;
        } catch (IOException | ClassNotFoundException e){
            throw new WrongValueTypeException("WRONGTYPE Key is not a valid HyperLogLog string value.");
        }
    }

    public static long convertToLong(String value){
        try {
            return Long.parseLong(value);
        } catch (NumberFormatException e) {
            throw new WrongValueTypeException("ERR value is not an integer or out of range");
        }
    }

    public static byte convertToByte(String value){
        try {
            byte bit = Byte.parseByte(value);
            if (bit != 0 && bit != 1) {
                throw new NumberFormatException();
            }
            return bit;
        } catch (NumberFormatException e) {
            throw new WrongValueTypeException("ERR bit is not an integer or out of range");
        }
    }

    public static int convertToNonNegativeInteger(String value){
        try {
            int pos = Integer.parseInt(value);
            if(pos < 0) throw new NumberFormatException("Int less than 0");
            return pos;
        } catch (NumberFormatException e) {
            throw new WrongValueTypeException("ERR bit offset is not an integer or out of range");
        }
    }

    public static int convertToInteger(String value){
        try {
            return Integer.parseInt(value);
        } catch (NumberFormatException e) {
            throw new WrongValueTypeException("ERR bit offset is not an integer or out of range");
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy