Javadoc fixes
[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 @Deprecated\r
26 public final class _Key {\r
27 \r
28         public final static String DELIM = "#";\r
29 \r
30         private final String cname;\r
31         private final long id;\r
32 \r
33         public _Key(Class<?> clazz) {\r
34                 if (clazz == null) {\r
35                         throw new IllegalArgumentException("Class or Id is NULL");\r
36                 }\r
37                 this.cname = clazz.getSimpleName();\r
38                 this.id = getNonRepeatableNumber();\r
39         }\r
40 \r
41         _Key(Class<?> clazz, long id) {\r
42                 if (clazz == null) {\r
43                         throw new IllegalArgumentException("Class or Id is NULL");\r
44                 }\r
45                 this.cname = clazz.getSimpleName();\r
46                 this.id = id;\r
47         }\r
48 \r
49         @Override\r
50         public boolean equals(Object obj) {\r
51                 if (obj == null) {\r
52                         return false;\r
53                 }\r
54                 _Key ckey = null;\r
55                 if (obj instanceof _Key) {\r
56                         ckey = (_Key) obj;\r
57                 } else {\r
58                         return false;\r
59                 }\r
60                 if (ckey.cname.equals(this.cname) && id == ckey.id) {\r
61                         return true;\r
62                 }\r
63                 return false;\r
64         }\r
65 \r
66         @Override\r
67         public String toString() {\r
68                 return cname + DELIM + id;\r
69         }\r
70 \r
71         /*\r
72          * id = 533411011589881 - 15 or 16 chars depending on the OS\r
73          * \r
74          * @see java.lang.Object#hashCode()\r
75          */\r
76         @Override\r
77         public int hashCode() {\r
78                 return new Integer(cname.hashCode()\r
79                                 * new Integer(new Long(id).toString().substring(9))).intValue();\r
80         }\r
81 \r
82         static long getNonRepeatableNumber() {\r
83                 // Keep the random number 2 digits wide to simplify the task Id parsing\r
84                 // if required\r
85                 // The random value is concatenated with time value not added to it and\r
86                 // then converted to long.\r
87                 return Long.parseLong(Util.getRandomNumber(10, 99) + ""\r
88                                 + System.nanoTime());\r
89         }\r
90 \r
91         public static _Key parse(String key) {\r
92                 if (Util.isEmpty(key)) {\r
93                         throw new NullPointerException("Key must be provided!");\r
94                 }\r
95                 if (!key.contains(DELIM)) {\r
96                         throw new InvalidKeyException("Key " + key\r
97                                         + " is not a valid task id");\r
98                 }\r
99 \r
100                 int idx = key.indexOf(DELIM);\r
101                 if (idx < 0) {\r
102                         throw new InvalidKeyException("Key " + key\r
103                                         + " is not a valid task id");\r
104                 }\r
105                 String runnerName = key.substring(0, idx);\r
106                 long id = 0;\r
107                 Class<?> runner = null;\r
108                 try {\r
109                         runner = Class.forName(runnerName);\r
110                         id = Long.parseLong(key.substring(idx + 1));\r
111                 } catch (NumberFormatException e) {\r
112                         throw new InvalidKeyException("Key " + key\r
113                                         + " is not a valid task id");\r
114                 } catch (ClassNotFoundException e) {\r
115                         throw new InvalidKeyException("Key " + key\r
116                                         + " is not a valid task id");\r
117                 }\r
118                 return new _Key(runner, id);\r
119         }\r
120 \r
121 }\r