67343c7da27150786909c4426a2f9fcb73b2491d
[jabaws.git] / engine / compbio / engine / conf / _Key.java
1 /* Copyright (c) 2009 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0     \r
4  * \r
5  *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
6  *  Apache License version 2 as published by the Apache Software Foundation\r
7  * \r
8  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
9  *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
10  *  License for more details.\r
11  * \r
12  *  A copy of the license is in apache_license.txt. It is also available here:\r
13  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 \r
19 package compbio.engine.conf;\r
20 \r
21 import javax.management.openmbean.InvalidKeyException;\r
22 \r
23 import compbio.util.Util;\r
24 \r
25 public final class _Key {\r
26 \r
27         public final static String DELIM = "#";\r
28 \r
29         private final String cname;\r
30         private final long id;\r
31 \r
32         public _Key(Class<?> clazz) {\r
33                 if (clazz == null) {\r
34                         throw new IllegalArgumentException("Class or Id is NULL");\r
35                 }\r
36                 this.cname = clazz.getSimpleName();\r
37                 this.id = getNonRepeatableNumber();\r
38         }\r
39 \r
40         _Key(Class<?> clazz, long id) {\r
41                 if (clazz == null) {\r
42                         throw new IllegalArgumentException("Class or Id is NULL");\r
43                 }\r
44                 this.cname = clazz.getSimpleName();\r
45                 this.id = id;\r
46         }\r
47 \r
48         @Override\r
49         public boolean equals(Object obj) {\r
50                 if (obj == null) {\r
51                         return false;\r
52                 }\r
53                 _Key ckey = null;\r
54                 if (obj instanceof _Key) {\r
55                         ckey = (_Key) obj;\r
56                 } else {\r
57                         return false;\r
58                 }\r
59                 if (ckey.cname.equals(this.cname) && id == ckey.id) {\r
60                         return true;\r
61                 }\r
62                 return false;\r
63         }\r
64 \r
65         @Override\r
66         public String toString() {\r
67                 return cname + DELIM + id;\r
68         }\r
69 \r
70         /*\r
71          * id = 533411011589881 - 15 or 16 chars depending on the OS\r
72          * \r
73          * @see java.lang.Object#hashCode()\r
74          */\r
75         @Override\r
76         public int hashCode() {\r
77                 return new Integer(cname.hashCode()\r
78                                 * new Integer(new Long(id).toString().substring(9))).intValue();\r
79         }\r
80 \r
81         static long getNonRepeatableNumber() {\r
82                 // Keep the random number 2 digits wide to simplify the task Id parsing\r
83                 // if required\r
84                 // The random value is concatenated with time value not added to it and\r
85                 // then converted to long.\r
86                 return Long.parseLong(Util.getRandomNumber(10, 99) + ""\r
87                                 + System.nanoTime());\r
88         }\r
89 \r
90         public static _Key parse(String key) {\r
91                 if (Util.isEmpty(key)) {\r
92                         throw new NullPointerException("Key must be provided!");\r
93                 }\r
94                 if (!key.contains(DELIM)) {\r
95                         throw new InvalidKeyException("Key " + key\r
96                                         + " is not a valid task id");\r
97                 }\r
98 \r
99                 int idx = key.indexOf(DELIM);\r
100                 if (idx < 0) {\r
101                         throw new InvalidKeyException("Key " + key\r
102                                         + " is not a valid task id");\r
103                 }\r
104                 String runnerName = key.substring(0, idx);\r
105                 long id = 0;\r
106                 Class<?> runner = null;\r
107                 try {\r
108                         runner = Class.forName(runnerName);\r
109                         id = Long.parseLong(key.substring(idx + 1));\r
110                 } catch (NumberFormatException e) {\r
111                         throw new InvalidKeyException("Key " + key\r
112                                         + " is not a valid task id");\r
113                 } catch (ClassNotFoundException e) {\r
114                         throw new InvalidKeyException("Key " + key\r
115                                         + " is not a valid task id");\r
116                 }\r
117                 return new _Key(runner, id);\r
118         }\r
119 \r
120 }\r