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

org.jnosql.diana.redis.key.RedisCollection Maven / Gradle / Ivy

There is a newer version: 0.0.9
Show newest version
/*
 *  Copyright (c) 2017 Otávio Santana and others
 *   All rights reserved. This program and the accompanying materials
 *   are made available under the terms of the Eclipse Public License v1.0
 *   and Apache License v2.0 which accompanies this distribution.
 *   The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 *   and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
 *
 *   You may elect to redistribute this code under either of these licenses.
 *
 *   Contributors:
 *
 *   Otavio Santana
 */

package org.jnosql.diana.redis.key;

import org.jnosql.diana.driver.JsonbSupplier;
import redis.clients.jedis.Jedis;

import javax.json.bind.Jsonb;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

abstract class RedisCollection implements Collection {

    protected static final  Jsonb JSONB = JsonbSupplier.getInstance().get();

    protected final Class clazz;

    protected final String keyWithNameSpace;

    protected final Jedis jedis;

    protected final boolean isString;



    RedisCollection(Jedis jedis, Class clazz, String keyWithNameSpace) {
        this.clazz = clazz;
        this.keyWithNameSpace = keyWithNameSpace;
        this.jedis = jedis;
        this.isString = String.class.equals(clazz);
    }

    @Override
    public boolean addAll(Collection c) {
        Objects.requireNonNull(c);
        for (T bean : c) {
            if (bean != null) {
                add(bean);
            }
        }
        return true;
    }

    @Override
    public int size() {
        return jedis.llen(keyWithNameSpace).intValue();
    }

    @Override
    public boolean isEmpty() {
        return size() == 0;
    }

    @Override
    public boolean contains(Object o) {
        return indexOf(o) != -1;
    }

    @Override
    public Iterator iterator() {
        return toArrayList().iterator();
    }

    @Override
    public Object[] toArray() {
        return toArrayList().toArray();
    }

    @Override
    public  E[] toArray(E[] a) {
        return toArrayList().toArray(a);
    }

    @Override
    public boolean retainAll(Collection c) {
        throw new UnsupportedOperationException("Use add all instead");
    }

    @SuppressWarnings("unchecked")
    @Override
    public boolean containsAll(Collection elements) {
        Objects.requireNonNull(elements);
        boolean containsAll = true;
        for (T element : (Collection) elements) {
            if (!contains(element)) {
                containsAll = false;
            }
        }
        return containsAll;
    }

    @SuppressWarnings("unchecked")
    @Override
    public boolean removeAll(Collection elements) {
        Objects.requireNonNull(elements);
        boolean result = false;
        for (T element : (Collection) elements) {
            if (remove(element)) {
                result = true;
            }
        }
        return result;
    }

    @Override
    public boolean remove(Object o) {
        if (!clazz.isInstance(o)) {
            throw new ClassCastException("The object required is " + clazz.getName());
        }
        int index = indexOf(o);

        if (index == -1) {
            return false;
        } else {
            remove(index);
        }

        return true;
    }

    protected T remove(int index) {
        String value = jedis.lindex(keyWithNameSpace, (long) index);
        if (value != null && !value.isEmpty()) {
            jedis.lrem(keyWithNameSpace, 1, value);
            return serialize(value);
        }
        return null;
    }


    protected int indexOf(Object o) {
        if (!clazz.isInstance(o)) {
            return -1;
        }

        String value = serialize(o);
        for (int index = 0; index < size(); index++) {
            String findedValue = jedis.lindex(keyWithNameSpace, (long) index);
            if (value.equals(findedValue)) {
                return index;
            }
        }
        return -1;
    }


    protected List toArrayList() {
        List list = new ArrayList<>();
        for (int index = 0; index < size(); index++) {
            T element = get(index);
            if (element != null) {
                list.add(element);
            }
        }
        return list;
    }

    protected T get(int index) {
        String value = jedis.lindex(keyWithNameSpace, index);
        if (value == null || value.isEmpty()) {
            return null;
        }
        return serialize(value);
    }


    protected T serialize(String value) {
        if(isString) {
            return (T) value;
        }
        return JSONB.fromJson(value,clazz);
    }

    protected String serialize(Object value) {
        if(value instanceof String) {
            return value.toString();
        }
        return JSONB.toJson(value);
    }


    @Override
    public int hashCode() {
        return Objects.hashCode(keyWithNameSpace);
    }

    @SuppressWarnings("rawtypes")
    @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (RedisCollection.class.isInstance(obj)) {
            RedisCollection otherRedis = RedisCollection.class.cast(obj);
            return Objects.equals(otherRedis.keyWithNameSpace, keyWithNameSpace);
        }
        return false;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy