1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.mavibot.btree.serializer;
21
22
23 import java.lang.reflect.Array;
24 import java.lang.reflect.GenericArrayType;
25 import java.lang.reflect.ParameterizedType;
26 import java.lang.reflect.Type;
27 import java.util.Comparator;
28
29
30
31
32
33
34
35
36
37 public abstract class AbstractElementSerializer<T> implements ElementSerializer<T>
38 {
39
40 private final Comparator<T> comparator;
41
42
43 private Class<?> type;
44
45
46
47
48
49 public AbstractElementSerializer( Comparator<T> comparator )
50 {
51 this.comparator = comparator;
52
53
54 Class<?> comparatorClass = comparator.getClass();
55 Type[] types = comparatorClass.getGenericInterfaces();
56
57 if ( types[0] instanceof Class )
58 {
59 type = ( Class<?> ) types[0];
60 }
61 else
62 {
63 Type[] argumentTypes = ( ( ParameterizedType ) types[0] ).getActualTypeArguments();
64
65 if ( ( argumentTypes != null ) && ( argumentTypes.length > 0 ) )
66 {
67 if ( argumentTypes[0] instanceof Class<?> )
68 {
69 type = ( Class<?> ) argumentTypes[0];
70 }
71 else if ( argumentTypes[0] instanceof GenericArrayType )
72 {
73 Class<?> clazz = ( Class<?> ) ( ( GenericArrayType ) argumentTypes[0] ).getGenericComponentType();
74
75 type = Array.newInstance( clazz, 0 ).getClass();
76 }
77 }
78 }
79 }
80
81
82
83
84
85 public int compare( T type1, T type2 )
86 {
87 return comparator.compare( type1, type2 );
88 }
89
90
91
92
93
94 @Override
95 public Comparator<T> getComparator()
96 {
97 return comparator;
98 }
99
100
101
102
103
104 @Override
105 public Class<?> getType()
106 {
107 return type;
108 }
109 }