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

com.redis.spring.batch.reader.ListToKeyValue Maven / Gradle / Ivy

There is a newer version: 4.0.7
Show newest version
package com.redis.spring.batch.reader;

import java.util.Iterator;
import java.util.List;
import java.util.function.Function;

import com.redis.spring.batch.KeyValue;
import com.redis.spring.batch.util.CodecUtils;

import io.lettuce.core.codec.RedisCodec;

public class ListToKeyValue implements Function> {

    private final Function toStringValueFunction;

    public ListToKeyValue(RedisCodec codec) {
        this.toStringValueFunction = CodecUtils.toStringValueFunction(codec);
    }

    @SuppressWarnings("unchecked")
    @Override
    public KeyValue apply(Object item) {
        if (item == null) {
            return null;
        }
        KeyValue keyValue = new KeyValue<>();
        Iterator iterator = ((List) item).iterator();
        if (iterator.hasNext()) {
            keyValue.setKey((K) iterator.next());
        }
        if (iterator.hasNext()) {
            keyValue.setType(toStringValueFunction.apply((V) iterator.next()));
        }
        if (iterator.hasNext()) {
            keyValue.setTtl((Long) iterator.next());
        }
        if (iterator.hasNext()) {
            Long memUsage = (Long) iterator.next();
            if (memUsage != null && memUsage > 0) {
                keyValue.setMemoryUsage(memUsage);
            }
        }
        if (iterator.hasNext()) {
            keyValue.setValue(iterator.next());
        }
        return keyValue;
    }

}