]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/common/Util.java
[XVSM] Waiters, Cooks and Pizzerias can be parameterized with a Space port. Group...
[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 java.awt.*;
4 import java.net.URI;
5 import java.util.*;
6 import java.util.List;
7
8 import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder;
9 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
10 import org.mozartspaces.capi3.Coordinator;
11 import org.mozartspaces.core.Capi;
12 import org.mozartspaces.core.ContainerReference;
13 import org.mozartspaces.core.MzsConstants.Container;
14 import org.mozartspaces.core.MzsConstants.RequestTimeout;
15 import org.mozartspaces.core.MzsCoreException;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 import javax.swing.*;
20 import javax.swing.border.TitledBorder;
21
22 public abstract class Util {
23         private static final Logger log = LoggerFactory.getLogger(Util.class);
24
25         public static boolean useJMS = true;
26
27
28
29         public static final String TABLE_ASSIGNED = "tables";
30         public static final String GROUPS_CONTAINER = "groups";
31         public static final String ASSIGN_TABLE = "assignTable";
32         public static final String TAKE_ORDER = "takeOrder";
33         public static final String ORDER_TAKEN = "order";
34         public static final String DELIVER_PIZZAS = "deliverPizzas";
35         public static final String PREPARE_PIZZAS = "preparePizzas";
36         public static final String PIZZAS_IN_PROGRESS = "pizzasInProgress";
37         public static final String ORDER_COMPLETE = "orderComplete";
38         public static final String PAYMENT_REQUEST = "payment";
39         public static final String FREE_TABLES = "freeTables";
40         public static final String IS_EATING = "isEating";
41         public static final String PAYMENT_DONE = "hasPaid";
42         public static final String INFO = "info";
43
44         private static Random random = new Random();
45
46         public static final long SPACE_TRANSACTION_TIMEOUT = 1500;
47
48         public static final String SERVER_ADDR = "xvsm://localhost:%d";
49         public static final int GROUP_AGENT_PORT = 9876;
50
51         public static final String JMS_CONNECTSTRING = "tcp://localhost:61616?jms.prefetchPolicy.all=1";
52
53         public static String getId(int id) {
54                 return (id != 0 && id != -1) ? String.valueOf(id) : "";
55         }
56
57         public static int getIntSafe(Integer nr) {
58                 return nr == null ? 0 : nr;
59         }
60
61     public static URI createURI(int port) {
62         return URI.create(String.format(SERVER_ADDR, port));
63     }
64
65         public static <T extends HasId> Map<Integer, T> intoMapById(List<T> hasIds) {
66                 if (hasIds == null) {
67                         return Collections.emptyMap();
68                 }
69
70                 Map<Integer, T> myMap = new HashMap<>();
71                 for (T hasId : hasIds) {
72                         myMap.put(hasId.getId(), hasId);
73                 }
74                 return myMap;
75         }
76
77         public static int getRandom(int min, int max) {
78                 return random.nextInt(max - min + 1) + min;
79         }
80
81     public static Tuple<Integer> parseIdAndSpacePort(String[] args, String usage) {
82         if (args.length != 2) {
83             throw new IllegalArgumentException(usage);
84         }
85
86         int parsedId = 0;
87         int port = 0;
88         try {
89             parsedId = Integer.parseInt(args[0]);
90             port = Integer.parseInt(args[1]);
91         } catch (NumberFormatException e) {
92             throw new IllegalArgumentException(usage);
93         }
94
95         return new Tuple<Integer>(parsedId, port);
96     }
97
98     public static JTable createTableInTitledPanel(JPanel wrapperPanel, TableModel<?> model, String title) {
99         JTable table = new JTable(model);
100         wrapperPanel.setLayout(new GridBagLayout());
101         GridBagConstraints c = new GridBagConstraints();
102         c.gridx = 0;
103         c.weightx = 1;
104         c.weighty = 1;
105         c.insets = new Insets(10, 10, 10, 10);
106         c.fill = GridBagConstraints.BOTH;
107         JScrollPane scrollPane = new JScrollPane(table);
108
109         wrapperPanel.setBorder(new TitledBorder(title));
110         scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
111         wrapperPanel.add(scrollPane, c);
112
113         return table;
114     }
115
116     /**
117      *
118      * @return A display string for PizzaTypes.
119      *         <p />
120      *         e.g. Cardinale, Cardinale, Margherita is displayed as
121      *         "2xCARDINALE,1xMARGHERITA"
122      */
123     public static String pizzaDisplay(List<PizzaOrder> list) {
124         HashMap<PizzaType, Integer> pizzaCount = new HashMap<PizzaType, Integer>();
125         for (PizzaOrder pizzaOrder : list) {
126             PizzaType pizzaType = pizzaOrder.getPizzaType();
127             if (pizzaCount.containsKey(pizzaType)) {
128                 pizzaCount.put(pizzaType, pizzaCount.get(pizzaType) + 1);
129             } else
130                 pizzaCount.put(pizzaType, 1);
131         }
132         Set<PizzaType> pizzaTypes = pizzaCount.keySet();
133         StringBuilder sb = new StringBuilder();
134
135         boolean multiplePizzas = false;
136         for (PizzaType pizzaType : pizzaTypes) {
137             if (multiplePizzas)
138                 sb.append(", ");
139             else
140                 multiplePizzas = true;
141
142             sb.append(pizzaCount.get(pizzaType) + "x");
143             sb.append(pizzaType.toString());
144         }
145
146         return sb.toString();
147     }
148 }