Java Collection Merge merge(J just, Collection justs)

Here you can find the source of merge(J just, Collection justs)

Description

Merges a given justification into a given collection of justifications.

License

Apache License

Parameter

Parameter Description
just a parameter
justs a parameter

Return

true if the collection is modified as a result of this operation and false otherwise

Declaration

public static <J extends Set<?>> boolean merge(J just, Collection<J> justs) 

Method Source Code

//package com.java2s;
/*-/*from   w w  w .  ja  v a 2s .  c o  m*/
 * #%L
 * Proof Utility Library
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2014 - 2017 Live Ontologies Project
 * %%
 * Licensed 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.
 * #L%
 */

import java.util.Collection;

import java.util.Iterator;

import java.util.Set;

public class Main {
    /**
     * Merges a given justification into a given collection of justifications.
     * The justification is added to the collection unless its subset is already
     * contained in the collection. Furthermore, all proper supersets of the
     * justification are removed from the collection.
     * 
     * @param just
     * @param justs
     * @return {@code true} if the collection is modified as a result of this
     *         operation and {@code false} otherwise
     */
    public static <J extends Set<?>> boolean merge(J just, Collection<J> justs) {
        int justSize = just.size();
        final Iterator<J> oldJustIter = justs.iterator();
        boolean isASubsetOfOld = false;
        while (oldJustIter.hasNext()) {
            final J oldJust = oldJustIter.next();
            if (justSize < oldJust.size()) {
                if (oldJust.containsAll(just)) {
                    // new justification is smaller
                    oldJustIter.remove();
                    isASubsetOfOld = true;
                }
            } else if (!isASubsetOfOld & just.containsAll(oldJust)) {
                // is a superset of some old justification
                return false;
            }
        }
        // justification survived all tests, it is minimal
        justs.add(just);
        return true;
    }
}

Related

  1. merge(Collection strings, char separator)
  2. merge(Collection... collections)
  3. merge(Collection... collections)
  4. merge(Collection... elems)
  5. merge(final Collection values)
  6. mergeArrayIntoCollection(T[] array, Collection collection)
  7. mergeCollection( final Collection col1, final Collection col2)
  8. mergeCollection(Collection a, Collection b)
  9. mergeCollections(Collection... inCollections)