Crossfire JXClient, Trunk
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-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.settings;
24 
25 import java.util.ArrayList;
26 import java.util.Iterator;
27 import java.util.List;
28 import org.jetbrains.annotations.NotNull;
29 import org.jetbrains.annotations.Nullable;
30 
35 public class CommandHistory {
36 
40  private static final int HISTORY_SIZE = 100;
41 
45  @NotNull
46  private final List<String> commands = new ArrayList<>(HISTORY_SIZE);
47 
51  private int commandIndex;
52 
57  public CommandHistory(@NotNull final String ident) {
58  }
59 
64  public void addCommand(@NotNull final String command) {
65  final String trimmedCommand = command.trim();
66  if (trimmedCommand.length() <= 0) {
67  return;
68  }
69  removeCommand(trimmedCommand);
70  commands.add(trimmedCommand);
71  commandIndex = commands.size();
72  trimToMaxSize();
73  }
74 
79  private void trimToMaxSize() {
80  while (commands.size() > HISTORY_SIZE) {
81  commands.remove(0);
82  if (commandIndex > 0) {
83  commandIndex--;
84  }
85  }
86  }
87 
92  private void removeCommand(@NotNull final String command) {
93  final Iterator<String> it = commands.iterator();
94  while (it.hasNext()) {
95  final String oldCommand = it.next();
96  if (oldCommand.equals(command)) {
97  it.remove();
98  break;
99  }
100  }
101  }
102 
108  @Nullable
109  public String up() {
110  return 1 <= commandIndex && commandIndex <= commands.size() ? commands.get(--commandIndex) : null;
111  }
112 
117  @Nullable
118  public String down() {
119  if (commandIndex < commands.size()) {
120  commandIndex++;
121  }
122  return commandIndex < commands.size() ? commands.get(commandIndex) : null;
123  }
124 
131  @Nullable
132  public String last(final int index) {
133  return index >= commands.size() ? null : commands.get(commands.size()-index-1);
134  }
135 
136 }
com.realtime.crossfire.jxclient.settings.CommandHistory.removeCommand
void removeCommand(@NotNull final String command)
Definition: CommandHistory.java:92
com.realtime.crossfire.jxclient.settings.CommandHistory
Definition: CommandHistory.java:35
com.realtime.crossfire.jxclient.settings.CommandHistory.addCommand
void addCommand(@NotNull final String command)
Definition: CommandHistory.java:64
com.realtime.crossfire.jxclient.settings.CommandHistory.commands
final List< String > commands
Definition: CommandHistory.java:46
com.realtime.crossfire.jxclient.settings.CommandHistory.up
String up()
Definition: CommandHistory.java:109
com.realtime.crossfire.jxclient.settings.CommandHistory.last
String last(final int index)
Definition: CommandHistory.java:132
com.realtime.crossfire.jxclient.settings.CommandHistory.commandIndex
int commandIndex
Definition: CommandHistory.java:51
com.realtime.crossfire.jxclient.settings.CommandHistory.CommandHistory
CommandHistory(@NotNull final String ident)
Definition: CommandHistory.java:57
com.realtime.crossfire.jxclient.settings.CommandHistory.HISTORY_SIZE
static final int HISTORY_SIZE
Definition: CommandHistory.java:40
com.realtime.crossfire.jxclient.settings.CommandHistory.trimToMaxSize
void trimToMaxSize()
Definition: CommandHistory.java:79
com.realtime.crossfire.jxclient.settings.CommandHistory.down
String down()
Definition: CommandHistory.java:118