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

fr.adbonnin.cz2128.json.repository.MapRepository Maven / Gradle / Ivy

The newest version!
package fr.adbonnin.cz2128.json.repository;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectReader;
import fr.adbonnin.cz2128.collect.IteratorUtils;
import fr.adbonnin.cz2128.json.JsonException;
import fr.adbonnin.cz2128.json.JsonProvider;
import fr.adbonnin.cz2128.json.iterator.FieldObjectIterator;
import fr.adbonnin.cz2128.json.iterator.FieldValueObjectIterator;

import java.io.IOException;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import static java.util.Objects.requireNonNull;

public abstract class MapRepository extends BaseRepository {

    private final ObjectReader reader;

    protected abstract long saveAll(JsonParser parser, JsonGenerator generator, Map elements) throws IOException;

    protected abstract long deleteAll(JsonParser parser, JsonGenerator generator, Predicate> predicate) throws IOException;

    public MapRepository(JsonProvider provider, ObjectReader reader) {
        super(provider);
        this.reader = requireNonNull(reader);
    }

    public ObjectReader getReader() {
        return reader;
    }

    @Override
    public long count() {
        return withFieldIterator(IteratorUtils::count);
    }

    public long count(Predicate predicate) {
        return withEntryIterator(field -> true, predicate, IteratorUtils::count);
    }

    public long countFields(Predicate predicate) {
        return withEntryIterator(predicate, value -> true, IteratorUtils::count);
    }

    public long countEntries(Predicate> predicate) {
        return withEntryIterator(iterator -> {
            final Iterator> filtered = IteratorUtils.filter(iterator, predicate);
            return IteratorUtils.count(filtered);
        });
    }

    public Optional> findFirst(Predicate predicate) {
        return withEntryIterator(field -> true, predicate, IteratorUtils::first);
    }

    public Optional> findFirstField(Predicate predicate) {
        return withEntryIterator(predicate, value -> true, IteratorUtils::first);
    }

    public Optional> findFirstEntry(Predicate> predicate) {
        return withEntryIterator(iterator -> IteratorUtils.find(iterator, predicate));
    }

    public Map findAll() {
        return findAll(entry -> true);
    }

    public Map findAll(Predicate> predicate) {
        return withEntryIterator(iterator -> {
            final Iterator> filtered = IteratorUtils.filter(iterator, predicate);
            return IteratorUtils.newLinkedHashMap(filtered);
        });
    }

    @Override
    public  R withIterator(Function, ? extends R> function) {
        return withEntryIterator(iterator -> {
            final Iterator valueIterator = IteratorUtils.valueIterator(iterator);
            return function.apply(valueIterator);
        });
    }

    public  R withFieldIterator(Function, R> function) {
        return withParser(parser -> {
            final FieldObjectIterator fieldIterator = new FieldObjectIterator(parser);
            return function.apply(fieldIterator);
        });
    }

    public  R withEntryIterator(Function>, R> function) {
        return withEntryIterator(field -> true, value -> true, function);
    }

    private  R withEntryIterator(Predicate fieldPredicate, Predicate valuePredicate, Function>, R> function) {
        return withParser(parser -> {
            final FieldValueObjectIterator fieldValueIterator = new FieldValueObjectIterator<>(parser, reader, fieldPredicate, valuePredicate);
            return function.apply(fieldValueIterator);
        });
    }

    public  R withEntryStream(Function>, R> function) {
        return withEntryIterator(iterator -> {
            final Spliterator> spliterator = Spliterators.spliteratorUnknownSize(iterator, 0);
            final Stream> stream = StreamSupport.stream(spliterator, false);
            return function.apply(stream);
        });
    }

    public boolean save(String key, T element) {
        return saveAll(Collections.singletonMap(key, element)) != 0;
    }

    public long saveAll(Map elements) {
        return withGenerator((parser, generator) -> {
            try {
                return saveAll(parser, generator, elements);
            }
            catch (IOException e) {
                throw new JsonException(e);
            }
        });
    }

    public boolean delete(String key) {
        return deleteAll(e -> key.equals(e.getKey())) > 0;
    }

    public long deleteAll() {
        return deleteAll(value -> true);
    }

    public long deleteAll(Collection elements) {
        return deleteAll(e -> elements.contains(e.getKey()));
    }

    public long deleteAll(Predicate> predicate) {
        return withGenerator((parser, generator) -> {
            try {
                return deleteAll(parser, generator, predicate);
            }
            catch (IOException e) {
                throw new JsonException(e);
            }
        });
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy