Java Iterable Item isPrefix(Iterable suspectedPrefix, Iterable container)

Here you can find the source of isPrefix(Iterable suspectedPrefix, Iterable container)

Description

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.

License

Open Source License

Parameter

Parameter Description
suspectedPrefix some iterable whose may be a prefix for container
container the iterable who may or may not start with suspectedPrefix

Return

true iff suspectedPrefix is a prefix of container

Declaration

public static boolean isPrefix(Iterable suspectedPrefix, Iterable container) 

Method Source Code


//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 {
    /**// ww  w.  j  ava 2 s . co  m
     * 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;
    }
}

Related

  1. getFirst(final Iterable iterable)
  2. getFirst(Iterable iterable)
  3. getFirstElement(Object maybeIterable)
  4. getFrom(Iterable iterable, int index)
  5. isIterable(Object obj)
  6. iterableContains(Iterable i, E match)
  7. maxes(Iterable elements)
  8. prefix(final Iterable lines, final String prefix)
  9. print(Iterable coll, String sep)