final Slot toInsert = new Slot(slot); leftNeighbour = currentSlot.getPrevSlot(); rightNeighbour = currentSlot; toInsert.setNextSlot(rightNeighbour); toInsert.setPrevSlot(leftNeighbour); rightNeighbour.setPrevSlot(toInsert); leftNeighbour.setNextSlot(toInsert);
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; }
public void clear() { if (isEmpty()) { return; } // iterate back because by default slots will try to pull new value from next slot // when someone try to remove their value Slot slot = lastSlot; while (slot != null) { slot.setValue(null); slot = slot.getPrevSlot(); } }
public boolean anyInputToTheRight() { if (value != null && !hardcoded()) { return true; } if (nextSlot != null) { return nextSlot.anyInputToTheRight(); } return false; }
@Test public void setValueOffset(){ Slot b = new Slot(Slot.RULES_DEFAULT, null, null); assertEquals(1, b.setValue(' ')); assertEquals(1, b.setValue('2')); Slot c = new Slot(Slot.RULES_DEFAULT, null, null); c.setPrevSlot(b); b.setNextSlot(c); assertEquals(1, c.setValue('3')); Slot a = new Slot(Slot.RULE_INPUT_MOVES_INPUT, 'a', null); b.setPrevSlot(a); a.setNextSlot(b); assertEquals(1, a.setValue('a')); assertEquals(2, a.setValue('3')); Slot a0 = new Slot(Slot.RULE_INPUT_MOVES_INPUT, 'a', null); a.setPrevSlot(a); a0.setNextSlot(a); assertEquals(1, a0.setValue('a')); assertEquals(3, a0.setValue(' ')); }
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; }
private static void linkSlots(@NonNull Slot[] slots, SlotsList list) { list.firstSlot = new Slot(slots[0]); Slot prev = list.firstSlot; if (list.size == 1) { list.lastSlot = list.firstSlot; } // link slots for (int i = 1; i < slots.length; i++) { Slot next = new Slot(slots[i]); prev.setNextSlot(next); next.setPrevSlot(prev); prev = next; if (i == slots.length - 1) { list.lastSlot = next; } } }
int index = 0; while (slot != null) { Character c = slot.getValue(); if (allowDecoration || !slot.hasTag(Slot.TAG_DECORATION)) { boolean anyInputFromHere = slot.anyInputToTheRight(); if (!showHardcodedTail || !slots.checkIsIndex(slot.hardcodedSequenceEndIndex() - 1 + index)) { break; slot = slot.getNextSlot(); index++;
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; }
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; }
public static Slot[] copySlotArray(Slot[] arr) { Slot[] result = new Slot[arr.length]; for (int i = 0; i < arr.length; i++) { result[i] = new Slot(arr[i]); } return result; } }
@Override public int getInitialInputPosition() { int cursorPosition = 0; Slot slot = slots.getSlot(cursorPosition); while (slot != null && slot.getValue() != null) { cursorPosition++; slot = slot.getNextSlot(); } return cursorPosition; }
private int emptySlotsOnTail() { int count = 0; Slot slot = slots.getLastSlot(); while (slot != null && slot.getValue() == null) { count++; slot = slot.getPrevSlot(); } return count; }
/** * 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; }
/** * Inserts slots at the and of the mask and mark newly inserted slot as 'extension' (extended * tail of non-terminated mask). 'Extended' slots will be removed when their values are cleared */ private void extendTail(int count) { if (terminated || count < 1) { return; } while (--count >= 0) { // create a copy of the last slot and make it the last one final Slot inserted = slots.insertSlotAt(slots.size(), slots.getLastSlot()); inserted.setValue(null); inserted.withTags(TAG_EXTENSION); } }
@Override protected Slot slotFromNonUnderscoredChar(char character) { if (!Character.isDigit(character)) { rule = Slot.RULE_INPUT_MOVES_INPUT | Slot.RULE_INPUT_REPLACE; final Slot hardcoded = PredefinedSlots.hardcodedSlot(character); return character == PLUS_SIGN ? hardcoded : hardcoded.withTags(Slot.TAG_DECORATION); } final Slot slot = new Slot(rule, character, SlotValidatorSet.setOf(new SlotValidators.DigitValidator())); rule = Slot.RULE_INPUT_MOVES_INPUT; return slot; } }
final int insertOffset = slotCandidate.setValue(newValue, slotForInputIndex.indexOffset > 0); int hardcodedTailLength = 0; if (slotCandidate != null) { hardcodedTailLength = slotCandidate.hardcodedSequenceEndIndex(); showHardcodedTail = nextSlot == null || !nextSlot.anyInputToTheRight();
@Test public void clear() throws Exception { SlotsList list2 = SlotsList.ofArray(new Slot[]{ PredefinedSlots.any(), PredefinedSlots.any(), PredefinedSlots.any(), PredefinedSlots.digit(), }); for (Slot slot : list2) { slot.setValue('0'); assertNotNull(slot.getValue()); } list2.clear(); for (Slot slot : list2) { assertNull(slot.getValue()); } }
@Override public int findCursorPositionInUnformattedString(int cursorPosition) { if (cursorPosition == 0) { return 0; } else if (cursorPosition < 0 || getSize() < cursorPosition) { throw new IndexOutOfBoundsException(String.format(Locale.getDefault(), "Mask size: %d, passed index: %d", getSize(), cursorPosition)); } Slot slot; if (cursorPosition == getSize()) { slot = slots.getLastSlot(); } else { slot = slots.getSlot(cursorPosition); } do { if (slot.hasTag(Slot.TAG_DECORATION)) { cursorPosition--; } slot = slot.getPrevSlot(); } while (slot != null); return cursorPosition; }