/* Copyright (c) 2009 Peter Troshin * * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0 * * This library is free software; you can redistribute it and/or modify it under the terms of the * Apache License version 2 as published by the Apache Software Foundation * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache * License for more details. * * A copy of the license is in apache_license.txt. It is also available here: * @see: http://www.apache.org/licenses/LICENSE-2.0.txt * * Any republication or derived work distributed in source code form * must include this copyright and license notice. */ package compbio.engine.conf; import javax.management.openmbean.InvalidKeyException; import compbio.util.Util; @Deprecated public final class _Key { public final static String DELIM = "#"; private final String cname; private final long id; public _Key(Class clazz) { if (clazz == null) { throw new IllegalArgumentException("Class or Id is NULL"); } this.cname = clazz.getSimpleName(); this.id = getNonRepeatableNumber(); } _Key(Class clazz, long id) { if (clazz == null) { throw new IllegalArgumentException("Class or Id is NULL"); } this.cname = clazz.getSimpleName(); this.id = id; } @Override public boolean equals(Object obj) { if (obj == null) { return false; } _Key ckey = null; if (obj instanceof _Key) { ckey = (_Key) obj; } else { return false; } if (ckey.cname.equals(this.cname) && id == ckey.id) { return true; } return false; } @Override public String toString() { return cname + DELIM + id; } /* * id = 533411011589881 - 15 or 16 chars depending on the OS * * @see java.lang.Object#hashCode() */ @Override public int hashCode() { return new Integer(cname.hashCode() * new Integer(new Long(id).toString().substring(9))).intValue(); } static long getNonRepeatableNumber() { // Keep the random number 2 digits wide to simplify the task Id parsing // if required // The random value is concatenated with time value not added to it and // then converted to long. return Long.parseLong(Util.getRandomNumber(10, 99) + "" + System.nanoTime()); } public static _Key parse(String key) { if (Util.isEmpty(key)) { throw new NullPointerException("Key must be provided!"); } if (!key.contains(DELIM)) { throw new InvalidKeyException("Key " + key + " is not a valid task id"); } int idx = key.indexOf(DELIM); if (idx < 0) { throw new InvalidKeyException("Key " + key + " is not a valid task id"); } String runnerName = key.substring(0, idx); long id = 0; Class runner = null; try { runner = Class.forName(runnerName); id = Long.parseLong(key.substring(idx + 1)); } catch (NumberFormatException e) { throw new InvalidKeyException("Key " + key + " is not a valid task id"); } catch (ClassNotFoundException e) { throw new InvalidKeyException("Key " + key + " is not a valid task id"); } return new _Key(runner, id); } }