@Override public Slot next() { Slot current = nextSlot; nextSlot = nextSlot.getNextSlot(); return current; }
@Override public int getInitialInputPosition() { int cursorPosition = 0; Slot slot = slots.getSlot(cursorPosition); while (slot != null && slot.getValue() != null) { cursorPosition++; slot = slot.getNextSlot(); } return cursorPosition; }
public Slot getSlot(int index) { if (!checkIsIndex(index)) { return null; } Slot result; if (index < (size >> 1)) { // first half of a list result = firstSlot; for (int i = 0; i < index; i++) { result = result.getNextSlot(); } } else { // second half of a list result = lastSlot; for (int i = size - 1; i > index; i--) { result = result.getPrevSlot(); } } if (result == null) { throw new IllegalStateException("Slot inside the mask should not be null. But it is."); } return result; }
private boolean filledFrom(final Slot initialSlot) { if (initialSlot == null) { throw new IllegalArgumentException("first slot is null"); } Slot nextSlot = initialSlot; do { if (!nextSlot.hasTag(TAG_EXTENSION)) { if (!nextSlot.hardcoded() && nextSlot.getValue() == null) { return false; } } nextSlot = nextSlot.getNextSlot(); } while (nextSlot != null); return true; }
private Character pullValueFromSlot(Slot slot) { if (slot == null) { return null; } Character result = null; if (!slot.hardcoded()) { result = slot.getValue(); if (result != null && !validate(result)) { return null; } slot.removeCurrentValue(); } else if (slot.getNextSlot() != null) { result = pullValueFromSlot(slot.getNextSlot()); } return result; }
/** * Looks for a slot to insert {@code value}. Search moves to the right from the specified one * (including it). While searching it checks whether the're any non-hardcoded slots that cannot * accept pending input if such slots are found it is marked in a resulting object. * * @param slot slot from where to start * @param value value to be inserted to slot * @return wrapper around index offset to the found slot and flag showing did search skip any * non-hardcoded slots */ private SlotIndexOffset validSlotIndexOffset(Slot slot, final char value) { final SlotIndexOffset result = new SlotIndexOffset(); while (slot != null && !slot.canInsertHere(value)) { if (!result.nonHarcodedSlotSkipped && !slot.hardcoded()) { result.nonHarcodedSlotSkipped = true; } slot = slot.getNextSlot(); result.indexOffset++; } return result; }
slot = slot.getNextSlot(); index++;
public Slot removeSlot(final Slot slotToRemove) { if (slotToRemove == null || !contains(slotToRemove)) { return null; } Slot left = slotToRemove.getPrevSlot(); Slot right = slotToRemove.getNextSlot(); if (left != null) { left.setNextSlot(right); } else { firstSlot = right; } if (right != null) { right.setPrevSlot(left); } else { lastSlot = left; } size--; return slotToRemove; }
@Test public void ofArray() throws Exception { assertEquals(5, list.size()); assertEquals(list.getFirstSlot(), first); assertEquals(list.getLastSlot(), last); assertEquals(list.getFirstSlot().getNextSlot(), second); assertEquals(list.getLastSlot().getPrevSlot().getPrevSlot().getPrevSlot(), second); }
@Test public void insertSlotAt() throws Exception { assertEquals(PredefinedSlots.hardcodedSlot('0'), list.insertSlotAt(0, PredefinedSlots.hardcodedSlot('0'))); assertEquals(6, list.size()); assertEquals(list.getFirstSlot(), PredefinedSlots.hardcodedSlot('0')); list.insertSlotAt(6, PredefinedSlots.hardcodedSlot('5')); assertEquals(7, list.size()); assertEquals(list.getLastSlot(), PredefinedSlots.hardcodedSlot('5')); list.insertSlotAt(1, PredefinedSlots.hardcodedSlot('1')); assertEquals(8, list.size()); assertEquals(list.getFirstSlot().getNextSlot(), PredefinedSlots.hardcodedSlot('1')); list.insertSlotAt(7, PredefinedSlots.hardcodedSlot('6')); assertEquals(9, list.size()); assertEquals(list.getLastSlot().getPrevSlot(), PredefinedSlots.hardcodedSlot('6')); }