001         package com.croftsoft.core.util;
002    
003         import java.util.Arrays;
004    
005         import com.croftsoft.core.lang.NullArgumentException;
006    
007         /*********************************************************************
008         * Array manipulation for Java 1.2+.
009         *
010         * <p>
011         * Java 1.2 compatible.
012         * </p>
013         *
014         * @see
015         *   ArrayLib
016         *
017         * @version
018         *   2001-05-25
019         * @since
020         *   2001-05-25
021         * @author
022         *   <a href="https://www.croftsoft.com/">David W. Croft</a>
023         *********************************************************************/
024    
025         public final class  ArrayLib2
026         //////////////////////////////////////////////////////////////////////
027         //////////////////////////////////////////////////////////////////////
028         {
029    
030         public static void  main ( String [ ]  args )
031         //////////////////////////////////////////////////////////////////////
032         {
033           System.out.println ( test ( args ) );
034         }
035    
036         public static boolean  test ( String [ ]  args )
037         //////////////////////////////////////////////////////////////////////
038         {
039           try
040           {
041             String [ ]  ARRAY_ABC = new String [ ] { "a", "b", "c" };
042    
043             if ( !Arrays.equals ( ARRAY_ABC,
044               insertSorted ( new String [ ] { "a", "c" }, "b", false ) ) )
045             {
046               return false;
047             }
048    
049             if ( !Arrays.equals ( new String [ ] { "a", "b", "b", "c" },
050               insertSorted ( ARRAY_ABC, "b", true ) ) )
051             {
052               return false;
053             }
054    
055             if ( !Arrays.equals ( ARRAY_ABC,
056               insertSorted ( ARRAY_ABC, "b", false ) ) )
057             {
058               return false;
059             }
060    
061             //
062    
063             int [ ]  ARRAY_123 = new int [ ] { 1, 2, 3 };
064    
065             if ( !Arrays.equals ( ARRAY_123,
066               insertSorted ( new int [ ] { 1, 3 }, 2, false ) ) )
067             {
068               return false;
069             }
070    
071             if ( !Arrays.equals ( new int [ ] { 1, 2, 2, 3 },
072               insertSorted ( ARRAY_123, 2, true ) ) )
073             {
074               return false;
075             }
076    
077             if ( !Arrays.equals ( ARRAY_123,
078               insertSorted ( ARRAY_123, 2, false ) ) )
079             {
080               return false;
081             }
082    
083             return true;
084           }
085           catch ( Exception  ex )
086           {
087             ex.printStackTrace ( );
088    
089             return false;
090           }
091         }
092    
093         //////////////////////////////////////////////////////////////////////
094         //////////////////////////////////////////////////////////////////////
095    
096         /*********************************************************************
097         * Inserts, in sort order, an integer into a presorted integer array.
098         *
099         * <p>
100         * As this function uses Arrays.binarySearch() and ArrayLib.insert(),
101         * it should be more efficient than growing a sorted array by
102         * appending an element and calling Arrays.sort() each time, especially
103         * as the array grows larger.
104         * </p>
105         *
106         * @see
107         *   java.util.Arrays#binarySearch(int[],int)
108         *
109         * @param  sortedIntArray
110         *
111         *   The array argument must be presorted.  If the array was built up
112         *   one element at a time using this method, the array will always be
113         *   in a sorted state.
114         *
115         *   If this parameter is null, a new integer array will be created.
116         *
117         * @param  comparable
118         *
119         *   The int value to be inserted.
120         *
121         * @param  allowDuplicates
122         *
123         *   If false, the original array argument may be returned unchanged.
124         *
125         * @return
126         *
127         *   Returns a new sorted integer array with integer argument inserted.
128         *********************************************************************/
129         public static int [ ]  insertSorted (
130           int [ ]  sortedIntArray,
131           int      i,
132           boolean  allowDuplicates )
133         //////////////////////////////////////////////////////////////////////
134         {
135           if ( sortedIntArray == null )
136           {
137             return new int [ ] { i };
138           }
139    
140           int  index = Arrays.binarySearch ( sortedIntArray, i );
141    
142           if ( index >= 0 )
143           {
144             if ( allowDuplicates )
145             {
146               return ArrayLib.insert ( sortedIntArray, i, index );
147             }
148             else
149             {
150               return sortedIntArray;
151             }
152           }
153    
154           return ArrayLib.insert ( sortedIntArray, i, -index - 1 );
155         }
156    
157         /*********************************************************************
158         * Inserts, in sort order, a Comparable into a presorted array.
159         *
160         * <p>
161         * As this function uses Arrays.binarySearch(), it should be more
162         * efficient than growing a sorted array by appending an element and
163         * calling Arrays.sort() each time, especially as the array grows
164         * larger.
165         * </p>
166         *
167         * @see
168         *   java.util.Arrays#binarySearch(java.lang.Object[],java.lang.Object)
169         *
170         * @param  sortedComparableArray
171         *
172         *   The array argument must be presorted.  If the array was built up
173         *   one element at a time using this method, the array will always be
174         *   in a sorted state.
175         *
176         * @param  comparable
177         *
178         *   The Comparable object to be inserted.
179         *
180         * @param  allowDuplicates
181         *
182         *   If false, the original array argument may be returned unchanged.
183         *
184         * @throws NullArgumentException
185         *
186         *   If sortedComparableArray or comparable is null.
187         *
188         * @return
189         *
190         *   Returns an array with the same component type as the old array.
191         *********************************************************************/
192         public static Comparable [ ]  insertSorted (
193           Comparable [ ]  sortedComparableArray,
194           Comparable      comparable,
195           boolean         allowDuplicates )
196         //////////////////////////////////////////////////////////////////////
197         {
198           NullArgumentException.check ( sortedComparableArray );
199    
200           NullArgumentException.check ( comparable );
201    
202           int  index
203             = Arrays.binarySearch ( sortedComparableArray, comparable );
204    
205           if ( index >= 0 )
206           {
207             if ( allowDuplicates )
208             {
209               return ( Comparable [ ] )
210                 ArrayLib.insert ( sortedComparableArray, comparable, index );
211             }
212             else
213             {
214               return sortedComparableArray;
215             }
216           }
217    
218           return ( Comparable [ ] )
219             ArrayLib.insert ( sortedComparableArray, comparable, -index - 1 );
220         }
221    
222         //////////////////////////////////////////////////////////////////////
223         //////////////////////////////////////////////////////////////////////
224    
225         private  ArrayLib2 ( ) { }
226    
227         //////////////////////////////////////////////////////////////////////
228         //////////////////////////////////////////////////////////////////////
229         }