Crossfire JXClient, Trunk  R20561
CommandList.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.gui.commandlist;
23 
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import org.jetbrains.annotations.NotNull;
27 
32 public class CommandList {
33 
37  @NotNull
39 
43  @NotNull
44  private final Collection<GUICommand> commandList = new ArrayList<>();
45 
50  public CommandList(@NotNull final CommandListType commandListType) {
51  this.commandListType = commandListType;
52  }
53 
58  public void add(@NotNull final GUICommand guiCommand) {
59  commandList.add(guiCommand);
60  }
61 
66  private boolean canExecute() {
67  switch (commandListType) {
68  case AND:
69  for (final GUICommand command : commandList) {
70  if (!command.canExecute()) {
71  return false;
72  }
73  }
74  break;
75 
76  case OR:
77  boolean ok = false;
78  for (final GUICommand command : commandList) {
79  if (command.canExecute()) {
80  ok = true;
81  break;
82  }
83  }
84  if (!ok) {
85  return false;
86  }
87  break;
88  }
89 
90  return true;
91  }
92 
97  public void execute() {
98  if (!canExecute()) {
99  return;
100  }
101 
102  commandList.forEach(GUICommand::execute);
103  }
104 
109  @NotNull
110  public String getCommandString() {
111  final StringBuilder sb = new StringBuilder();
112  boolean firstCommand = true;
113  for (final GUICommand guiCommand : commandList) {
114  if (!(guiCommand instanceof GUICommand2)) {
115  throw new AssertionError("Cannot encode command of type "+guiCommand.getClass().getName());
116  }
117  final String commandString = ((GUICommand2)guiCommand).getCommandString();
118  if (firstCommand) {
119  firstCommand = false;
120  } else {
121  sb.append(';');
122  }
123  sb.append(commandString);
124  }
125  return sb.toString();
126  }
127 
128 }
CommandList(@NotNull final CommandListType commandListType)
Creates a new instance as an empty command list.
boolean canExecute()
Returns whether execution is possible.
A GUICommand that has a string representation.
final CommandListType commandListType
The command list type.
void add(@NotNull final GUICommand guiCommand)
Adds a command to the end of this command list.
String getCommandString()
Returns the commands as a string.
final Collection< GUICommand > commandList
The list of GUICommands in execution order.
void execute()
Execute the command list by calling GUICommand#execute() for each command in order.