1 /* Copyright (c) 2009 Peter Troshin
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0
\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
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
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
15 * Any republication or derived work distributed in source code form
\r
16 * must include this copyright and license notice.
\r
19 package compbio.engine.conf;
\r
21 import javax.management.openmbean.InvalidKeyException;
\r
23 import compbio.util.Util;
\r
25 public final class _Key {
\r
27 public final static String DELIM = "#";
\r
29 private final String cname;
\r
30 private final long id;
\r
32 public _Key(Class<?> clazz) {
\r
33 if (clazz == null) {
\r
34 throw new IllegalArgumentException("Class or Id is NULL");
\r
36 this.cname = clazz.getSimpleName();
\r
37 this.id = getNonRepeatableNumber();
\r
40 _Key(Class<?> clazz, long id) {
\r
41 if (clazz == null) {
\r
42 throw new IllegalArgumentException("Class or Id is NULL");
\r
44 this.cname = clazz.getSimpleName();
\r
49 public boolean equals(Object obj) {
\r
54 if (obj instanceof _Key) {
\r
59 if (ckey.cname.equals(this.cname) && id == ckey.id) {
\r
66 public String toString() {
\r
67 return cname + DELIM + id;
\r
71 * id = 533411011589881 - 15 or 16 chars depending on the OS
\r
73 * @see java.lang.Object#hashCode()
\r
76 public int hashCode() {
\r
77 return new Integer(cname.hashCode()
\r
78 * new Integer(new Long(id).toString().substring(9))).intValue();
\r
81 static long getNonRepeatableNumber() {
\r
82 // Keep the random number 2 digits wide to simplify the task Id parsing
\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
90 public static _Key parse(String key) {
\r
91 if (Util.isEmpty(key)) {
\r
92 throw new NullPointerException("Key must be provided!");
\r
94 if (!key.contains(DELIM)) {
\r
95 throw new InvalidKeyException("Key " + key
\r
96 + " is not a valid task id");
\r
99 int idx = key.indexOf(DELIM);
\r
101 throw new InvalidKeyException("Key " + key
\r
102 + " is not a valid task id");
\r
104 String runnerName = key.substring(0, idx);
\r
106 Class<?> runner = null;
\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
117 return new _Key(runner, id);
\r