![JAR search and dependency download from the Maven repository](/logo.png)
org.nuiton.topia.persistence.TopiaIdFactorySupport Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of persistence Show documentation
Show all versions of persistence Show documentation
ToPIA extension persistence module
package org.nuiton.topia.persistence;
/*-
* #%L
* ToPIA Extension :: persistence
* %%
* Copyright (C) 2018 - 2022 Ultreia.io
* %%
* 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 3 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, see
* .
* #L%
*/
import io.ultreia.java4all.lang.Objects2;
import io.ultreia.java4all.lang.Strings;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* Created on 08/01/2022.
*
* @author Tony Chemit - [email protected]
* @since 1.60
*/
public class TopiaIdFactorySupport implements TopiaIdFactory {
private static final Logger log = LogManager.getLogger(TopiaIdFactorySupport.class);
private static final long serialVersionUID = 1L;
private final String packagePrefix;
private final String idPrefix;
public TopiaIdFactorySupport(String idPrefix, String packagePrefix) {
this.idPrefix = idPrefix;
this.packagePrefix = packagePrefix;
}
public String getPackagePrefix() {
return packagePrefix;
}
public String getIdPrefix() {
return idPrefix;
}
public String newTopiaId(Class entityType) {
return newTopiaId(entityType, getRandom() + "");
}
public String newTopiaId(String entityType) {
return getIdFromClass(entityType) + getSeparator() + System.currentTimeMillis() + '#' + getRandom();
}
public String newTopiaId(String entityType, String randomPart) {
return getIdFromClass(entityType) + getSeparator() + System.currentTimeMillis() + '#' + randomPart;
}
@Override
public String newTopiaId(Class entityClass, TopiaEntity topiaEntity) {
if (!entityClass.isInterface()) {
throw new IllegalArgumentException("Only interface is permit to create id: " + entityClass);
}
return getIdFromClass(entityClass.getName()) + getSeparator() + System.currentTimeMillis() + '#' + getRandom();
}
public String newTopiaId(Class entityClass, String idPrefix, String randomPart) {
return getIdFromClass(entityClass.getName()) + getSeparator() + idPrefix + '#' + randomPart;
}
@Override
public String newTopiaId(Class entityClass, String randomPart) {
return getIdFromClass(entityClass.getName()) + getSeparator() + System.currentTimeMillis() + '#' + randomPart;
}
@Override
public Class getClassName(String topiaId) {
String className = getClassName0(topiaId);
return Objects2.forName(className);
}
@Override
public String getRandomPart(String topiaId) {
return Strings.substringAfter(topiaId, getSeparator());
}
@Override
public String getSeparator() {
return "#";
}
@Override
public boolean isTopiaId(String str) {
if (str != null) {
String className = getClassName0(str);
try {
Class.forName(className);
return true;
// for (int index = 1; index < split.length; index++) {
// isTopiaId &= Strings.isNotEmpty(split[index]);
// }
} catch (ClassNotFoundException eee) {
// nothing to do, just return false
log.debug(eee);
}
}
return false;
}
private String getClassName0(String str) {
return getClassFromId(Strings.substringBefore(str, getSeparator()));
}
private double getRandom() {
double random = Math.random();
while (Double.toString(random).contains("E-")) {
random = Math.random();
}
return random;
}
private String getIdFromClass(String entityClassName) {
return getIdPrefix() + Strings.removeStart(entityClassName, getPackagePrefix());
}
private String getClassFromId(String id) {
return packagePrefix + Strings.removeStart(id, idPrefix);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy