]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/common/Util.java
[XVSM] Crucial container communication changes
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / common / Util.java
1 package at.ac.tuwien.sbc.valesriegler.common;
2
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;
7
8 import javax.swing.*;
9 import javax.swing.border.TitledBorder;
10 import java.awt.*;
11 import java.net.URI;
12 import java.util.*;
13 import java.util.List;
14
15 public abstract class Util {
16         private static final Logger log = LoggerFactory.getLogger(Util.class);
17     public static boolean useJMS = true;
18
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 = "deliverDeliveryOrderContainer";
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";
37     public static final String PIZZERIA_GROUP = "pizzeriaGroupContainer";
38     public static final String PIZZERIA_TABLE = "pizzeriaTableContainer";
39     public static final String PIZZERIA_DELIVERY = "pizzeriaDeliveryContainer";
40
41         private static Random random = new Random();
42
43         public static final long SPACE_TRANSACTION_TIMEOUT = 1500;
44
45         public static final String SERVER_ADDR = "xvsm://localhost:%d";
46         public static final int GROUP_AGENT_PORT = 9876;
47         public static final int DELIVERY_CUSTOMERS_PORT = 9877;
48
49         public static final String JMS_CONNECTSTRING = "tcp://localhost:61616?jms.prefetchPolicy.all=1";
50
51         public static String getId(int id) {
52                 return (id != 0 && id != -1) ? String.valueOf(id) : "";
53         }
54
55         public static int getIntSafe(Integer nr) {
56                 return nr == null ? 0 : nr;
57         }
58
59     public static URI createURI(int port) {
60         return URI.create(String.format(SERVER_ADDR, port));
61     }
62
63         public static <T extends HasId> Map<Integer, T> intoMapById(List<T> hasIds) {
64                 if (hasIds == null) {
65                         return Collections.emptyMap();
66                 }
67
68                 Map<Integer, T> myMap = new HashMap<>();
69                 for (T hasId : hasIds) {
70                         myMap.put(hasId.getId(), hasId);
71                 }
72                 return myMap;
73         }
74
75         public static int getRandom(int min, int max) {
76                 return random.nextInt(max - min + 1) + min;
77         }
78
79     public static Tuple<Integer> parseIdAndSpacePort(String[] args, String usage) {
80         if (args.length != 2) {
81             throw new IllegalArgumentException(usage);
82         }
83
84         int parsedId = 0;
85         int port = 0;
86         try {
87             parsedId = Integer.parseInt(args[0]);
88             port = Integer.parseInt(args[1]);
89         } catch (NumberFormatException e) {
90             throw new IllegalArgumentException(usage);
91         }
92
93         return new Tuple<Integer>(parsedId, port);
94     }
95
96     public static JTable createTableInTitledPanel(JPanel wrapperPanel, TableModel<?> model, String title) {
97         JTable table = new JTable(model);
98         wrapperPanel.setLayout(new GridBagLayout());
99         GridBagConstraints c = new GridBagConstraints();
100         c.gridx = 0;
101         c.weightx = 1;
102         c.weighty = 1;
103         c.insets = new Insets(10, 10, 10, 10);
104         c.fill = GridBagConstraints.BOTH;
105         JScrollPane scrollPane = new JScrollPane(table);
106
107         wrapperPanel.setBorder(new TitledBorder(title));
108         scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
109         wrapperPanel.add(scrollPane, c);
110         table.setAutoCreateRowSorter(true);
111         return table;
112     }
113
114     /**
115      *
116      * @return A display string for PizzaTypes.
117      *         <p />
118      *         e.g. Cardinale, Cardinale, Margherita is displayed as
119      *         "2xCARDINALE,1xMARGHERITA"
120      */
121     public static String pizzaDisplay(List<PizzaOrder> list) {
122         HashMap<PizzaType, Integer> pizzaCount = new HashMap<PizzaType, Integer>();
123         for (PizzaOrder pizzaOrder : list) {
124             PizzaType pizzaType = pizzaOrder.getPizzaType();
125             if (pizzaCount.containsKey(pizzaType)) {
126                 pizzaCount.put(pizzaType, pizzaCount.get(pizzaType) + 1);
127             } else
128                 pizzaCount.put(pizzaType, 1);
129         }
130         Set<PizzaType> pizzaTypes = pizzaCount.keySet();
131         StringBuilder sb = new StringBuilder();
132
133         boolean multiplePizzas = false;
134         for (PizzaType pizzaType : pizzaTypes) {
135             if (multiplePizzas)
136                 sb.append(", ");
137             else
138                 multiplePizzas = true;
139
140             sb.append(pizzaCount.get(pizzaType) + "x");
141             sb.append(pizzaType.toString());
142         }
143
144         return sb.toString();
145     }
146
147     public static void handleSpaceErrorAndTerminate(Exception e) {
148         log.error(e.getMessage());
149         e.printStackTrace();
150         System.exit(1);
151     }
152 }