@Override public boolean addAll(final Collection<? extends T> values) { for (final T value : values) { add(value); } return true; }
/** * Add a new newest entry. * * @param value the value of the entry * @return whether the entry was added */ @Override public boolean add(final T value) { return add(value, false); }
/** * Adds the most recently issued command. * * @param command the most recent command to add to the history */ public void add(final String command) { entries.add(command); position = -1; currentCommand = ""; }
/** * Add a new oldest entry. * <p> * This method helps recreating {@link LastRecentlyUsed} instances given the * entries in the order newest first, oldest last. * </p> * * @param value the value of the entry to add */ public void addToEnd(final T value) { add(value, true); }
/** * Adds the most recently issued command. * * @param command the most recent command to add to the history */ public void add(final String command) { entries.add(command); position = -1; currentCommand = ""; }
/** * Add a new newest entry. * * @param value the value of the entry * @return whether the entry was added */ @Override public boolean add(final T value) { return add(value, false); }
/** * Add a new oldest entry. * <p> * This method helps recreating {@link LastRecentlyUsed} instances given the * entries in the order newest first, oldest last. * </p> * * @param value the value of the entry to add */ public void addToEnd(final T value) { add(value, true); }
@Override public boolean addAll(final Collection<? extends T> values) { for (final T value : values) { add(value); } return true; }
private boolean add(final T value, boolean addAtEnd) { final Integer existing = map.get(value); int insert; if (existing != null) { insert = existing; remove(insert); } else if (map.size() == entries.length / 2) { insert = bottom - 1; remove(insert); } else { insert = value.hashCode() % entries.length; if (insert < 0) insert += entries.length; while (insert < entries.length && entries[insert] != null) insert++; } add(insert, value, addAtEnd); return existing == null; }
private boolean add(final T value, boolean addAtEnd) { final Integer existing = map.get(value); int insert; if (existing != null) { insert = existing; remove(insert); } else if (map.size() == entries.length / 2) { insert = bottom - 1; remove(insert); } else { insert = value.hashCode() % entries.length; if (insert < 0) insert += entries.length; while (insert < entries.length && entries[insert] != null) insert++; } add(insert, value, addAtEnd); return existing == null; }
@Test public void test() { int count = 3; final LastRecentlyUsed<String> lru = new LastRecentlyUsed<>(count); for (int i = 1; i <= count; i++) { lru.add("" + i); } int position = -1; for (int i = 1; i <= count; i++) { position = lru.next(position); assertEquals("" + i, lru.get(position)); } position = lru.next(position); assertEquals(-1, position); for (int i = count; i >= 1; i--) { position = lru.previous(position); assertEquals("" + i, lru.get(position)); } position = lru.previous(position); assertEquals(-1, position); }