public SlotsList(@NonNull SlotsList list) { if (!list.isEmpty()) { Slot previous = null; for (Slot slot : list) { final Slot newSlot = new Slot(slot); if (size == 0) { this.firstSlot = newSlot; } else { previous.setNextSlot(newSlot); newSlot.setPrevSlot(previous); } previous = newSlot; size++; } this.lastSlot = previous; } }
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; } } }
toInsert.setNextSlot(rightNeighbour); toInsert.setPrevSlot(leftNeighbour); leftNeighbour.setNextSlot(toInsert);
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 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(' ')); }
@Test public void invalidValueNotRemovedFromNext() { Slot first = new Slot('0', new SlotValidators.DigitValidator()); Slot second = new Slot('a', new SlotValidators.LetterValidator()); first.setNextSlot(second); second.setPrevSlot(first); first.setValue(null); Assert.assertEquals(new Character('a'), second.getValue()); }
@Test public void validValueCopiedFromNext() { Slot first = new Slot('1', new SlotValidators.DigitValidator()); Slot second = new Slot('2', new SlotValidators.DigitValidator()); first.setNextSlot(second); second.setPrevSlot(first); first.setValue(null); Assert.assertEquals(new Character('2'), first.getValue()); }
@Test public void invalidValueNotCopiedFromNext() { Slot first = new Slot('0', new SlotValidators.DigitValidator()); Slot second = new Slot('a', new SlotValidators.LetterValidator()); first.setNextSlot(second); second.setPrevSlot(first); first.setValue(null); Assert.assertNull(first.getValue()); }
@Test public void validValueRemovedFromNext() { Slot first = new Slot('1', new SlotValidators.DigitValidator()); Slot second = new Slot('2', new SlotValidators.DigitValidator()); first.setNextSlot(second); second.setPrevSlot(first); first.setValue(null); Assert.assertNull(second.getValue()); }