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

com.regnosys.rosetta.translate.IngestMerger Maven / Gradle / Ivy

There is a newer version: 11.25.1
Show newest version
package com.regnosys.rosetta.translate;

import static com.rosetta.util.CollectionUtils.emptyIfNull;
import static java.util.Objects.requireNonNull;
import static java.util.Optional.ofNullable;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.rosetta.model.lib.RosettaModelObjectBuilder;
import com.rosetta.model.lib.process.AttributeMeta;
import com.rosetta.model.lib.process.BuilderMerger;

/**
 * Implementation of BuilderMerger interface that merges two objects together without pruning or throwing exceptions.
 */
public class IngestMerger implements BuilderMerger {

	private static final Logger LOGGER = LoggerFactory.getLogger(IngestMerger.class);
	
    @Override
    public  void run(T o1, T o2) {
        if(LOGGER.isDebugEnabled()) {
    		LOGGER.debug("Merging [o1={},o2={}]", o1, o2);
    	}
        // merge o2 into o1
    	requireNonNull(o1).merge(requireNonNull(o2), this);
    	
    	if(LOGGER.isDebugEnabled()) {
        	LOGGER.debug("Merged [o1={}]", o1);
    	}
    }

    @Override
    public  void mergeRosetta(T o1, T o2, Consumer o1Setter) {
        if (o1 != null && o2 != null) {
            // if both o1 and o2 are present, then we need to merge
            o1.merge(o2, this);
        } else {
            // if o2 is present and o1 is absent, then we can just overwrite
            ofNullable(o2).ifPresent(o1Setter);
        }
    }

    @Override
    public  void mergeRosetta(List o1, List o2, Function o1GetOrCreateByIndex) {
        // merge list items with matching indexes, e.g. object at list o1 index 0, merged with object at list o2 index 0, and so on..
        // iterate through a copy of the lists to prevent a ConcurrentModificationException.
        int i = 0;
        for (Iterator i2 = emptyIfNull(o2).iterator(); i2.hasNext(); i++) {
            if (i2.hasNext()) {
                o1GetOrCreateByIndex.apply(i).merge(i2.next(), this);
            }
        }
    }

    @Override
    public  void mergeBasic(T o1, T o2, Consumer o1Setter, AttributeMeta... metas) {
        if (o1 != null && o2 != null && !o1.equals(o2)) {
            if (!metaContains(metas, AttributeMeta.GLOBAL_KEY)) {
            	LOGGER.info("Merge clash [o1={},o2={}]", o1, o2);
            }
        } else {
            // if o1 is absent and o2 is present, then we can just overwrite
            ofNullable(o2).ifPresent(o1Setter);
        }
    }

    @Override
    public  void mergeBasic(List o1, List o2, Consumer o1Add) {
        // merge lists
        emptyIfNull(o2).forEach(o1Add);
    }

    private boolean metaContains(AttributeMeta[] metas, AttributeMeta attributeMeta) {
        return Arrays.stream(metas).anyMatch(m -> m == attributeMeta);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy