]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/common/Util.java
Create Driver and DriverXVSM classes for XVSM
[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.net.URI;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Random;
9
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 public abstract class Util {
20         private static final Logger log = LoggerFactory.getLogger(Util.class);
21
22         // TODO: solve the switch between mom by command-line arguments
23         public static boolean useJMS = true;
24
25         public static final String TABLE_ASSIGNED = "tables";
26         public static final String GROUPS_CONTAINER = "groups";
27         public static final String ASSIGN_TABLE = "assignTable";
28         public static final String TAKE_ORDER = "takeOrder";
29         public static final String ORDER_TAKEN = "order";
30         public static final String DELIVER_PIZZAS = "deliverPizzas";
31         public static final String PREPARE_PIZZAS = "preparePizzas";
32         public static final String PIZZAS_IN_PROGRESS = "pizzasInProgress";
33         public static final String ORDER_COMPLETE = "orderComplete";
34         public static final String PAYMENT_REQUEST = "payment";
35         public static final String FREE_TABLES = "freeTables";
36         public static final String IS_EATING = "isEating";
37         public static final String PAYMENT_DONE = "hasPaid";
38         public static final String INFO = "info";
39
40         private static Random random = new Random();
41
42         public static final long SPACE_TRANSACTION_TIMEOUT = 1500;
43
44         public static final String SERVER_ADDR = "xvsm://localhost:9876";
45
46         public static final String JMS_CONNECTSTRING = "tcp://localhost:61616?jms.prefetchPolicy.all=1";
47
48         public static String getId(int id) {
49                 return (id != 0 && id != -1) ? String.valueOf(id) : "";
50         }
51
52         public static int getIntSafe(Integer nr) {
53                 return nr == null ? 0 : nr;
54         }
55
56         public static <T extends HasId> Map<Integer, T> intoMapById(List<T> hasIds) {
57                 if (hasIds == null) {
58                         return Collections.emptyMap();
59                 }
60
61                 Map<Integer, T> myMap = new HashMap<>();
62                 for (T hasId : hasIds) {
63                         myMap.put(hasId.getId(), hasId);
64                 }
65                 return myMap;
66         }
67
68         public static int getRandom(int min, int max) {
69                 return random.nextInt(max - min + 1) + min;
70         }
71
72     public static int parseId(String[] args, String usage) {
73         if (args.length != 1) {
74             throw new IllegalArgumentException(usage);
75         }
76
77         int parsedId = 0;
78         try {
79             parsedId = Integer.parseInt(args[0]);
80         } catch (NumberFormatException e) {
81             throw new IllegalArgumentException(usage);
82         }
83
84         return parsedId;
85     }
86
87 }