Crossfire JXClient, Trunk  R20561
CommandHistory.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.settings;
23 
24 import java.util.ArrayList;
25 import java.util.Iterator;
26 import java.util.List;
27 import org.jetbrains.annotations.NotNull;
28 import org.jetbrains.annotations.Nullable;
29 
34 public class CommandHistory {
35 
39  private static final int HISTORY_SIZE = 100;
40 
44  @NotNull
45  private final List<String> commands = new ArrayList<>(HISTORY_SIZE);
46 
50  private int commandIndex;
51 
56  public CommandHistory(@NotNull final String ident) {
57  }
58 
63  public void addCommand(@NotNull final String command) {
64  final String trimmedCommand = command.trim();
65  if (trimmedCommand.length() <= 0) {
66  return;
67  }
68  removeCommand(trimmedCommand);
69  commands.add(trimmedCommand);
70  commandIndex = commands.size();
71  trimToMaxSize();
72  }
73 
78  private void trimToMaxSize() {
79  while (commands.size() > HISTORY_SIZE) {
80  commands.remove(0);
81  if (commandIndex > 0) {
82  commandIndex--;
83  }
84  }
85  }
86 
91  private void removeCommand(@NotNull final String command) {
92  final Iterator<String> it = commands.iterator();
93  while (it.hasNext()) {
94  final String oldCommand = it.next();
95  if (oldCommand.equals(command)) {
96  it.remove();
97  break;
98  }
99  }
100  }
101 
107  @Nullable
108  public String up() {
109  return 1 <= commandIndex && commandIndex <= commands.size() ? commands.get(--commandIndex) : null;
110  }
111 
116  @Nullable
117  public String down() {
118  if (commandIndex < commands.size()) {
119  commandIndex++;
120  }
121  return commandIndex < commands.size() ? commands.get(commandIndex) : null;
122  }
123 
130  @Nullable
131  public CharSequence last(final int index) {
132  return index >= commands.size() ? null : commands.get(commands.size()-index-1);
133  }
134 
135 }
static final int HISTORY_SIZE
The maximum number of commands to store.
void removeCommand(@NotNull final String command)
Removes a command.
CharSequence last(final int index)
Returns the last executed command.
void trimToMaxSize()
Cut off old entries if more than HISTORY_SIZE commands are present.
CommandHistory(@NotNull final String ident)
Creates a new instance.
void addCommand(@NotNull final String command)
Adds a new command.
Manages a list of previously entered commands.