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