1 package at.ac.tuwien.sbc.valesriegler.common;
3 import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder;
4 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
5 import org.slf4j.Logger;
6 import org.slf4j.LoggerFactory;
9 import javax.swing.border.TitledBorder;
13 import java.util.List;
15 public abstract class Util {
16 private static final Logger log = LoggerFactory.getLogger(Util.class);
17 public static boolean useJMS = true;
19 public static final String TABLE_ASSIGNED = "tables";
20 public static final String ASSIGN_TABLE = "assignTable";
21 public static final String TAKE_ORDER = "takeOrder";
22 public static final String ORDER_TAKEN = "order";
23 public static final String DELIVERY_ORDER_TAKEN = "deliverOrderTaken";
24 public static final String DELIVER_PIZZAS = "deliverPizzas";
25 public static final String DELIVER_DELIVERY_PIZZAS = "deliverDeliveryPizzas";
26 public static final String DELIVER_DELIVERY_ORDER = "deliverDeliveryOrder";
27 public static final String PREPARE_PIZZAS = "preparePizzas";
28 public static final String PREPARE_DELIVERY_PIZZAS = "prepareDeliveryPizzas";
29 public static final String PIZZAS_IN_PROGRESS = "pizzasInProgress";
30 public static final String ORDER_COMPLETE = "orderComplete";
31 public static final String PAYMENT_REQUEST = "payment";
32 public static final String FREE_TABLES = "freeTables";
33 public static final String PAYMENT_DONE = "hasPaid";
34 public static final String PIZZERIA_INFO = "pizzeriaInfo";
35 public static final String PHONE_CALLS = "phoneCalls";
36 public static final String GROUP_AGENT_INFO = "groupAgentInfo";
38 private static Random random = new Random();
40 public static final long SPACE_TRANSACTION_TIMEOUT = 1500;
42 public static final String SERVER_ADDR = "xvsm://localhost:%d";
43 public static final int GROUP_AGENT_PORT = 9876;
44 public static final int DELIVERY_CUSTOMERS_PORT = 9877;
46 public static final String JMS_CONNECTSTRING = "tcp://localhost:61616?jms.prefetchPolicy.all=1";
48 public static String getId(int id) {
49 return (id != 0 && id != -1) ? String.valueOf(id) : "";
52 public static int getIntSafe(Integer nr) {
53 return nr == null ? 0 : nr;
56 public static URI createURI(int port) {
57 return URI.create(String.format(SERVER_ADDR, port));
60 public static <T extends HasId> Map<Integer, T> intoMapById(List<T> hasIds) {
62 return Collections.emptyMap();
65 Map<Integer, T> myMap = new HashMap<>();
66 for (T hasId : hasIds) {
67 myMap.put(hasId.getId(), hasId);
72 public static int getRandom(int min, int max) {
73 return random.nextInt(max - min + 1) + min;
76 public static Tuple<Integer> parseIdAndSpacePort(String[] args, String usage) {
77 if (args.length != 2) {
78 throw new IllegalArgumentException(usage);
84 parsedId = Integer.parseInt(args[0]);
85 port = Integer.parseInt(args[1]);
86 } catch (NumberFormatException e) {
87 throw new IllegalArgumentException(usage);
90 return new Tuple<Integer>(parsedId, port);
93 public static JTable createTableInTitledPanel(JPanel wrapperPanel, TableModel<?> model, String title) {
94 JTable table = new JTable(model);
95 wrapperPanel.setLayout(new GridBagLayout());
96 GridBagConstraints c = new GridBagConstraints();
100 c.insets = new Insets(10, 10, 10, 10);
101 c.fill = GridBagConstraints.BOTH;
102 JScrollPane scrollPane = new JScrollPane(table);
104 wrapperPanel.setBorder(new TitledBorder(title));
105 scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
106 wrapperPanel.add(scrollPane, c);
113 * @return A display string for PizzaTypes.
115 * e.g. Cardinale, Cardinale, Margherita is displayed as
116 * "2xCARDINALE,1xMARGHERITA"
118 public static String pizzaDisplay(List<PizzaOrder> list) {
119 HashMap<PizzaType, Integer> pizzaCount = new HashMap<PizzaType, Integer>();
120 for (PizzaOrder pizzaOrder : list) {
121 PizzaType pizzaType = pizzaOrder.getPizzaType();
122 if (pizzaCount.containsKey(pizzaType)) {
123 pizzaCount.put(pizzaType, pizzaCount.get(pizzaType) + 1);
125 pizzaCount.put(pizzaType, 1);
127 Set<PizzaType> pizzaTypes = pizzaCount.keySet();
128 StringBuilder sb = new StringBuilder();
130 boolean multiplePizzas = false;
131 for (PizzaType pizzaType : pizzaTypes) {
135 multiplePizzas = true;
137 sb.append(pizzaCount.get(pizzaType) + "x");
138 sb.append(pizzaType.toString());
141 return sb.toString();
144 public static void handleSpaceErrorAndTerminate(Exception e) {
145 log.error(e.getMessage());