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

org.swiftboot.sheet.meta.MetaMap Maven / Gradle / Ivy

There is a newer version: 2.4.7
Show newest version
package org.swiftboot.sheet.meta;

import org.apache.commons.collections4.ListUtils;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @author swiftech
 */
public class MetaMap {
    /**
     * mapping from sheet id to meta items in the order added.
     */
    Map> data = new LinkedHashMap<>();

    Map> handlers = new HashMap<>();

    /**
     * to indicate that extract images during parsing Excel sheet, for import only.
     */
    boolean withImages = false;
    Map> imageConverters = new HashMap<>();

    public void setSheetHandler(SheetId id, Consumer handler) {
        handlers.put(id, handler);
    }

    public Consumer getSheetHandler(SheetId id) {
        return handlers.get(id);
    }

    public boolean isWithImages() {
        return withImages;
    }

    public void setWithImages(boolean withImages) {
        this.withImages = withImages;
    }

    public void setImageConverter(SheetId id,Function converter) {
        this.imageConverters.put(id, converter);
    }

    public Function getImageConverter(SheetId id) {
        return this.imageConverters.get(id);
    }

    /**
     * Add item to a sheet.
     *
     * @param id
     * @param item
     */
    public void addItem(SheetId id, MetaItem item) {
        List sub = data.computeIfAbsent(id, k -> new ArrayList<>());
        sub.add(item);
    }

    /**
     * Get all items for a sheet.
     *
     * @param id
     * @return
     */
    public List sheetItems(SheetId id) {
        return data.get(id);
    }

    /**
     * Get all items.
     *
     * @return
     */
    public List allItems() {
        Optional> reduced = data.values().stream().reduce(ListUtils::sum);
        return reduced.orElse(null);
    }

    public boolean isEmpty() {
        return data.isEmpty();
    }

    /**
     * Traverse all sheets
     *
     * @param onSheet
     */
    public void traverse(OnSheet onSheet) {
        for (SheetId sheetId : data.keySet()) {
            onSheet.onSheet(sheetId, data.get(sheetId));
        }
    }

    @FunctionalInterface
    public interface OnSheet {
        void onSheet(SheetId sheetId, List items);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy