Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

FODBIndexFactory.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  2004 Creation P.Delrieu
00026  *  2004 Modified by Romain Beaugrand
00027  * 
00028  */
00029 package org.openmobileis.database.fastobjectdb.db.index;
00030 
00031 import java.lang.reflect.*;
00032 
00033 import org.openmobileis.common.util.exception.BadDataFormatException;
00034 import org.openmobileis.database.fastobjectdb.FODBIndexDescriptor;
00035 import org.openmobileis.database.fastobjectdb.FODBIntIndexDescriptor;
00036 import org.openmobileis.database.fastobjectdb.FODBLongIndexDescriptor;
00037 import org.openmobileis.database.fastobjectdb.FODBStringIndexDescriptor;
00038 import org.openmobileis.database.fastobjectdb.db.exception.FODBException;
00039 import org.openmobileis.database.fastobjectdb.db.store.FODBCollectionIndexFile;
00040 import org.openmobileis.database.fastobjectdb.db.util.FODBUtils;
00041 
00046 public final class FODBIndexFactory {
00047         public static final int UNIQUE = 1;
00048         public static final int MULTIPLE = 2;
00049         
00050         private static FODBIndexFactory factory;
00051         
00052         public FODBIndexFactory() {
00053         }
00054         
00055         public static FODBIndexFactory getFactory() {
00056                 if (factory == null)  {
00057                         synchronized(FODBIndexFactory.class) {
00058                                 if (factory == null)  {
00059                                         factory = new FODBIndexFactory();
00060                                 }
00061                         }
00062                 }
00063                 return factory;
00064         }
00065 
00066         protected boolean isValidKeyType(Class keyType,Class indexType){
00067                 boolean valid=false;
00068                 if(keyType==indexType)
00069                         valid=true;
00070                 else if(keyType.getComponentType()==indexType)
00071                         valid=true;
00072                 return valid;
00073         }
00074         public FODBIndex createIndex(FODBIndexDescriptor descriptor, Class objectType, FODBCollectionIndexFile cFile) throws BadDataFormatException, FODBException {
00075                 Class keyType;
00076                 AccessibleObject accessObj = FODBUtils.getMember(objectType, descriptor.getMemberName());
00077 
00078                 if (accessObj == null) {
00079                         throw new BadDataFormatException("Method or Field does not exist");
00080                 }
00081                 
00082                 if (accessObj instanceof Method) {
00083                         keyType = ((Method)accessObj).getReturnType();
00084                 }
00085                 else { // accessObj instanceof Field
00086                         keyType = ((Field)accessObj).getType();
00087                 }
00088                 
00089                 if (descriptor instanceof FODBStringIndexDescriptor) {
00090                         if (!isValidKeyType(keyType,String.class)) {
00091                                 throw new BadDataFormatException("Invalid Method or Field for type String");
00092                         }
00093                         FODBIndex newIndex;
00094                         if (descriptor.getType() == UNIQUE) {
00095                                 newIndex = new FODBUniqueStringIndex((FODBStringIndexDescriptor)descriptor, cFile, accessObj);
00096                                 return newIndex;
00097                         } else {
00098                                 newIndex = new FODBMultipleStringIndex((FODBStringIndexDescriptor)descriptor, cFile, accessObj);
00099                                 return newIndex;
00100                         }
00101                 }
00102                 else if (descriptor instanceof FODBIntIndexDescriptor) {
00103                         if (!isValidKeyType(keyType,int.class)) {
00104                                 throw new BadDataFormatException("Invalid Method or Field for type int");
00105                         }
00106                         FODBIndex newIndex;
00107                         if (descriptor.getType() == UNIQUE) {
00108                                 newIndex = new FODBUniqueIntIndex((FODBIntIndexDescriptor)descriptor, cFile, accessObj);
00109                                 return newIndex;
00110                         } else {
00111                                 newIndex = new FODBMultipleIntIndex((FODBIntIndexDescriptor)descriptor, cFile, accessObj);
00112                                 return newIndex;
00113                         }
00114                 }
00115                 else if (descriptor instanceof FODBLongIndexDescriptor) {
00116                         if (!isValidKeyType(keyType,long.class)) {
00117                                 throw new BadDataFormatException("Invalid Method or Field for type long");
00118                         }
00119                         FODBIndex newIndex;
00120                         if (descriptor.getType() == UNIQUE) {
00121                                 newIndex = new FODBUniqueLongIndex((FODBLongIndexDescriptor)descriptor, cFile, accessObj);
00122                                 return newIndex;
00123                         } else {
00124                                 newIndex = new FODBMultipleLongIndex((FODBLongIndexDescriptor)descriptor, cFile, accessObj);
00125                                 return newIndex;
00126                         }
00127                 } else {
00128                         throw new BadDataFormatException("Unrecognised IndexDescriptor");
00129                 }
00130 
00131         }
00132         
00133         public FODBIndex createIndex(FODBIndexHeader header, Class objectType, FODBCollectionIndexFile cFile) throws BadDataFormatException, FODBException {
00134                 Class keyType;
00135                 AccessibleObject accessObj = FODBUtils.getMember(objectType, header.getMemberName());
00136                 
00137                 if (accessObj == null) {
00138                         throw new BadDataFormatException("Method or Field does not exist");
00139                 }
00140                 
00141                 if (accessObj instanceof Method) {
00142                         keyType = ((Method)accessObj).getReturnType();
00143                 }
00144                 else { // accessObj instanceof Field
00145                         keyType = ((Field)accessObj).getType();
00146                 }
00147 
00148                 //if (keyType == String.class) {
00149                 if (isValidKeyType(keyType,String.class)) {
00150                         FODBIndex newIndex;
00151                         if (header.type == UNIQUE) {
00152                                 newIndex = new FODBUniqueStringIndex(header, cFile, accessObj);
00153                                 return newIndex;
00154                         } else {
00155                                 newIndex = new FODBMultipleStringIndex(header, cFile, accessObj);
00156                                 return newIndex;
00157                         }
00158                 }
00159                 else if (isValidKeyType(keyType,int.class)) {
00160                         FODBIndex newIndex;
00161                         if (header.type == UNIQUE) {
00162                                 newIndex = new FODBUniqueIntIndex(header, cFile, accessObj);
00163                                 return newIndex;
00164                         } else {
00165                                 newIndex = new FODBMultipleIntIndex(header, cFile, accessObj);
00166                                 return newIndex;
00167                         }
00168                 }
00169                 else if (isValidKeyType(keyType,long.class)) {
00170                         FODBIndex newIndex;
00171                         if (header.type == UNIQUE) {
00172                                 newIndex = new FODBUniqueLongIndex(header, cFile, accessObj);
00173                                 return newIndex;
00174                         } else {
00175                                 newIndex = new FODBMultipleLongIndex(header, cFile, accessObj);
00176                                 return newIndex;
00177                         }
00178                 } else {
00179                         throw new BadDataFormatException("Unrecognised key type for creation of index");
00180                 }
00181 
00182         }
00183 }

Generated on Wed Dec 14 21:05:33 2005 for OpenMobileIS by  doxygen 1.4.4