001 package com.croftsoft.core.util; 002 003 import java.io.Serializable; 004 import java.lang.reflect.Array; 005 import java.util.*; 006 007 import com.croftsoft.core.lang.NullArgumentException; 008 import com.croftsoft.core.lang.Testable; 009 010 /********************************************************************* 011 * Optimized for reading from a stable array. 012 * 013 * @version 014 * 2003-04-14 015 * @since 016 * 2003-04-11 017 * @author 018 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 019 *********************************************************************/ 020 021 public final class StableArrayKeeper 022 implements ArrayKeeper, Serializable, Testable 023 ////////////////////////////////////////////////////////////////////// 024 ////////////////////////////////////////////////////////////////////// 025 { 026 027 private static final long serialVersionUID = 0L; 028 029 // 030 031 private Class baseClass; 032 033 private Object [ ] array; 034 035 // 036 037 private transient Map classToArrayMap; 038 039 ////////////////////////////////////////////////////////////////////// 040 ////////////////////////////////////////////////////////////////////// 041 042 public static void main ( String [ ] args ) 043 ////////////////////////////////////////////////////////////////////// 044 { 045 System.out.println ( test ( args ) ); 046 } 047 048 public static boolean test ( String [ ] args ) 049 ////////////////////////////////////////////////////////////////////// 050 { 051 try 052 { 053 ArrayKeeper arrayKeeper = new StableArrayKeeper ( ); 054 055 arrayKeeper.insert ( "c" ); 056 057 arrayKeeper.insert ( "b" ); 058 059 arrayKeeper.insert ( "a" ); 060 061 ArrayLib.println ( arrayKeeper.getArray ( ) ); 062 } 063 catch ( Exception ex ) 064 { 065 ex.printStackTrace ( ); 066 067 return false; 068 } 069 070 return true; 071 } 072 073 ////////////////////////////////////////////////////////////////////// 074 // constructor methods 075 ////////////////////////////////////////////////////////////////////// 076 077 public StableArrayKeeper ( Object [ ] array ) 078 ////////////////////////////////////////////////////////////////////// 079 { 080 setArray ( array ); 081 } 082 083 public StableArrayKeeper ( ) 084 ////////////////////////////////////////////////////////////////////// 085 { 086 this ( new Object [ 0 ] ); 087 } 088 089 ////////////////////////////////////////////////////////////////////// 090 // accessor methods 091 ////////////////////////////////////////////////////////////////////// 092 093 public Object [ ] getArray ( ) 094 ////////////////////////////////////////////////////////////////////// 095 { 096 return array; 097 } 098 099 public Object [ ] getArray ( Class c ) 100 ////////////////////////////////////////////////////////////////////// 101 { 102 if ( baseClass.equals ( c ) ) 103 { 104 return array; 105 } 106 107 NullArgumentException.check ( c ); 108 109 if ( classToArrayMap == null ) 110 { 111 classToArrayMap = new HashMap ( ); 112 } 113 114 Object [ ] cArray = ( Object [ ] ) classToArrayMap.get ( c ); 115 116 if ( cArray != null ) 117 { 118 return cArray; 119 } 120 121 cArray = ( Object [ ] ) Array.newInstance ( c, 0 ); 122 123 for ( int i = 0; i < array.length; i++ ) 124 { 125 if ( c.isInstance ( array [ i ] ) ) 126 { 127 cArray = ( Object [ ] ) ArrayLib.append ( cArray, array [ i ] ); 128 } 129 } 130 131 classToArrayMap.put ( c, cArray ); 132 133 return cArray; 134 } 135 136 ////////////////////////////////////////////////////////////////////// 137 // mutator methods 138 ////////////////////////////////////////////////////////////////////// 139 140 public void append ( Object o ) 141 ////////////////////////////////////////////////////////////////////// 142 { 143 setArray ( ( Object [ ] ) ArrayLib.append ( array, o ) ); 144 } 145 146 public void insert ( Comparable comparable ) 147 ////////////////////////////////////////////////////////////////////// 148 { 149 append ( comparable ); 150 151 Arrays.sort ( array ); 152 } 153 154 public void remove ( Object o ) 155 ////////////////////////////////////////////////////////////////////// 156 { 157 setArray ( ( Object [ ] ) ArrayLib.remove ( array, o ) ); 158 } 159 160 public void setArray ( Object [ ] array ) 161 ////////////////////////////////////////////////////////////////////// 162 { 163 NullArgumentException.check ( this.array = array ); 164 165 baseClass = array.getClass ( ); 166 167 classToArrayMap = null; 168 } 169 170 ////////////////////////////////////////////////////////////////////// 171 ////////////////////////////////////////////////////////////////////// 172 }