001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020package org.apache.directory.mavibot.btree.serializer; 021 022 023import java.lang.reflect.Array; 024import java.lang.reflect.GenericArrayType; 025import java.lang.reflect.ParameterizedType; 026import java.lang.reflect.Type; 027import java.util.Comparator; 028 029 030/** 031 * An abstract ElementSerializer that implements comon methods 032 * 033 * @param <T> The type for the element to serialize and compare 034 * 035 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 036 */ 037public abstract class AbstractElementSerializer<T> implements ElementSerializer<T> 038{ 039 /** The associated comparator */ 040 private final Comparator<T> comparator; 041 042 /** The type which is being serialized */ 043 private Class<?> type; 044 045 046 /** 047 * Create a new instance of Serializer 048 */ 049 public AbstractElementSerializer( Comparator<T> comparator ) 050 { 051 this.comparator = comparator; 052 053 // We will extract the Type to use for values, using the comparator for that 054 Class<?> comparatorClass = comparator.getClass(); 055 Type[] types = comparatorClass.getGenericInterfaces(); 056 057 if ( types[0] instanceof Class ) 058 { 059 type = ( Class<?> ) types[0]; 060 } 061 else 062 { 063 Type[] argumentTypes = ( ( ParameterizedType ) types[0] ).getActualTypeArguments(); 064 065 if ( ( argumentTypes != null ) && ( argumentTypes.length > 0 ) ) 066 { 067 if ( argumentTypes[0] instanceof Class<?> ) 068 { 069 type = ( Class<?> ) argumentTypes[0]; 070 } 071 else if ( argumentTypes[0] instanceof GenericArrayType ) 072 { 073 Class<?> clazz = ( Class<?> ) ( ( GenericArrayType ) argumentTypes[0] ).getGenericComponentType(); 074 075 type = Array.newInstance( clazz, 0 ).getClass(); 076 } 077 } 078 } 079 } 080 081 082 /** 083 * {@inheritDoc} 084 */ 085 public int compare( T type1, T type2 ) 086 { 087 return comparator.compare( type1, type2 ); 088 } 089 090 091 /** 092 * {@inheritDoc} 093 */ 094 @Override 095 public Comparator<T> getComparator() 096 { 097 return comparator; 098 } 099 100 101 /** 102 * {@inheritDoc} 103 */ 104 @Override 105 public Class<?> getType() 106 { 107 return type; 108 } 109}