| | +package com.whitemagicsoftware.rxm; |
| | + |
| | +import java.util.LinkedList; |
| | + |
| | +import static java.lang.System.out; |
| | + |
| | +/** |
| | + * Provides the ability to determine the index whereat two lists begin |
| | + * to differ in content. Both this list and the list to comapre against |
| | + * must not contain null strings. |
| | + */ |
| | +public class DivergentList extends LinkedList<String> { |
| | + /** |
| | + * Answers the index at which the strings within this list differ from |
| | + * the strings in the given list. |
| | + * |
| | + * @param list The list to compare against. |
| | + * |
| | + * @return -1 if the lists have no common strings. |
| | + */ |
| | + public int diverges( DivergentList list ) { |
| | + int index = -1; |
| | + |
| | + if( valid( list ) && valid( this ) ) { |
| | + while( equals( list, ++index ) ); |
| | + } |
| | + |
| | + return index; |
| | + } |
| | + |
| | + /** |
| | + * Answers whether the element at the given index is the same in both |
| | + * lists. This is not null-safe. |
| | + * |
| | + * @param list The list to compare against this list. |
| | + * @return true The lists have the same string at the given index. |
| | + */ |
| | + private boolean equals( DivergentList list, int index ) { |
| | + return (index < size()) && (index < list.size()) && |
| | + get( index ).equals( list.get( index ) ); |
| | + } |
| | + |
| | + /** |
| | + * Answers whether the given element path contains at least one |
| | + * string. |
| | + * |
| | + * @param list The list that must have at least one string. |
| | + * @return true The list has at least one element. |
| | + */ |
| | + private boolean valid( DivergentList list ) { |
| | + return list != null && list.size() > 0; |
| | + } |
| | + |
| | + /** |
| | + * Test the functionality. |
| | + */ |
| | + public static void main( String args[] ) { |
| | + DivergentList list1 = new DivergentList(); |
| | + list1.addLast( "name" ); |
| | + list1.addLast( "first" ); |
| | + list1.addLast( "middle" ); |
| | + list1.addLast( "last" ); |
| | + list1.addLast( "maiden" ); |
| | + |
| | + DivergentList list2 = new DivergentList(); |
| | + list2.addLast( "name" ); |
| | + list2.addLast( "middle" ); |
| | + list2.addLast( "last" ); |
| | + |
| | + // Prints 1 |
| | + out.println( list2.diverges( list1 ) ); |
| | + |
| | + list1.clear(); |
| | + list1.addLast( "name" ); |
| | + list1.addLast( "middle" ); |
| | + list1.addLast( "last" ); |
| | + |
| | + list2.clear(); |
| | + list2.addLast( "name" ); |
| | + list2.addLast( "middle" ); |
| | + list2.addLast( "last" ); |
| | + list2.addLast( "maiden" ); |
| | + list2.addLast( "honorific" ); |
| | + |
| | + // Prints 3 |
| | + out.println( list2.diverges( list1 ) ); |
| | + |
| | + list1.clear(); |
| | + list2.clear(); |
| | + |
| | + // Prints -1 |
| | + out.println( list1.diverges( list2 ) ); |
| | + |
| | + list1.add( "name" ); |
| | + list2.add( "address" ); |
| | + |
| | + // Prints 0 |
| | + out.println( list1.diverges( list2 ) ); |
| | + |
| | + list2.addFirst( "name" ); |
| | + |
| | + // Prints 1 |
| | + out.println( list1.diverges( list2 ) ); |
| | + } |
| | +} |
| | + |
| | |