1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| public class Exemple {
/** Interface permettant de récuperer la valeur d'un attribut d'un objet
* E = Type de l'objet sur lequel on récupere un attribut
* A = ype de l'attribut
*/
public interface AttributeFetcher<E,A> {
public A getAttribute(E element);
}
/** Comparator de collections permettant de trier sur la frequence d'apparition d'un attribut au sein des objets
* @param <E> Type des objets triés au sein de la liste
* @param <A> Type de l'attribut supporter l'analyse de la frequence d'apparition
*/
public static class FrequencyAttributeComparator<E,A> implements Comparator<E> {
private AttributeFetcher<E,A> fetcher = null;
private Map<A,Integer> distribution = null;
/** Constructeur
* @param fetcher Implémentation deleguant l'intelligence de recuperer l'attribut d'un objet
* @param list List utilisée pour calculer les frequences d'apparition
*/
public FrequencyAttributeComparator(AttributeFetcher<E,A> fetcher , Collection<E> list ) {
this.fetcher = fetcher;
/** Création de la distribution
*/
this.distribution = new HashMap<A,Integer>();
for(E element : list ) {
A attribute = element == null ? null : this.fetcher.getAttribute(element);
Integer count = this.distribution.get(attribute);
if( count == null ) count = 1;
else count = count + 1;
this.distribution.put(attribute,count);
}
}
/** Tri
*/
public int compare(E o1, E o2) {
if( o1 == o2 ) return 0;
if( o1 == null ) return 1;
if( o2 == null ) return 1;
A a1 = fetcher.getAttribute(o1);
A a2 = fetcher.getAttribute(o2);
Integer i1 = this.distribution.get(a1);
Integer i2 = this.distribution.get(a2);
if( i1 == null || i2 == null )
throw new IllegalStateException("Undistributed attribute value");
return i1.compareTo(i2) * -1;
}
}
/** Classe pour faire un exemple
*/
public static class Car {
Color color = null;
boolean withClimatisation = false;
public Car(Color color , boolean withClimatisation ) {
this.color = color;
this.withClimatisation = withClimatisation;
}
public String toString() {
return "Car[color=" + color + ", climatisation=" + withClimatisation + "]";
}
}
/** Exemple
*/
public static void main(String[] args) {
List<Car> listATrier = new ArrayList<Car>();
listATrier.add( new Car( Color.RED , true) );
listATrier.add( new Car( Color.BLUE , true ) );
listATrier.add( new Car( Color.WHITE , false ) );
listATrier.add( new Car( Color.BLACK , false ) );
listATrier.add( new Car( Color.BLACK , true ) );
listATrier.add( new Car( Color.RED , true ) );
listATrier.add( new Car( Color.YELLOW , false ) );
listATrier.add( new Car( Color.RED , true ) );
AttributeFetcher<Car,Color> colorFetcher = new AttributeFetcher<Car, Color>() {
public Color getAttribute(Car element) {
if( element == null ) return null;
return element.color;
}
};
Comparator<Car> comp = new FrequencyAttributeComparator<Car,Color>( colorFetcher , listATrier );
Collections.sort( listATrier , comp );
for(Car car : listATrier ) {
System.out.println( car );
}
}
} |
Partager