package com.logntw.eva.sort.imp; import java.util.Arrays; import java.util.Comparator; import junit.framework.Assert; import org.junit.Test; import com.logntw.eva.anno.bean.Dimension; import com.logntw.eva.anno.struct.FieldSeq; import com.logntw.eva.sort.ComparatorFactory; import com.logntw.eva.sort.SortHelper; import com.logntw.eva.struct.StructMapper; import com.logntw.eva.struct.defo.DefoStructMappers; public class ComparatorAbstractFactoryTest2 { // 3 * 4 + 1= 13 @com.logntw.eva.anno.struct.Struct public static interface Y { @FieldSeq(1) byte getA(); void setA(byte a); @FieldSeq(2) @Dimension(3) int[] getB(); void setB(int[] b); } // 1 + 13 + 1 = 15 @com.logntw.eva.anno.struct.Struct public static interface X { @FieldSeq(1) byte getA(); void setA(byte a); @FieldSeq(2) Y getB(); void setB(Y b); @FieldSeq(3) byte getC(); void setC(byte c); } @Test public void testComparatorAbstractFactory() throws Exception { StructMapper<X> g = DefoStructMappers.getMapper(X.class); X x1 = g.createStruct(); X x2 = g.createStruct(); X x3 = g.createStruct(); x1.setA((byte)1); x1.getB().setA((byte)1); x1.setC((byte)2); x2.setA((byte)2); x2.getB().setA((byte)1); x2.setC((byte)1); x3.setA((byte)1); x3.getB().setA((byte)2); x3.setC((byte)1); ComparatorFactory<X> cf = SortHelper.getComparatorFactory(X.class); Comparator<X> c1 = cf.getComparator("a", "b.a", "c"); Comparator<X> c2 = cf.getComparator("b.a", "c", "a"); Comparator<X> c3 = cf.getComparator("c", "a", "b.a"); X[] data1 = new X[]{x1,x2,x3}; X[] data2 = new X[]{x1,x2,x3}; X[] data3 = new X[]{x1,x2,x3}; Arrays.sort(data1, c1); Arrays.sort(data2, c2); Arrays.sort(data3, c3); Assert.assertEquals(data1[0], x1); Assert.assertEquals(data1[1], x3); Assert.assertEquals(data1[2], x2); Assert.assertEquals(data2[0], x2); Assert.assertEquals(data2[1], x1); Assert.assertEquals(data2[2], x3); Assert.assertEquals(data3[0], x3); Assert.assertEquals(data3[1], x2); Assert.assertEquals(data3[2], x1); } }