org.hibernate.cfg.binder.BinderUtils Maven / Gradle / Ivy
package org.hibernate.cfg.binder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.hibernate.internal.util.collections.JoinedIterator;
import org.hibernate.mapping.Component;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;
public class BinderUtils {
public static String makeUnique(
Iterator props,
String originalPropertyName) {
int cnt = 0;
String propertyName = originalPropertyName;
Set uniqueNames = new HashSet();
while ( props.hasNext() ) {
Property element = props.next();
uniqueNames.add( element.getName() );
}
while( uniqueNames.contains(propertyName) ) {
cnt++;
propertyName = originalPropertyName + "_" + cnt;
}
return propertyName;
}
@SuppressWarnings("unchecked")
public static String makeUnique(PersistentClass clazz, String propertyName) {
List list = new ArrayList();
if( clazz.hasIdentifierProperty() ) {
list.add( clazz.getIdentifierProperty() );
}
if( clazz.isVersioned() ) {
list.add( clazz.getVersion() );
}
Iterator propertyClosureIterator = clazz.getPropertyClosureIterator();
JoinedIterator iterator =
new JoinedIterator(
list.iterator(),
propertyClosureIterator);
return BinderUtils.makeUnique(iterator, propertyName);
}
@SuppressWarnings("unchecked")
public static String makeUnique(Component clazz, String propertyName) {
return BinderUtils.makeUnique(clazz.getPropertyIterator(), propertyName);
}
public static Map,?> safeMap(Map,?> map) {
if(map==null) {
return Collections.emptyMap();
} else {
return map;
}
}
}