| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.database; |
| 18 |
|
| 19 |
import java.util.Iterator; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public final class CursorJoiner |
| 46 |
implements Iterator<CursorJoiner.Result>, Iterable<CursorJoiner.Result> { |
| 47 |
private Cursor mCursorLeft; |
| 48 |
private Cursor mCursorRight; |
| 49 |
private boolean mCompareResultIsValid; |
| 50 |
private Result mCompareResult; |
| 51 |
private int[] mColumnsLeft; |
| 52 |
private int[] mColumnsRight; |
| 53 |
private String[] mValues; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public enum Result { |
| 59 |
|
| 60 |
RIGHT, |
| 61 |
|
| 62 |
LEFT, |
| 63 |
|
| 64 |
BOTH |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public CursorJoiner( |
| 76 |
Cursor cursorLeft, String[] columnNamesLeft, |
| 77 |
Cursor cursorRight, String[] columnNamesRight) { |
| 78 |
if (columnNamesLeft.length != columnNamesRight.length) { |
| 79 |
throw new IllegalArgumentException( |
| 80 |
"you must have the same number of columns on the left and right, " |
| 81 |
+ columnNamesLeft.length + " != " + columnNamesRight.length); |
| 82 |
} |
| 83 |
|
| 84 |
mCursorLeft = cursorLeft; |
| 85 |
mCursorRight = cursorRight; |
| 86 |
|
| 87 |
mCursorLeft.moveToFirst(); |
| 88 |
mCursorRight.moveToFirst(); |
| 89 |
|
| 90 |
mCompareResultIsValid = false; |
| 91 |
|
| 92 |
mColumnsLeft = buildColumnIndiciesArray(cursorLeft, columnNamesLeft); |
| 93 |
mColumnsRight = buildColumnIndiciesArray(cursorRight, columnNamesRight); |
| 94 |
|
| 95 |
mValues = new String[mColumnsLeft.length * 2]; |
| 96 |
} |
| 97 |
|
| 98 |
public Iterator<Result> iterator() { |
| 99 |
return this; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
private int[] buildColumnIndiciesArray(Cursor cursor, String[] columnNames) { |
| 109 |
int[] columns = new int[columnNames.length]; |
| 110 |
for (int i = 0; i < columnNames.length; i++) { |
| 111 |
columns[i] = cursor.getColumnIndexOrThrow(columnNames[i]); |
| 112 |
} |
| 113 |
return columns; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public boolean hasNext() { |
| 121 |
if (mCompareResultIsValid) { |
| 122 |
switch (mCompareResult) { |
| 123 |
case BOTH: |
| 124 |
return !mCursorLeft.isLast() || !mCursorRight.isLast(); |
| 125 |
|
| 126 |
case LEFT: |
| 127 |
return !mCursorLeft.isLast() || !mCursorRight.isAfterLast(); |
| 128 |
|
| 129 |
case RIGHT: |
| 130 |
return !mCursorLeft.isAfterLast() || !mCursorRight.isLast(); |
| 131 |
|
| 132 |
default: |
| 133 |
throw new IllegalStateException("bad value for mCompareResult, " |
| 134 |
+ mCompareResult); |
| 135 |
} |
| 136 |
} else { |
| 137 |
return !mCursorLeft.isAfterLast() || !mCursorRight.isAfterLast(); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public Result next() { |
| 157 |
if (!hasNext()) { |
| 158 |
throw new IllegalStateException("you must only call next() when hasNext() is true"); |
| 159 |
} |
| 160 |
incrementCursors(); |
| 161 |
assert hasNext(); |
| 162 |
|
| 163 |
boolean hasLeft = !mCursorLeft.isAfterLast(); |
| 164 |
boolean hasRight = !mCursorRight.isAfterLast(); |
| 165 |
|
| 166 |
if (hasLeft && hasRight) { |
| 167 |
populateValues(mValues, mCursorLeft, mColumnsLeft, 0 ); |
| 168 |
populateValues(mValues, mCursorRight, mColumnsRight, 1 ); |
| 169 |
switch (compareStrings(mValues)) { |
| 170 |
case -1: |
| 171 |
mCompareResult = Result.LEFT; |
| 172 |
break; |
| 173 |
case 0: |
| 174 |
mCompareResult = Result.BOTH; |
| 175 |
break; |
| 176 |
case 1: |
| 177 |
mCompareResult = Result.RIGHT; |
| 178 |
break; |
| 179 |
} |
| 180 |
} else if (hasLeft) { |
| 181 |
mCompareResult = Result.LEFT; |
| 182 |
} else { |
| 183 |
assert hasRight; |
| 184 |
mCompareResult = Result.RIGHT; |
| 185 |
} |
| 186 |
mCompareResultIsValid = true; |
| 187 |
return mCompareResult; |
| 188 |
} |
| 189 |
|
| 190 |
public void remove() { |
| 191 |
throw new UnsupportedOperationException("not implemented"); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
private static void populateValues(String[] values, Cursor cursor, int[] columnIndicies, |
| 205 |
int startingIndex) { |
| 206 |
assert startingIndex == 0 || startingIndex == 1; |
| 207 |
for (int i = 0; i < columnIndicies.length; i++) { |
| 208 |
values[startingIndex + i*2] = cursor.getString(columnIndicies[i]); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
private void incrementCursors() { |
| 217 |
if (mCompareResultIsValid) { |
| 218 |
switch (mCompareResult) { |
| 219 |
case LEFT: |
| 220 |
mCursorLeft.moveToNext(); |
| 221 |
break; |
| 222 |
case RIGHT: |
| 223 |
mCursorRight.moveToNext(); |
| 224 |
break; |
| 225 |
case BOTH: |
| 226 |
mCursorLeft.moveToNext(); |
| 227 |
mCursorRight.moveToNext(); |
| 228 |
break; |
| 229 |
} |
| 230 |
mCompareResultIsValid = false; |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
private static int compareStrings(String... values) { |
| 243 |
if ((values.length % 2) != 0) { |
| 244 |
throw new IllegalArgumentException("you must specify an even number of values"); |
| 245 |
} |
| 246 |
|
| 247 |
for (int index = 0; index < values.length; index+=2) { |
| 248 |
if (values[index] == null) { |
| 249 |
if (values[index+1] == null) continue; |
| 250 |
return -1; |
| 251 |
} |
| 252 |
|
| 253 |
if (values[index+1] == null) { |
| 254 |
return 1; |
| 255 |
} |
| 256 |
|
| 257 |
int comp = values[index].compareTo(values[index+1]); |
| 258 |
if (comp != 0) { |
| 259 |
return comp < 0 ? -1 : 1; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
return 0; |
| 264 |
} |
| 265 |
} |