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

org.apache.cayenne.dbsync.merge.MergerDictionaryDiff Maven / Gradle / Ivy

There is a newer version: 5.0-M1
Show newest version
/*****************************************************************
 *   Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 ****************************************************************/

package org.apache.cayenne.dbsync.merge;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

class MergerDictionaryDiff {

    private List> same = new LinkedList<>();
    private List> missing = new LinkedList<>();

    MergerDictionaryDiff() {
    }

    public List> getSame() {
        return same;
    }

    public List> getMissing() {
        return missing;
    }

    public void addAll(MergerDictionaryDiff other) {
        same.addAll(other.getSame());
        missing.addAll(other.getMissing());
    }

    static class Builder {
        private MergerDictionaryDiff diff;
        private MergerDictionary originalDictionary;
        private MergerDictionary importedDictionary;
        private Set sameNames = new HashSet<>();

        Builder() {
            diff = new MergerDictionaryDiff<>();
        }

        Builder originalDictionary(MergerDictionary dictionary) {
            this.originalDictionary = dictionary;
            return this;
        }

        Builder importedDictionary(MergerDictionary dictionary) {
            this.importedDictionary = dictionary;
            return this;
        }

        MergerDictionaryDiff build() {
            if(originalDictionary == null || importedDictionary == null) {
                throw new IllegalArgumentException("Dictionaries not set");
            }

            originalDictionary.init();
            importedDictionary.init();

            diff.same = buildSame();
            diff.missing = buildMissing();

            return diff;
        }

        private List> buildSame() {
            List> sameEntities = new LinkedList<>();
            for(Map.Entry entry : originalDictionary.getDictionary().entrySet()) {
                String name = entry.getKey();
                T original = entry.getValue();
                T imported = importedDictionary.getByName(name);
                if(imported != null) {
                    MergerDiffPair pair = new MergerDiffPair<>(original, imported);
                    sameEntities.add(pair);
                    sameNames.add(name);
                }
            }

            return sameEntities;
        }

        private List> buildMissing() {
            List> missingEntities = new LinkedList<>();
            addMissingFromDictionary(missingEntities, originalDictionary, true);
            addMissingFromDictionary(missingEntities, importedDictionary, false);
            return missingEntities;
        }

        private void addMissingFromDictionary(List> missingEntities, MergerDictionary dictionary, boolean isOriginal) {
            for(Map.Entry entry : dictionary.getDictionary().entrySet()) {
                if(sameNames.contains(entry.getKey())) {
                    continue;
                }
                MergerDiffPair pair = new MergerDiffPair<>(
                        isOriginal ? entry.getValue() : null,
                        isOriginal ? null : entry.getValue());
                missingEntities.add(pair);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy