00001 /* 00002 * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM) 00003 * Copyright (C) 2004-2006 Philippe Delrieu 00004 * All rights reserved. 00005 * Contact: pdelrieu@openmobileis.org 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.query.soda; 00030 00031 import org.openmobileis.common.util.collection.Array; 00032 00039 public final class SodaLongIndexComparator extends SodaIndexComparator { 00040 private Array comparatorList; 00041 00042 class LongOperande { 00043 int comparator=0; 00044 long operande; 00045 int traversal; 00046 00047 LongOperande() { 00048 } 00049 } 00053 public SodaLongIndexComparator() { 00054 super(); 00055 comparatorList = new Array(); 00056 } 00057 00058 public boolean setOperand(Object ope) { 00059 if (ope instanceof Long) { 00060 LongOperande operand = new LongOperande(); 00061 operand.operande = ((Long)ope).longValue(); 00062 comparatorList.add(operand); 00063 return true; 00064 } 00065 return false; 00066 } 00067 00068 public void mergeIndexComporator(SodaIndexComparator index) { 00069 SodaLongIndexComparator newindex = (SodaLongIndexComparator)index; 00070 comparatorList.addArray(newindex.comparatorList); 00071 } 00072 00073 public boolean addComparator(int comparator) { 00074 LongOperande comp = (LongOperande) comparatorList.getLastAdded(); 00075 if (comp != null) { 00076 if (comp.comparator == BaseConstraint.COMPARATOR_NONE) { 00077 comp.comparator = comparator; 00078 comp.traversal = this.getTraversalWayWithComparator(comp.comparator); 00079 } else { 00080 //manage not or equals. 00081 if (comp.comparator == BaseConstraint.EQUALS) { 00082 if (comparator == BaseConstraint.SMALLER) { 00083 comp.comparator = BaseConstraint.EQUALSMALLER; 00084 } else if (comparator == BaseConstraint.GREATER) { 00085 comp.comparator = BaseConstraint.EQUALGREATER; 00086 } else if (comparator == BaseConstraint.NOT) { 00087 comp.comparator = BaseConstraint.NOT; 00088 } else if (comparator == BaseConstraint.EQUALS) { 00089 comp.comparator = BaseConstraint.EQUALS; 00090 } 00091 } else if (comp.comparator == BaseConstraint.NOT) { 00092 if (comparator == BaseConstraint.SMALLER) { 00093 comp.comparator = BaseConstraint.EQUALGREATER; 00094 } else if (comparator == BaseConstraint.GREATER) { 00095 comp.comparator = BaseConstraint.EQUALSMALLER; 00096 } else if (comparator == BaseConstraint.EQUALS) { 00097 comp.comparator = BaseConstraint.NOT; 00098 } else if (comparator == BaseConstraint.NOT) { 00099 comp.comparator = BaseConstraint.NOT; 00100 } 00101 } else if (comp.comparator == BaseConstraint.SMALLER) { 00102 if (comparator == BaseConstraint.NOT) { 00103 comp.comparator = BaseConstraint.EQUALGREATER; 00104 } else if (comparator == BaseConstraint.EQUALS) { 00105 comp.comparator = BaseConstraint.EQUALSMALLER; 00106 } else if (comparator == BaseConstraint.GREATER) { 00107 comp.comparator = BaseConstraint.EQUALS; 00108 } else if (comparator == BaseConstraint.SMALLER) { 00109 comp.comparator = BaseConstraint.SMALLER; 00110 } 00111 } else if (comp.comparator == BaseConstraint.GREATER) { 00112 if (comparator == BaseConstraint.NOT) { 00113 comp.comparator = BaseConstraint.EQUALSMALLER; 00114 } else if (comparator == BaseConstraint.EQUALS) { 00115 comp.comparator = BaseConstraint.EQUALGREATER; 00116 } else if (comparator == BaseConstraint.SMALLER) { 00117 comp.comparator = BaseConstraint.EQUALS; 00118 } else if (comparator == BaseConstraint.GREATER) { 00119 comp.comparator = BaseConstraint.GREATER; 00120 } 00121 } else { 00122 LongOperande operand = new LongOperande(); 00123 operand.operande = comp.operande; 00124 operand.comparator = comparator; 00125 operand.traversal = this.getTraversalWayWithComparator(operand.comparator); 00126 comparatorList.add(operand); 00127 } 00128 } 00129 return true; 00130 } 00131 return false; 00132 } 00133 00134 public int getSearchAlgo() { 00135 if (comparatorList.size() == 0) { 00136 return SodaIndexComparator.FULL_TRAVERSAL; 00137 } else { 00138 return ((LongOperande) comparatorList.get(0)).traversal; 00139 } 00140 } 00141 00146 public boolean isSelected(long searchInt) { 00147 boolean ret = true; 00148 int size = comparatorList.size(); 00149 for (int i = 0; i < size && ret; i++) { 00150 ret = this.selectFisrtOperande((LongOperande) comparatorList.get(i), searchInt); 00151 } 00152 return ret; 00153 } 00154 00159 public int compareTo(long searchInt) { 00160 if (comparatorList.size() == 0) { 00161 return -1; //force all tree traversal. 00162 } else { 00163 return this.compareFisrtOperande((LongOperande) comparatorList.get(0), searchInt); 00164 } 00165 } 00166 00167 private boolean selectFisrtOperande(LongOperande op, long key) { 00168 long test = key - op.operande; 00169 switch (op.comparator) { 00170 case BaseConstraint.EQUALS : 00171 return (test==0) ; 00172 case BaseConstraint.SMALLER : 00173 return (test <0) ; 00174 case BaseConstraint.EQUALSMALLER : 00175 return (test <=0) ; 00176 case BaseConstraint.GREATER : 00177 return (test >0) ; 00178 case BaseConstraint.EQUALGREATER : 00179 return (test >=0) ; 00180 case BaseConstraint.NOT : 00181 return (test != 0) ; 00182 } 00183 return true; 00184 } 00185 00186 private int compareFisrtOperande(LongOperande op, long key) { 00187 long res = 0; 00188 switch (op.comparator) { 00189 case BaseConstraint.EQUALS : 00190 case BaseConstraint.SMALLER : 00191 case BaseConstraint.EQUALSMALLER : 00192 case BaseConstraint.NOT : 00193 res = key - op.operande; 00194 if (res > 0) return 1; 00195 if (res < 0) return -1; 00196 return 0 ; 00197 case BaseConstraint.GREATER : 00198 case BaseConstraint.EQUALGREATER : 00199 res = op.operande - key; 00200 if (res > 0) return 1; 00201 if (res < 0) return -1; 00202 return 0 ; 00203 } 00204 return (int)(key - op.operande); 00205 } 00206 00207 }