1 package org.hibernate.transform; 3 4 import java.util.ArrayList ; 5 import java.util.HashSet ; 6 import java.util.List ; 7 import java.util.Set ; 8 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 12 15 public class DistinctRootEntityResultTransformer implements ResultTransformer { 16 17 private static final Log log = LogFactory.getLog(DistinctRootEntityResultTransformer.class); 18 19 static final class Identity { 20 final Object entity; 21 Identity(Object entity) { 22 this.entity = entity; 23 } 24 public boolean equals(Object other) { 25 Identity that = (Identity) other; 26 return entity==that.entity; 27 } 28 public int hashCode() { 29 return System.identityHashCode(entity); 30 } 31 } 32 33 public Object transformTuple(Object [] tuple, String [] aliases) { 34 return tuple[ tuple.length-1 ]; 35 } 36 37 public List transformList(List list) { 38 List result = new ArrayList ( list.size() ); 39 Set distinct = new HashSet (); 40 for ( int i=0; i<list.size(); i++ ) { 41 Object entity = list.get(i); 42 if ( distinct.add( new Identity(entity) ) ) { 43 result.add(entity); 44 } 45 } 46 if ( log.isDebugEnabled() ) log.debug( 47 "transformed: " + 48 list.size() + " rows to: " + 49 result.size() + " distinct results" 50 ); 51 return result; 52 } 53 54 } 55 | Popular Tags |