cn.featherfly.hammer.dml.AliasManager Maven / Gradle / Ivy
package cn.featherfly.hammer.dml;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import cn.featherfly.hammer.HammerException;
/**
*
* AliasManager
*
*
* @author zhongj
*/
public class AliasManager {
private Map nameAlias = new LinkedHashMap<>();
private Map nameAliasSize = new ConcurrentHashMap<>(0);
public static String generateAlias(String name) {
return "_" + name;
}
public String put(String name) {
Integer size = nameAliasSize.get(name);
if (size == null) {
size = 0;
}
String alias = generateAlias(name) + size;
put(name, alias, size);
return alias;
}
public AliasManager put(String name, String alias) {
Integer size = nameAliasSize.get(name);
if (size == null) {
size = 0;
}
return put(name, alias, size);
}
private AliasManager put(String name, String alias, Integer size) {
size++;
nameAliasSize.put(name, size);
nameAlias.put(alias, name);
return this;
}
public String getName(String alias) {
return nameAlias.get(alias);
}
public String getAlias(int index) {
if (index > nameAlias.size()) {
throw new HammerException(index + " > name alias size " + nameAlias.size());
}
int size = 0;
for (Entry entry : nameAlias.entrySet()) {
if (size == index) {
return entry.getKey();
}
size++;
}
return null;
}
public String getAlias(String name) {
for (Entry entry : nameAlias.entrySet()) {
if (entry.getValue().equals(name)) {
return entry.getKey();
}
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy