r2501 - in trunk/topia-persistence/src/main/java/org/nuiton/topia: framework persistence
Author: tchemit Date: 2012-05-23 15:13:13 +0200 (Wed, 23 May 2012) New Revision: 2501 Url: http://nuiton.org/repositories/revision/topia/2501 Log: introduces new classes Added: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaFunctions.java trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaPredicates.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaIdUtil.java Added: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaFunctions.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaFunctions.java (rev 0) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaFunctions.java 2012-05-23 13:13:13 UTC (rev 2501) @@ -0,0 +1,20 @@ +package org.nuiton.topia.framework; + +import com.google.common.base.Function; +import org.nuiton.topia.persistence.TopiaEntity; + +/** + * All {@link Function} used by ToPIA. + * + * @author tchemit <chemit@codelutin.com> + * @since 3.0 + */ +public class TopiaFunctions { + + public static final Function<TopiaEntity, String> GET_TOPIA_ID = new Function<TopiaEntity, String>() { + @Override + public String apply(TopiaEntity input) { + return input.getTopiaId(); + } + }; +} Added: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaPredicates.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaPredicates.java (rev 0) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaPredicates.java 2012-05-23 13:13:13 UTC (rev 2501) @@ -0,0 +1,22 @@ +package org.nuiton.topia.framework; + +import com.google.common.base.Predicate; +import org.nuiton.topia.persistence.TopiaEntity; + +/** + * Useful {@link Predicate} used by ToPIA. + * + * @author tchemit <chemit@codelutin.com> + * @since 3.0 + */ +public class TopiaPredicates { + + public static <E extends TopiaEntity> Predicate<E> newTopiaIdEquals(final String topiaId) { + return new Predicate<E>() { + @Override + public boolean apply(E input) { + return topiaId.equals(TopiaFunctions.GET_TOPIA_ID.apply(input)); + } + }; + } +} Added: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaIdUtil.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaIdUtil.java (rev 0) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaIdUtil.java 2012-05-23 13:13:13 UTC (rev 2501) @@ -0,0 +1,147 @@ +package org.nuiton.topia.persistence; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.topia.TopiaNotFoundException; + +import java.util.StringTokenizer; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.regex.Pattern; + +/** + * Useful methods around the {@code topia-id}. + * + * @author tchemit <chemit@codelutin.com> + * @since 3.0 + */ +public class TopiaIdUtil { + + /** Logger. */ + private static final Log log = LogFactory.getLog(TopiaIdUtil.class); + + protected TopiaIdUtil() { + // util class should not be instanciated + } + + /** + * Cree un topiaId pour une certaine classe + * + * @param clazz + * @return a generated topiaId + */ + 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. + * + * @param topiaId + * @return class + * @throws TopiaNotFoundException if could not find + */ + 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); + } + } + + /** + * Return class name id topiaId is id, and empty string if topiaId is not an + * id. + * + * @param topiaId + * @return class name + */ + public static String getClassNameAsString(String topiaId) { + String result = ""; + int i = topiaId.indexOf('#'); + if (i > 0) { + result = topiaId.substring(0, i); + } + 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. + * + * @param topiaId + * @return is valid topiaId + */ + public static boolean isValidId(String topiaId) { + try { + if (topiaId.matches(".*?#[0-9]+#[0-9.]+")) { + getClassName(topiaId); + return true; + } + return false; + } catch (Exception eee) { + if (log.isWarnEnabled()) { + log.warn("Error during verfication of topiaId", eee); + } + return false; + } + } + + /** + * Compute a regex pattern given a format string. + * <p/> + * A {@link String#format(String, Object...)} will be apply to + * <code>format</code>, with for parameters the list of <code>klass</code> + * transformed in topia pattern via method {@link #getTopiaIdPattern(Class)} + * ready to be capture (enclosed by ()). + * + * @param format the format + * @param classes the list of class to use + * @return the pattern computed + */ + public static Pattern getTopiaPattern(String format, + Class<? extends TopiaEntity>... classes) { + String[] entityPatterns = new String[classes.length]; + for (int i = 0; i < classes.length; i++) { + Class<? extends TopiaEntity> aClass = classes[i]; + entityPatterns[i] = "(" + getTopiaIdPattern(aClass) + ")"; + } + String s = String.format(format, (Object[]) entityPatterns); + if (log.isDebugEnabled()) { + log.debug(s); + } + return Pattern.compile(s); + } + + /** + * Compute the pattern to be used to capture a topia id for a given entity + * class. + * + * @param klass the entity class + * @return the pattern to capture a topia id for the given entity class. + */ + public static String getTopiaIdPattern(Class<? extends TopiaEntity> klass) { + StringBuilder buffer = new StringBuilder(); + StringTokenizer stk = new StringTokenizer(klass.getName(), "."); + while (stk.hasMoreTokens()) { + buffer.append("\\.").append(stk.nextToken()); + } + buffer.append("#(?:\\d+?)#(?:\\d+)\\.(?:\\d+)"); + return buffer.substring(2); + } + +}
participants (1)
-
tchemit@users.nuiton.org