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

com.github.zxl0714.redismock.Utils Maven / Gradle / Ivy

The newest version!
package com.github.zxl0714.redismock;

import com.github.zxl0714.redismock.expecptions.WrongNumberOfArgumentsException;

import java.io.*;
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) {
            // ignore
        }
    }

    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) throws Exception {
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        ObjectOutputStream oo = new ObjectOutputStream(bo);
        oo.writeObject(o);
        Slice encode = new Slice(bo.toByteArray());
        oo.close();
        bo.close();
        return encode;
    }

    public static  T deserializeObject(Slice data) throws Exception {
        ByteArrayInputStream bi = new ByteArrayInputStream(data.data());
        ObjectInputStream oi = new ObjectInputStream(bi);
        T ret = (T) oi.readObject();
        oi.close();
        bi.close();
        return ret;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy