Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
// 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:

import java.util.*;

public class Main {
    /**
     * there's no point in being "type safe" in this method, as the returned value is not typed, and we would like to
     * support any type of iterable comparison.
     *
     * @param suspectedPrefix some iterable whose may be a prefix for container
     * @param container the iterable who may or may not start with suspectedPrefix
     * @return true iff suspectedPrefix is a prefix of container
     *
     */
    public static boolean isPrefix(Iterable suspectedPrefix, Iterable container) {

        final Iterator spi = suspectedPrefix.iterator();
        final Iterator ci = container.iterator();

        //the function consumes both iterators from left to right
        while (true) {

            //if we finished consuming the entire prefix - great!
            if (!spi.hasNext()) {
                return true;
            }

            //if prefix hasn't been consumed entirely, but the collection has - bad!
            if (!ci.hasNext()) {
                return false;
            }

            //if both still have not been completely consumed, but there's a difference - bad!
            if (areDifferent(spi.next(), ci.next())) {
                return false;
            }
        }
    }

    /**
     *
     * method that determines if any of the inputs is not .equal()
     * the method handles null gracefully: if all elements are null, they are considered equal.
     *
     * the function assumes that equality is transitive, and doesn't all pair permutations.
     * it also assumes that no object .equals(null)
     *
     * @param elements to check if different
     * @param <T> type of the elements
     * @return false if all elements are equal or all elements are null. base cases of empty and single element are
     * also considered "equal" in an empty fashion.
     */
    public static <T> boolean areDifferent(T... elements) {

        return !areEqual(elements);
    }

    /**
     *
     * method that determines if all inputs are .equal().
     * the method handles null gracefully: if all elements are null, they are considered equal.
     *
     * the function assumes that equality is transitive, and doesn't all pair permutations.
     * it also assumes that no object .equals(null)
     *
     * @param elements to check if equal
     * @param <T> type of the elements
     * @return true if all elements are equal or all elements are null. base cases of empty and single element are
     * also considered "equal" in an empty fashion.
     */
    public static <T> boolean areEqual(T... elements) {

        //base cases, trivially "equal"
        if (elements.length < 2) {
            return true;
        }

        T first = elements[0];
        for (int i = 1; i < elements.length; i++) {
            T current = elements[i];

            //if both are null, then ok, but if only one is... bad.
            if (first == null) {
                if (current != null) {
                    return false;
                }
            }

            //if not null then safe to use equals method. assumes that it handles null correctly.
            else {
                if (!first.equals(current)) {
                    return false;
                }
            }

        }

        return true;
    }
}