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
26 public final class _Key {
\r
28 public final static String DELIM = "#";
\r
30 private final String cname;
\r
31 private final long id;
\r
33 public _Key(Class<?> clazz) {
\r
34 if (clazz == null) {
\r
35 throw new IllegalArgumentException("Class or Id is NULL");
\r
37 this.cname = clazz.getSimpleName();
\r
38 this.id = getNonRepeatableNumber();
\r
41 _Key(Class<?> clazz, long id) {
\r
42 if (clazz == null) {
\r
43 throw new IllegalArgumentException("Class or Id is NULL");
\r
45 this.cname = clazz.getSimpleName();
\r
50 public boolean equals(Object obj) {
\r
55 if (obj instanceof _Key) {
\r
60 if (ckey.cname.equals(this.cname) && id == ckey.id) {
\r
67 public String toString() {
\r
68 return cname + DELIM + id;
\r
72 * id = 533411011589881 - 15 or 16 chars depending on the OS
\r
74 * @see java.lang.Object#hashCode()
\r
77 public int hashCode() {
\r
78 return new Integer(cname.hashCode()
\r
79 * new Integer(new Long(id).toString().substring(9))).intValue();
\r
82 static long getNonRepeatableNumber() {
\r
83 // Keep the random number 2 digits wide to simplify the task Id parsing
\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
91 public static _Key parse(String key) {
\r
92 if (Util.isEmpty(key)) {
\r
93 throw new NullPointerException("Key must be provided!");
\r
95 if (!key.contains(DELIM)) {
\r
96 throw new InvalidKeyException("Key " + key
\r
97 + " is not a valid task id");
\r
100 int idx = key.indexOf(DELIM);
\r
102 throw new InvalidKeyException("Key " + key
\r
103 + " is not a valid task id");
\r
105 String runnerName = key.substring(0, idx);
\r
107 Class<?> runner = null;
\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
118 return new _Key(runner, id);
\r