r2798 - trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence
Author: tchemit Date: 2013-08-14 15:09:05 +0200 (Wed, 14 Aug 2013) New Revision: 2798 Url: http://nuiton.org/projects/topia/repository/revisions/2798 Log: fixes #2825: Improve TopiaIdFactory Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/DefaultTopiaIdFactory.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/LegacyTopiaIdFactory.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaIdFactory.java Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/DefaultTopiaIdFactory.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/DefaultTopiaIdFactory.java 2013-08-08 15:27:29 UTC (rev 2797) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/DefaultTopiaIdFactory.java 2013-08-14 13:09:05 UTC (rev 2798) @@ -24,12 +24,10 @@ * #L% */ -import java.util.UUID; - +import org.apache.commons.lang3.StringUtils; import org.nuiton.topia.TopiaException; -import com.google.common.base.Splitter; -import com.google.common.collect.Iterables; +import java.util.UUID; /** * Default implementation of {@link TopiaIdFactory}. Generates an FQN followed @@ -46,13 +44,14 @@ throw new IllegalArgumentException( "Only interface is permit to create id: " + entityClass); } - String result = entityClass.getName() + '_' + UUID.randomUUID().toString(); + String result = entityClass.getName() + getSeparator() + UUID.randomUUID().toString(); return result; } @Override public <E extends TopiaEntity> Class<E> getClassName(String topiaId) { - String className = Iterables.get(Splitter.on('_').split(topiaId), 0); +// String className = Iterables.get(Splitter.on(getSeparator()).split(topiaId), 0); + String className = StringUtils.substringBefore(topiaId, getSeparator()); try { Class<E> entityClass = (Class<E>) Class.forName(className); return entityClass; @@ -61,4 +60,14 @@ } } + @Override + public String getSeparator() { + return "_"; + } + + @Override + public String getRandomPart(String topiaId) { + return StringUtils.substringBefore(topiaId, getSeparator()); + } + } Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/LegacyTopiaIdFactory.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/LegacyTopiaIdFactory.java 2013-08-08 15:27:29 UTC (rev 2797) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/LegacyTopiaIdFactory.java 2013-08-14 13:09:05 UTC (rev 2798) @@ -24,6 +24,7 @@ * #L% */ +import org.apache.commons.lang3.StringUtils; import org.nuiton.topia.TopiaNotFoundException; /** @@ -45,7 +46,7 @@ while (Double.toString(random).contains("E-")) { random = Math.random(); } - return entityClass.getName() + '#' + System.currentTimeMillis() + '#' + random; + return entityClass.getName() + getSeparator() + System.currentTimeMillis() + '#' + random; } /** @@ -57,18 +58,27 @@ */ @Override public <E extends TopiaEntity> Class<E> getClassName(String topiaId) { - String className = ""; - int i = topiaId.indexOf('#'); - if (i > 0) { - className = topiaId.substring(0, i); - } + String className = StringUtils.substringBefore(topiaId, getSeparator()); +// int i = topiaId.indexOf('#'); +// if (i > 0) { +// className = topiaId.substring(0, i); +// } try { Class result = Class.forName(className); return result; } catch (ClassNotFoundException eee) { throw new TopiaNotFoundException("Can't find class for " + topiaId, - eee); + eee); } } + @Override + public String getSeparator() { + return "#"; + } + + @Override + public String getRandomPart(String topiaId) { + return StringUtils.substringBefore(topiaId, getSeparator()); + } } Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaIdFactory.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaIdFactory.java 2013-08-08 15:27:29 UTC (rev 2797) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaIdFactory.java 2013-08-14 13:09:05 UTC (rev 2798) @@ -26,16 +26,40 @@ /** * This API represents an ID generation strategy + * <p/> + * // TODO AThimel 20/07/13 Javadoc (finish this javadoc) * - * @author bleny + * @author bleny <leny@codelutin.com/> + * @author tchemit <chemit@codelutin.com/> * @since 3.0 */ public interface TopiaIdFactory { - // TODO AThimel 20/07/13 Javadoc + /** + * Generates a new topia id for the given entity type and the given entity. + * + * @param entityClass type of entity (must be a not null interface) + * @param topiaEntity the entity on which we want to generate the id (must be not null) + * @param <E> type of entity + * @return the new topia id for the given entity + */ <E extends TopiaEntity> String newTopiaId(Class<E> entityClass, TopiaEntity topiaEntity); - // TODO AThimel 20/07/13 Javadoc + /** + * @param topiaId the topia id to inspect + * @param <E> type of the entity + * @return the fqn part of the topia id + */ <E extends TopiaEntity> Class<E> getClassName(String topiaId); + /** + * @param topiaId the topia id to inspect + * @return the random poart of the topia id. + */ + String getRandomPart(String topiaId); + + /** + * @return the separator between the fqn and the random part of any topia id. + */ + String getSeparator(); }
participants (1)
-
tchemit@users.nuiton.org