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

org.optaplanner.examples.common.persistence.AbstractJsonSolutionFileIO Maven / Gradle / Ivy

Go to download

OptaPlanner solves planning problems. This lightweight, embeddable planning engine implements powerful and scalable algorithms to optimize business resource scheduling and planning. This module contains the examples which demonstrate how to use it in a normal Java application.

There is a newer version: 9.44.0.Final
Show newest version
package org.optaplanner.examples.common.persistence;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.optaplanner.examples.common.persistence.jackson.AbstractKeyDeserializer;
import org.optaplanner.persistence.jackson.impl.domain.solution.JacksonSolutionFileIO;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 *
 * @see AbstractKeyDeserializer
 */
public class AbstractJsonSolutionFileIO extends JacksonSolutionFileIO {

    public AbstractJsonSolutionFileIO(Class clazz) {
        super(clazz);
    }

    public AbstractJsonSolutionFileIO(Class clazz, ObjectMapper mapper) {
        super(clazz, mapper);
    }

    protected  void deduplicateEntities(Solution_ solution,
            Function> entityCollectionFunction, Function entityIdFunction,
            Function> entityMapGetter,
            BiConsumer> entityMapSetter) {
        var entityCollection = entityCollectionFunction.apply(solution);
        var entitiesById = entityCollection.stream()
                .collect(Collectors.toMap(entityIdFunction, Function.identity()));
        for (Entity_ entity : entityCollection) {
            var originalMap = entityMapGetter.apply(entity);
            if (originalMap.isEmpty()) {
                continue;
            }
            var newMap = new LinkedHashMap(originalMap.size());
            originalMap
                    .forEach((otherEntity, value) -> newMap.put(entitiesById.get(entityIdFunction.apply(otherEntity)), value));
            entityMapSetter.accept(entity, newMap);
        }

    }

    protected  Map deduplicateMap(Map originalMap, Map index,
            Function idFunction) {
        if (originalMap == null || originalMap.isEmpty()) {
            return originalMap;
        }

        Map newMap = new LinkedHashMap<>(originalMap.size());
        originalMap.forEach((key, value) -> newMap.put(index.get(idFunction.apply(key)), value));
        return newMap;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy