Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.Collection;

import java.util.Iterator;
import java.util.List;

import java.util.Set;

public class Main {
    /**
     * Filters all objects in the list against a specified collection. Either removes all items that
     * are found in specified collection or removes all items that are not found in the collection.
     * Uses {@link Iterator#remove()} to remove items from the target list.
     * 
     * @param items collection of items acting as a filter
     * @param iter iterator over the target collection, from which items will be removed
     * @param remove true if <em>removing</em> all items found in the collection or false if
     *           <em>retaining</em> them
     * @return true if the list was modified and something was removed
     */
    // TODO: add javadoc about using this to implement Collection.removeAll/retainAll
    public static boolean filter(Collection<?> items, Iterator<?> iter, boolean remove) {
        boolean modified = false;
        while (iter.hasNext()) {
            if (items.contains(iter.next()) == remove) {
                iter.remove();
                modified = true;
            }
        }
        return modified;
    }

    public static boolean contains(Iterator<?> iteratorToCheck, Object item) {
        while (iteratorToCheck.hasNext()) {
            Object o = iteratorToCheck.next();
            if (o == null ? item == null : o.equals(item)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Implements <em>equals</em> per the contract defined by {@link List#equals(Object)}.
     * 
     * @param list the list
     * @param o an object to compare to the list
     * @return true if {@code o} equals {@code list}
     */
    public static boolean equals(List<?> list, Object o) {
        if (!(o instanceof List)) {
            return false;
        }
        if (list == o) {
            return true;
        }
        List<?> l = (List<?>) o;
        if (l.size() != list.size()) {
            return false;
        }
        return listContentsEquals(list, l);
    }

    /**
     * Implements <em>equals</em> per the contract defined by {@link Set#equals(Object)}.
     * 
     * @param set the set
     * @param o an object to compare to the list
     * @return true if {@code o} equals {@code list}
     */
    public static boolean equals(Set<?> set, Object o) {
        if (!(o instanceof Set)) {
            return false;
        }
        if (set == o) {
            return true;
        }
        Set<?> other = (Set<?>) o;
        if (set.size() != other.size()) {
            return false;
        }
        // The spec for interface Set says a set should never contain itself, but most implementations
        // do not explicitly block this. So the paranoid code below handles this case.
        boolean containsItself = false;
        for (Object element : set) {
            if (element == set || element == other) {
                // don't test using contains(...) since that could cause infinite recursion
                containsItself = true;
            } else {
                if (!other.contains(element)) {
                    return false;
                }
            }
        }
        if (containsItself) {
            // safely check that other also contains itself
            for (Object element : other) {
                if (element == set || element == other) {
                    return true;
                }
            }
            return false;
        }
        return true;
    }

    private static boolean listContentsEquals(Iterable<?> l1, Iterable<?> l2) {
        Iterator<?> iter1 = l1.iterator();
        Iterator<?> iter2 = l2.iterator();
        while (iter1.hasNext()) {
            if (!iter2.hasNext()) {
                return false;
            }
            Object e1 = iter1.next();
            Object e2 = iter2.next();
            if ((e1 == l1 && (e2 == l1 || e2 == l2)) || (e1 == l2 && (e2 == l1 || e2 == l2))) {
                // handle case where list contains itself - don't overflow stack
                continue;
            }
            if (e1 == null ? e2 != null : !e1.equals(e2)) {
                return false;
            }
        }
        if (iter2.hasNext()) {
            return false;
        }
        return true;
    }
}