001         package com.croftsoft.core.util;
002    
003         import java.util.*;
004    
005         /*********************************************************************
006         *
007         * Static method library to manipulate Hashtable objects.
008         *
009         * @version
010         *   1998-10-04
011         * @author
012         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
013         *********************************************************************/
014    
015         public class  HashtableLib
016         //////////////////////////////////////////////////////////////////////
017         //////////////////////////////////////////////////////////////////////
018         {
019    
020         private  HashtableLib ( ) { }
021    
022         /*********************************************************************
023         * Creates a Hashtable of just the new and updated elements.
024         * <P>
025         * Returns a Hashtable containing those elements in the new
026         * Hashtable that were not in the old Hashtable plus those elements
027         * in new Hashtable that were in the old Hashtable but with different
028         * values.
029         * <P>
030         * Assumes that the Hashtable value objects have overridden
031         * their equals() method for comparison.
032         *
033         * @return
034         *   Key and value objects returned are shared from the newHashtable.
035         *   Returns null if newHashtable is null.
036         *   Returns a shallow clone of newHashtable if oldHashtable is null.
037         *********************************************************************/
038         public static Hashtable  hashtableDelta (
039           Hashtable  oldHashtable, Hashtable  newHashtable )
040         //////////////////////////////////////////////////////////////////////
041         {
042           if ( newHashtable == null ) return null;
043           if ( oldHashtable == null )
044           {
045             return ( Hashtable ) newHashtable.clone ( );
046           }
047    
048           Hashtable  deltaHashtable = new Hashtable ( );
049    
050           Enumeration  e = newHashtable.keys ( );
051           while ( e.hasMoreElements ( ) )
052           {
053             Object  key = e.nextElement ( );
054             Object  value = oldHashtable.get ( key );
055             if ( ( value == null )
056               || !value.equals ( newHashtable.get ( key ) ) )
057             {
058               deltaHashtable.put ( key, newHashtable.get ( key ) );
059             }
060           }
061    
062           return deltaHashtable;
063         }
064    
065         //////////////////////////////////////////////////////////////////////
066         //////////////////////////////////////////////////////////////////////
067         }