View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
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   * An abstract ElementSerializer that implements comon methods
32   *
33   * @param <T> The type for the element to serialize and compare
34   *
35   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36   */
37  public abstract class AbstractElementSerializer<T> implements ElementSerializer<T>
38  {
39      /** The associated comparator */
40      private final Comparator<T> comparator;
41  
42      /** The type which is being serialized */
43      private Class<?> type;
44  
45  
46      /**
47       * Create a new instance of Serializer
48       */
49      public AbstractElementSerializer( Comparator<T> comparator )
50      {
51          this.comparator = comparator;
52  
53          // We will extract the Type to use for values, using the comparator for that
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       * {@inheritDoc}
84       */
85      public int compare( T type1, T type2 )
86      {
87          return comparator.compare( type1, type2 );
88      }
89  
90  
91      /**
92       * {@inheritDoc}
93       */
94      @Override
95      public Comparator<T> getComparator()
96      {
97          return comparator;
98      }
99  
100 
101     /**
102      * {@inheritDoc}
103      */
104     @Override
105     public Class<?> getType()
106     {
107         return type;
108     }
109 }