]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/common/Util.java
replaced all <> with explicit types - needed for jenkins
[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
65         public static final boolean runSimulation = false;
66
67         public static final String NUMBER_DISPLAY_FORMAT = String.format("%%0%dd", 3);
68
69         private static Random random = new Random();
70
71         public static final long SPACE_TRANSACTION_TIMEOUT = 1500;
72
73         public static final String SERVER_ADDR = "xvsm://localhost:%d";
74         public static final int GROUP_AGENT_PORT = 9876;
75         public static final int DELIVERY_CUSTOMERS_PORT = 9877;
76
77         public static final String JMS_DELIVERY_DESTINATION = "tcp://localhost:61611?jms.prefetchPolicy.all=1";
78
79         public static String getId(int id) {
80                 return (id != 0 && id != -1) ? String.valueOf(id) : "";
81         }
82
83         public static int getIntSafe(Integer nr) {
84                 return nr == null ? 0 : nr;
85         }
86
87         public static URI createURI(int port) {
88                 return URI.create(String.format(SERVER_ADDR, port));
89         }
90
91         public static <T extends HasId> Map<Integer, T> intoMapById(List<T> hasIds) {
92                 if (hasIds == null) {
93                         return Collections.emptyMap();
94                 }
95
96                 Map<Integer, T> myMap = new HashMap<Integer, T>();
97                 for (T hasId : hasIds) {
98                         myMap.put(hasId.getId(), hasId);
99                 }
100                 return myMap;
101         }
102
103         public static int getRandom(int min, int max) {
104                 return random.nextInt(max - min + 1) + min;
105         }
106
107         public static Tuple<Integer> parseIdAndSpacePort(String[] args, String usage) {
108                 if (args.length != 2) {
109                         throw new IllegalArgumentException(usage);
110                 }
111
112                 int parsedId = 0;
113                 int port = 0;
114                 try {
115                         parsedId = Integer.parseInt(args[0]);
116                         port = Integer.parseInt(args[1]);
117                 } catch (NumberFormatException e) {
118                         throw new IllegalArgumentException(usage);
119                 }
120
121                 return new Tuple<Integer>(parsedId, port);
122         }
123
124         public static JTable createTableInTitledPanel(JPanel wrapperPanel, TableModel<?> model, String title) {
125                 JTable table = new JTable(model);
126                 wrapperPanel.setLayout(new GridBagLayout());
127                 GridBagConstraints c = new GridBagConstraints();
128                 c.gridx = 0;
129                 c.weightx = 1;
130                 c.weighty = 1;
131                 c.insets = new Insets(10, 10, 10, 10);
132                 c.fill = GridBagConstraints.BOTH;
133                 JScrollPane scrollPane = new JScrollPane(table);
134
135                 wrapperPanel.setBorder(new TitledBorder(title));
136                 scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
137                 wrapperPanel.add(scrollPane, c);
138                 return table;
139         }
140
141         /**
142          * 
143          * @return A display string for PizzaTypes.
144          *         <p />
145          *         e.g. Cardinale, Cardinale, Margherita is displayed as "2xCARDINALE,1xMARGHERITA"
146          */
147         public static String pizzaDisplay(List<PizzaOrder> list) {
148                 HashMap<PizzaType, Integer> pizzaCount = new HashMap<PizzaType, Integer>();
149                 for (PizzaOrder pizzaOrder : list) {
150                         PizzaType pizzaType = pizzaOrder.getPizzaType();
151                         if (pizzaCount.containsKey(pizzaType)) {
152                                 pizzaCount.put(pizzaType, pizzaCount.get(pizzaType) + 1);
153                         } else
154                                 pizzaCount.put(pizzaType, 1);
155                 }
156                 Set<PizzaType> pizzaTypes = pizzaCount.keySet();
157                 StringBuilder sb = new StringBuilder();
158
159                 boolean multiplePizzas = false;
160                 for (PizzaType pizzaType : pizzaTypes) {
161                         if (multiplePizzas)
162                                 sb.append(", ");
163                         else
164                                 multiplePizzas = true;
165
166                         sb.append(pizzaCount.get(pizzaType) + "x");
167                         sb.append(pizzaType.toString());
168                 }
169
170                 return sb.toString();
171         }
172
173         public static void handleSpaceErrorAndTerminate(Exception e) {
174                 log.error(e.getMessage());
175                 e.printStackTrace();
176                 System.exit(1);
177         }
178
179         public static Order createOrder(HasId group, GroupCreationDetailsRequest gc, boolean isDelivery) {
180                 List<PizzaOrder> pizzaOrders = new ArrayList<PizzaOrder>();
181                 for (PizzaType pt : gc.pizzaTypes) {
182                         final PizzaOrder pizzaOrder = new PizzaOrder(pt);
183             pizzaOrder.setDeliveryPizza(isDelivery);
184                         pizzaOrders.add(pizzaOrder);
185                 }
186                 Order order = new Order(group, pizzaOrders);
187                 order.setNumberOfPizzas(order.getOrderedPizzas().size());
188
189                 return order;
190         }
191 }