]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/common/Util.java
[XVSM]runXVSM
[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.group.gui.GroupCreationDetailsRequest;
4 import at.ac.tuwien.sbc.valesriegler.types.Order;
5 import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder;
6 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
9
10 import javax.swing.*;
11 import javax.swing.border.TitledBorder;
12 import java.awt.*;
13 import java.net.URI;
14 import java.util.*;
15 import java.util.List;
16
17 public abstract class Util {
18         private static final Logger log = LoggerFactory.getLogger(Util.class);
19         public static boolean useJMS = false;
20
21         public static final String TABLE_ASSIGNED = "tables";
22         public static final String ASSIGN_TABLE = "assignTable";
23         public static final String TAKE_ORDER = "takeOrder";
24         public static final String ORDER_TAKEN = "order";
25         public static final String DELIVERY_ORDER_TAKEN = "deliverOrderTaken";
26         public static final String DELIVER_PIZZAS = "deliverPizzas";
27         public static final String DELIVER_DELIVERY_PIZZAS = "deliverDeliveryPizzas";
28         public static final String DELIVER_DELIVERY_ORDER = "deliverDeliveryOrderContainer";
29         public static final String PREPARE_PIZZAS = "preparePizzas";
30         public static final String PREPARE_DELIVERY_PIZZAS = "prepareDeliveryPizzas";
31         public static final String PIZZAS_IN_PROGRESS = "pizzasInProgress";
32         public static final String ORDER_COMPLETE = "orderComplete";
33         public static final String PAYMENT_REQUEST = "payment";
34         public static final String FREE_TABLES = "freeTables";
35         public static final String PAYMENT_DONE = "hasPaid";
36         public static final String PIZZERIA_INFO = "pizzeriaInfo";
37         public static final String PHONE_CALLS = "phoneCalls";
38         public static final String GROUP_AGENT_INFO = "groupAgentInfo";
39         public static final String PIZZERIA_GROUP = "pizzeriaGroupContainer";
40         public static final String PIZZERIA_TABLE = "pizzeriaTableContainer";
41         public static final String PIZZERIA_DELIVERY = "pizzeriaDeliveryContainer";
42         public static final String DELIVERY_PROGRESS = "deliveryInProgress";
43         public static final String DELIVERY_DONE = "deliveryDone";
44
45         public static final boolean runSimulation = true;
46
47         public static final String NUMBER_DISPLAY_FORMAT = String.format("%%0%dd", 3);
48
49         private static Random random = new Random();
50
51         public static final long SPACE_TRANSACTION_TIMEOUT = 1500;
52
53         public static final String SERVER_ADDR = "xvsm://localhost:%d";
54         public static final int GROUP_AGENT_PORT = 9876;
55         public static final int DELIVERY_CUSTOMERS_PORT = 9877;
56
57         public static final String JMS_DELIVERY_DESTINATION = "tcp://localhost:61611?jms.prefetchPolicy.all=1";
58
59         public static String getId(int id) {
60                 return (id != 0 && id != -1) ? String.valueOf(id) : "";
61         }
62
63         public static int getIntSafe(Integer nr) {
64                 return nr == null ? 0 : nr;
65         }
66
67         public static URI createURI(int port) {
68                 return URI.create(String.format(SERVER_ADDR, port));
69         }
70
71         public static <T extends HasId> Map<Integer, T> intoMapById(List<T> hasIds) {
72                 if (hasIds == null) {
73                         return Collections.emptyMap();
74                 }
75
76                 Map<Integer, T> myMap = new HashMap<>();
77                 for (T hasId : hasIds) {
78                         myMap.put(hasId.getId(), hasId);
79                 }
80                 return myMap;
81         }
82
83         public static int getRandom(int min, int max) {
84                 return random.nextInt(max - min + 1) + min;
85         }
86
87         public static Tuple<Integer> parseIdAndSpacePort(String[] args, String usage) {
88                 if (args.length != 2) {
89                         throw new IllegalArgumentException(usage);
90                 }
91
92                 int parsedId = 0;
93                 int port = 0;
94                 try {
95                         parsedId = Integer.parseInt(args[0]);
96                         port = Integer.parseInt(args[1]);
97                 } catch (NumberFormatException e) {
98                         throw new IllegalArgumentException(usage);
99                 }
100
101                 return new Tuple<Integer>(parsedId, port);
102         }
103
104         public static JTable createTableInTitledPanel(JPanel wrapperPanel, TableModel<?> model, String title) {
105                 JTable table = new JTable(model);
106                 wrapperPanel.setLayout(new GridBagLayout());
107                 GridBagConstraints c = new GridBagConstraints();
108                 c.gridx = 0;
109                 c.weightx = 1;
110                 c.weighty = 1;
111                 c.insets = new Insets(10, 10, 10, 10);
112                 c.fill = GridBagConstraints.BOTH;
113                 JScrollPane scrollPane = new JScrollPane(table);
114
115                 wrapperPanel.setBorder(new TitledBorder(title));
116                 scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
117                 wrapperPanel.add(scrollPane, c);
118                 // table.setAutoCreateRowSorter(true);
119                 return table;
120         }
121
122         /**
123          * 
124          * @return A display string for PizzaTypes.
125          *         <p />
126          *         e.g. Cardinale, Cardinale, Margherita is displayed as "2xCARDINALE,1xMARGHERITA"
127          */
128         public static String pizzaDisplay(List<PizzaOrder> list) {
129                 HashMap<PizzaType, Integer> pizzaCount = new HashMap<PizzaType, Integer>();
130                 for (PizzaOrder pizzaOrder : list) {
131                         PizzaType pizzaType = pizzaOrder.getPizzaType();
132                         if (pizzaCount.containsKey(pizzaType)) {
133                                 pizzaCount.put(pizzaType, pizzaCount.get(pizzaType) + 1);
134                         } else
135                                 pizzaCount.put(pizzaType, 1);
136                 }
137                 Set<PizzaType> pizzaTypes = pizzaCount.keySet();
138                 StringBuilder sb = new StringBuilder();
139
140                 boolean multiplePizzas = false;
141                 for (PizzaType pizzaType : pizzaTypes) {
142                         if (multiplePizzas)
143                                 sb.append(", ");
144                         else
145                                 multiplePizzas = true;
146
147                         sb.append(pizzaCount.get(pizzaType) + "x");
148                         sb.append(pizzaType.toString());
149                 }
150
151                 return sb.toString();
152         }
153
154         public static void handleSpaceErrorAndTerminate(Exception e) {
155                 log.error(e.getMessage());
156                 e.printStackTrace();
157                 System.exit(1);
158         }
159
160         public static Order createOrder(HasId group, GroupCreationDetailsRequest gc) {
161                 List<PizzaOrder> pizzaOrders = new ArrayList<>();
162                 for (PizzaType pt : gc.pizzaTypes) {
163                         final PizzaOrder pizzaOrder = new PizzaOrder(pt);
164                         pizzaOrders.add(pizzaOrder);
165                 }
166                 Order order = new Order(group, pizzaOrders);
167                 order.setNumberOfPizzas(order.getOrderedPizzas().size());
168
169                 return order;
170         }
171 }