Java List Merge mergeLists(Object oldValue, Object newValue, Object newValue2)

Here you can find the source of mergeLists(Object oldValue, Object newValue, Object newValue2)

Description

Returns a list which is 3 way merge.

License

Apache License

Parameter

Parameter Description
oldValue old list of values (common ancestor of two new values)
newValue new list of values
newValue2 new list of values

Return

a list which is a 3 way merge.

Declaration

public static List mergeLists(Object oldValue, Object newValue,
        Object newValue2) 

Method Source Code

//package com.java2s;
/*/*from  w  ww.ja v  a  2 s . com*/
 * 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.
 *
 * Copyright (C) 2006-2010 Adele Team/LIG/Grenoble University, France
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Returns a list which is 3 way merge.
     * In case of conflicts, newValue2 is considered as master.
     * 
     * @param oldValue  old list of values (common ancestor of two new values)
     * @param newValue  new list of values
     * @param newValue2 new list of values
     * @return a list which is a 3 way merge.
     */
    public static List mergeLists(Object oldValue, Object newValue,
            Object newValue2) {
        if (!(oldValue instanceof List) || !(newValue instanceof List)
                || !(newValue2 instanceof List))
            throw new IllegalArgumentException(
                    "one of the parameters is not a list.");

        List oldList = (List) oldValue;
        List newList = (List) newValue;
        List newList2 = (List) newValue2;

        List resultList = new ArrayList();
        resultList.addAll(newList2);

        // compute removal in newList and newList2
        List removedInNewList = new ArrayList();
        List removedInNewList2 = new ArrayList();
        for (Object obj : oldList) {
            if (!newList.contains(obj))
                removedInNewList.add(obj);
            if (!newList2.contains(obj))
                removedInNewList2.add(obj);
        }

        // compute addition in newList
        List addedInNewList = new ArrayList();
        for (Object obj : newList) {
            if (!oldList.contains(obj))
                addedInNewList.add(obj);
        }

        // compute addition in newList2
        List addedInNewList2 = new ArrayList();
        for (Object obj : newList2) {
            if (!oldList.contains(obj))
                addedInNewList2.add(obj);
        }

        // compute merge
        for (Object obj : addedInNewList) {
            if (!addedInNewList2.contains(obj)
                    && !removedInNewList2.contains(obj))
                resultList.add(obj);
        }
        for (Object obj : removedInNewList) {
            if (!addedInNewList2.contains(obj)
                    && !removedInNewList2.contains(obj))
                resultList.remove(obj);
        }

        //TODO define a better 3 way merge algo for lists

        return resultList;
    }
}

Related

  1. mergeLists(final T... array)
  2. mergeLists(List baseList, List newItems)
  3. mergeLists(List copyTo, List copyFrom)
  4. mergeLists(List dest, List inserts)
  5. mergeLists(List listA, List listB, List listC)
  6. mergeOperations(List> list1, Set ops2)
  7. mergeOrdered( List base, List elems)
  8. mergepath(List pieces, String separator)
  9. mergerList(List sources, List targets)