00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 package com.realtime.crossfire.jxclient.settings;
00023
00024 import java.util.ArrayList;
00025 import java.util.Iterator;
00026 import java.util.List;
00027 import org.jetbrains.annotations.NotNull;
00028 import org.jetbrains.annotations.Nullable;
00029
00034 public class CommandHistory {
00035
00039 private static final int HISTORY_SIZE = 100;
00040
00044 @NotNull
00045 private final List<String> commands = new ArrayList<String>(HISTORY_SIZE);
00046
00050 private int commandIndex = 0;
00051
00056 public CommandHistory(@NotNull final String ident) {
00057 }
00058
00063 public void addCommand(@NotNull final String command) {
00064 final String trimmedCommand = command.trim();
00065 if (trimmedCommand.length() <= 0) {
00066 return;
00067 }
00068 removeCommand(trimmedCommand);
00069 commands.add(trimmedCommand);
00070 commandIndex = commands.size();
00071 trimToMaxSize();
00072 }
00073
00078 private void trimToMaxSize() {
00079 while (commands.size() > HISTORY_SIZE) {
00080 commands.remove(0);
00081 if (commandIndex > 0) {
00082 commandIndex--;
00083 }
00084 }
00085 }
00086
00091 private void removeCommand(@NotNull final String command) {
00092 final Iterator<String> it = commands.iterator();
00093 while (it.hasNext()) {
00094 final String oldCommand = it.next();
00095 if (oldCommand.equals(command)) {
00096 it.remove();
00097 break;
00098 }
00099 }
00100 }
00101
00107 @Nullable
00108 public String up() {
00109 return 1 <= commandIndex && commandIndex <= commands.size() ? commands.get(--commandIndex) : null;
00110 }
00111
00117 @Nullable
00118 public String down() {
00119 if (commandIndex < commands.size()) {
00120 commandIndex++;
00121 }
00122 return commandIndex < commands.size() ? commands.get(commandIndex) : null;
00123 }
00124
00125 }