@Test public void rewindToCommand() throws Exception { CommandBuffer buffer = new CommandBuffer(5); for (int i = 1; i <= 4; i++) { buffer.addCommand(createCmd(i)); if (i <= 3) { buffer.consumeCommand(); } } assertEquals(4, buffer.getBufferedCommandsCount()); buffer.rewindToCommand(2); assertEquals(4, buffer.getBufferedCommandsCount()); assertEquals(3, buffer.consumeCommand().getId()); assertEquals(4, buffer.consumeCommand().getId()); assertNull(buffer.consumeCommand()); }
public boolean rewindToCommand(long commandId) { if (commandId == -1) { if (bufferFlippedAtLeastOnce) { return false; } else { nextConsumable = 0; return true; } } else { for (int i = tail < nextConsumable ? nextConsumable - 1 : nextConsumable + buffer.length - 1; i >= tail; i--) { CMD command = buffer[i % buffer.length]; if (command == null) { return false; } if (command.getId() == commandId) { nextConsumable = i + 1; return true; } } return false; } }
CommandBuffer { public boolean rewindToCommand(long commandId) { if (commandId == -1) { if (bufferFlippedAtLeastOnce) { return false; } else { nextConsumable = 0; return true; } } else { for (int i = tail < nextConsumable ? nextConsumable - 1 : nextConsumable + buffer.length - 1; i >= tail; i--) { CMD command = buffer[i % buffer.length]; if (command == null) { return false; } if (command.getId() == commandId) { nextConsumable = i + 1; return true; } } return false; } } }
CommandBuffer { public boolean rewindToCommand(long commandId) { if (commandId == -1) { if (bufferFlippedAtLeastOnce) { return false; } else { nextConsumable = 0; return true; } } else { for (int i = tail < nextConsumable ? nextConsumable - 1 : nextConsumable + buffer.length - 1; i >= tail; i--) { CMD command = buffer[i % buffer.length]; if (command == null) { return false; } if (command.getId() == commandId) { nextConsumable = i + 1; return true; } } return false; } } CommandBuffer(int capacity); }
CommandBuffer { public boolean rewindToCommand(long commandId) { if (commandId == -1) { if (bufferFlippedAtLeastOnce) { return false; } else { nextConsumable = 0; return true; } } else { for (int i = tail < nextConsumable ? nextConsumable - 1 : nextConsumable + buffer.length - 1; i >= tail; i--) { CMD command = buffer[i % buffer.length]; if (command == null) { return false; } if (command.getId() == commandId) { nextConsumable = i + 1; return true; } } return false; } } CommandBuffer(int capacity); int getBufferedCommandsCount(); int getUnconsumedCommandsCount(); void addCommand(CMD command); CMD consumeCommand(); void purgeTillCommand(int commandIdExclusive); boolean rewindToCommand(long commandId); long getNextCommandId(); void clear(); }
CommandBuffer { public boolean rewindToCommand(long commandId) { if (commandId == -1) { if (bufferFlippedAtLeastOnce) { return false; } else { nextConsumable = 0; return true; } } else { for (int i = tail < nextConsumable ? nextConsumable - 1 : nextConsumable + buffer.length - 1; i >= tail; i--) { CMD command = buffer[i % buffer.length]; if (command == null) { return false; } if (command.getId() == commandId) { nextConsumable = i + 1; return true; } } return false; } } CommandBuffer(int capacity); int getBufferedCommandsCount(); int getUnconsumedCommandsCount(); void addCommand(CMD command); CMD consumeCommand(); void purgeTillCommand(int commandIdExclusive); boolean rewindToCommand(long commandId); long getNextCommandId(); void clear();  }