@SuppressWarnings("unchecked") private void freeze() { if (storage instanceof Map<?,?>) { if (storage != EMPTY) { Object[] arr = compact((Map<CharSequence,Object>)storage); storage = cacheHolder(new Holder(arr)); } } }
protected MapSnapshot(MapSnapshot<V> parent) { storage = EMPTY; assert (parent == null || parent.parent == null || !parent.parent.isEmpty()) : "how grand father could be empty " + parent; // optimization to prevent chaining of empty snapshots while (parent != null && parent.isEmpty()) { parent = parent.parent; } this.parent = parent; if (this.parent != null) { this.parent.freeze(); } }
@Override protected APTMacroMapSnapshot getParent() { return (APTMacroMapSnapshot) super.getParent(); }
public Map<CharSequence, V> getAll() { LinkedList<MapSnapshot> stack = new LinkedList<MapSnapshot>(); MapSnapshot<V> snap = this; int i = 0; while (snap != null) { i += snap.size(); stack.add(snap); snap = snap.parent; } Map<CharSequence, V> out = new HashMap<CharSequence, V>(i); while(!stack.isEmpty()) { snap = stack.removeLast(); for (Object object : snap) { Entry<CharSequence, V> entry = (Entry<CharSequence, V>) object; if (isRemoved(entry.getValue())) { out.remove(entry.getKey()); } else { out.put(entry.getKey(), entry.getValue()); } } } return out; }
public boolean isEmpty() { return size() == 0; }
public final V get(CharSequence key) { assert CharSequences.isCompact(key) : "string can't be here " + key; MapSnapshot currentSnap = this; while (currentSnap != null) { Object value = currentSnap.getImpl(key); if (value != null) { return (V)value; } currentSnap = currentSnap.parent; } return null; }
@Override public String toString() { Map<CharSequence, V> tmpMap = getAll(); StringBuilder retValue = new StringBuilder(); retValue.append("VALUES (sorted ").append(tmpMap.size()).append("):\n"); // NOI18N List<CharSequence> macrosSorted = new ArrayList<CharSequence>(tmpMap.keySet()); Collections.sort(macrosSorted, CharSequences.comparator()); for (CharSequence key : macrosSorted) { Object macro = tmpMap.get(key); assert(macro != null); retValue.append(macro); retValue.append("'\n"); // NOI18N } return retValue.toString(); }
@Override public Iterator<Map.Entry<CharSequence, APTMacro>> iterator() { if (storage instanceof CharSequence) { return new SingleItemIterator((CharSequence) storage, UNDEFINED_MACRO); } else if (storage instanceof APTMacro) { APTMacro value = (APTMacro) storage; return new SingleItemIterator(value.getName().getTextID(), value); } else { return super.iterator(); } }