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

se.kth.iss.ug2.Ug2Arrays Maven / Gradle / Ivy

There is a newer version: 1.0.5
Show newest version
/*
 * MIT License
 *
 * Copyright (c) 2017 Kungliga Tekniska högskolan
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package se.kth.iss.ug2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;

/**
 * Utility methods for String arrays, simulating some of the methods from
 * {@link  java.util.Collection}.
 */
public class Ug2Arrays {

    private Ug2Arrays() {
    } // No instance of this class should ever exist

    /**
     * Checks if a String value exists in a String array.
     *
     * @param a the array containing Strings to check.
     * @param s the String to look for in the array.
     * @return true if the String is found in the array.
     * @throws IllegalArgumentException if the array is null
     */
    public static boolean contains(String[] a, String s) {
        if (a == null) {
            throw new IllegalArgumentException("Array can't be null");
        }
        return Arrays.asList(a).contains(s);
    }

    /**
     * Takes the difference between two arrays, sorting and making the first
     * array unique before taking the difference.
     *
     * @param a the String array to diff against.
     * @param b the String array to diff against array a.
     * @return a new String array containing the sorted and unique difference
     * between a and b.
     */
    public static String[] sortedDistinctDiff(String[] a, String... b) {
        if (b.length == 0) {
            return a;
        }
        if (a.length == 1) {
            if (b.length == 1) {
                if (Objects.equals(a[0], b[0])) {
                    return new String[0];
                } else {
                    return a;
                }
            }
            for (String v : b) {
                if (Objects.equals(a[0], v)) {
                    return new String[0];
                }
            }
            return a;
        }

        List ab = new ArrayList<>();
        for (String az : a) {
            if (!ab.contains(az)) {
                boolean contains = false;
                for (String bz : b) {
                    if (Objects.equals(az, bz)) {
                        contains = true;
                        break;
                    }
                }
                if (!contains) {
                    ab.add(az);
                }
            }
        }
        ab.sort(Comparator.naturalOrder());
        return ab.toArray(new String[ab.size()]);
    }

    /**
     * Removes duplicates from a String array and sorts the result.
     *
     * @param a the array to make unique.
     * @return a new array containing the unique sorted elements of array {@code a
     * }.
     */
    public static String[] sortUnique(String... a) {
        Set s = new TreeSet<>(Arrays.asList(a));
        s.addAll(Arrays.asList(a));
        return s.toArray(new String[s.size()]);

    }

    /**
     * Finds the distinct elements in two arrays that are common for both arrays
     * and sorts the result.
     *
     * @param a the first String array.
     * @param b the second String array.
     * @return a new sorted String array containing the distinct common elements
     * between the two arrays.
     */
    public static String[] sortedDistinctIntersection(String[] a, String... b) {
        Set sa = new TreeSet<>(Arrays.asList(a));
        sa.retainAll(Arrays.asList(b));
        return sa.toArray(new String[sa.size()]);
    }

    /**
     * Creates the union of the distinct elements of two arrays and sorts the
     * result.
     *
     * @param a the first String array.
     * @param b the second String array.
     * @return a new String array containing the distinct union of the elements
     * in array a and array b.
     */
    public static String[] sortedDistinctUnion(String[] a, String... b) {
        Set s = new TreeSet<>(Arrays.asList(a));
        s.addAll(Arrays.asList(b));
        return s.toArray(new String[s.size()]);
    }

    /**
     * Verify that the distinct elements of an array equals the distinct
     * elements of another array.
     *
     * @param a the String array to check.
     * @param b the String array who's elements should exist in {@code a}.
     * @return true if the distinct elements of array {@code a} equals all the
     * distinct elements from array {@code b}, false otherwise.
     */
    public static boolean setEquals(String[] a, String... b) {
        Set sa = new HashSet<>(Arrays.asList(a));
        Set sb = new HashSet<>(Arrays.asList(b));
        return sa.containsAll(sb);
    }

    /**
     * Makes a case insensitive diff between the distinct elements of two arrays
     * and sorts the result.
     *
     * @param a the array to diff against.
     * @param b the array to diff.
     * @return the difference between the distinct elements of array a and b,
     * ignoring case for each element.
     */
    public static String[] caseInsensitiveDistinctDiff(String[] a, String... b) {
        Set as = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
        as.addAll(Arrays.asList(a));
        List bl = Arrays.asList(b);
        as.removeAll(bl);
        return as.toArray(new String[as.size()]);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy