Index: topia2/src/java/org/codelutin/topia/persistence/TopiaId.java diff -u /dev/null topia2/src/java/org/codelutin/topia/persistence/TopiaId.java:1.1 --- /dev/null Fri Jan 13 15:45:14 2006 +++ topia2/src/java/org/codelutin/topia/persistence/TopiaId.java Fri Jan 13 15:45:09 2006 @@ -0,0 +1,121 @@ +/* *##% + * Copyright (C) 2002, 2003 Code Lutin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program 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 + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +/* * + * TopiaId.java + * + * Created: 6 juil. 2004 + * + * @author Benjamin Poussin + * Copyright Code Lutin + * @version $Revision: 1.1 $ + * + * Mise a jour: $Date: 2006/01/13 15:45:09 $ + * par : $Author: bpoussin $ + */ + +package org.codelutin.topia.persistence; + +import java.io.Serializable; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.codelutin.topia.TopiaNotFoundException; + +/** +* Classe representant un Id, utilisable par JDO. Cette classe contient aussi un +* ensemble de methode static utile pour la manipulation des topiaId +*/ +public class TopiaId implements Serializable { // TopiaId + + public String topiaId = null; + + public TopiaId(){} + public TopiaId(String topiaId){ + this.topiaId = topiaId; + } + + public int hashCode (){ + if(topiaId == null){ + Logger.getLogger(getClass().getName() + ".hashCode").log(Level.WARNING, "Use null topiaId", new Throwable()); + return 0; + } + return topiaId.hashCode(); + } + + public boolean equals(Object o){ + return topiaId.equals(o); + } + + public String toString(){ + return topiaId; + } + + /** + * Cree un topiaId pour une certaine classe + */ + public static String create(Class clazz){ + if(!clazz.isInterface()){ + throw new IllegalArgumentException("Only interface is permit to create id: " + clazz); + } + double random = Math.random(); + while (Double.toString(random).contains("E-")) { + random = Math.random(); + } + return clazz.getName() + "#" + System.currentTimeMillis() + "#" + random; + } + + /** + * Extrait la classe du topiaId + * @throws ClassNotFoundException + */ + public static Class getClassName(String topiaId) throws TopiaNotFoundException { + String classname = getClassNameAsString(topiaId); + try { + Class result = Class.forName(classname); + return result; + } catch (ClassNotFoundException eee) { + throw new TopiaNotFoundException("Can't find class for " + topiaId, eee); + } + } + + public static String getClassNameAsString(String topiaId){ + String result = topiaId.substring(0, topiaId.indexOf("#")); + return result; + } + + /** + * Verifie si l'id passé en paramètre est bien un Id topia, c-a-d si la + * forme est bien classname#timemillis#random et si le classname est + * celui d'une classe valide, c-a-d que le systeme arrive a trouver + */ + public static boolean isValidId(String topiaId){ + try{ + if(topiaId.matches(".*?#[0-9]+#[0-9.]+")){ + getClassName(topiaId); + return true; + } + return false; + }catch(Exception eee){ + Logger.getLogger(TopiaId.class.getName() + ".isValidId").log(Level.WARNING, "Error during verfication of topiaId", eee); + return false; + } + } + +} // TopiaId +