4646 */
4747public final class EnumDeclaration extends Declaration implements ISimpleDatatypeDeclaration {
4848
49+ private static final int CACHE_SIZE = 4096 ;
50+
4951 /**
5052 * A pair of longs class
5153 *
@@ -111,6 +113,7 @@ public String toString() {
111113 private final Multimap <Pair , String > fEnumMap = LinkedHashMultimap .create ();
112114 private final IntegerDeclaration fContainerType ;
113115 private Pair fLastAdded = new Pair (-1 , -1 );
116+ private @ Nullable String [] fCache = new String [CACHE_SIZE ];
114117
115118 // ------------------------------------------------------------------------
116119 // Constructors
@@ -141,7 +144,7 @@ public EnumDeclaration(IntegerDeclaration containerType) {
141144 * Existing enum declaration table
142145 * @since 2.3
143146 */
144- public EnumDeclaration (IntegerDeclaration containerType , Map <Pair , String > enumTree ){
147+ public EnumDeclaration (IntegerDeclaration containerType , Map <Pair , String > enumTree ) {
145148 fContainerType = containerType ;
146149 enumTree .entrySet ().forEach (entry -> fEnumMap .put (entry .getKey (), entry .getValue ()));
147150 }
@@ -212,6 +215,21 @@ public boolean add(long low, long high, @Nullable String label) {
212215 if (high < low ) {
213216 return false ;
214217 }
218+ if (low < 0 || low >= CACHE_SIZE - 1 || high < 0 || high >= CACHE_SIZE - 1 ) {
219+ fCache = null ;
220+ }
221+ for (int i = (int ) low ; i <= high ; i ++) { // high is inclusive
222+ if (fCache != null ) {
223+ if (fCache [i ] == null ) {
224+ fCache [i ] = label ;
225+ } else {
226+ fCache = null ;
227+ break ;
228+ }
229+ } else {
230+ break ;
231+ }
232+ }
215233 Pair key = new Pair (low , high );
216234 fEnumMap .put (key , label );
217235 fLastAdded = key ;
@@ -241,6 +259,12 @@ public boolean add(@Nullable String label) {
241259 * @return the label of that value, can be null
242260 */
243261 public @ Nullable String query (long value ) {
262+ if (fCache != null ) {
263+ if (value < 0 || value >= CACHE_SIZE ) {
264+ return null ;
265+ }
266+ return fCache [(int ) value ];
267+ }
244268 List <String > strValues = new ArrayList <>();
245269 fEnumMap .forEach ((k , v ) -> {
246270 if (value >= k .getFirst () && value <= k .getSecond ()) {
@@ -332,8 +356,8 @@ public boolean equals(@Nullable Object obj) {
332356 return false ;
333357 }
334358 /*
335- * Must iterate through the entry sets as the comparator used in the enum tree
336- * does not respect the contract
359+ * Must iterate through the entry sets as the comparator used in the
360+ * enum tree does not respect the contract
337361 */
338362 return Iterables .elementsEqual (fEnumMap .entries (), other .fEnumMap .entries ());
339363 }
@@ -354,8 +378,8 @@ public boolean isBinaryEquivalent(@Nullable IDeclaration obj) {
354378 return false ;
355379 }
356380 /*
357- * Must iterate through the entry sets as the comparator used in the enum tree
358- * does not respect the contract
381+ * Must iterate through the entry sets as the comparator used in the
382+ * enum tree does not respect the contract
359383 */
360384 return Iterables .elementsEqual (fEnumMap .entries (), other .fEnumMap .entries ());
361385 }
0 commit comments