1 13 14 package mondrian.rolap; 15 16 import java.util.Arrays ; 17 18 39 public interface CellKey { 40 45 int size(); 46 47 55 int[] getOrdinals(); 56 57 64 void setOrdinals(int[] pos); 65 66 73 int getAxis(int axis); 74 75 82 void setAxis(int axis, int value); 83 84 89 CellKey copy(); 90 91 public class Generator { 92 98 public static CellKey newCellKey(int size) { 99 switch (size) { 100 case 0: 101 return new Zero(); 102 case 1: 103 return new One(0); 104 case 2: 105 return new Two(0, 0); 106 case 3: 107 return new Three(0, 0, 0); 108 default: 109 return new Many(new int[size]); 110 } 111 } 112 113 119 public static CellKey newCellKey(int[] pos) { 120 switch (pos.length) { 121 case 0: 122 return new Zero(); 123 case 1: 124 return new One(pos[0]); 125 case 2: 126 return new Two(pos[0], pos[1]); 127 case 3: 128 return new Three(pos[0], pos[1], pos[2]); 129 default: 130 return new Many(pos.clone()); 131 } 132 } 133 134 142 public static CellKey newRefCellKey(int[] pos) { 143 return new Many(pos); 145 } 146 147 154 static CellKey newManyCellKey(int size) { 155 return new Many(new int[size]); 156 } 157 } 158 159 public class Zero implements CellKey { 160 private static final int[] EMPTY_INT_ARRAY = new int[0]; 161 public static final Zero INSTANCE = new Zero(); 162 163 166 private Zero() { 167 } 168 169 public Zero copy() { 170 return this; 172 } 173 174 public boolean equals(Object o) { 175 return o instanceof Zero; 176 } 177 178 179 public int size() { 180 return 0; 181 } 182 183 public int[] getOrdinals() { 184 return EMPTY_INT_ARRAY; 185 } 186 187 public void setOrdinals(int[] pos) { 188 if (pos.length != 0) { 189 throw new IllegalArgumentException (); 190 } 191 } 192 193 public int getAxis(int axis) { 194 throw new ArrayIndexOutOfBoundsException (axis); 195 } 196 197 public void setAxis(int axis, int value) { 198 throw new ArrayIndexOutOfBoundsException (axis); 199 } 200 } 201 202 public class One implements CellKey { 203 private int ordinal0; 204 205 210 private One(int ordinal0) { 211 this.ordinal0 = ordinal0; 212 } 213 214 public int size() { 215 return 1; 216 } 217 218 public int[] getOrdinals() { 219 return new int[] {ordinal0}; 220 } 221 222 public void setOrdinals(int[] pos) { 223 if (pos.length != 1) { 224 throw new IllegalArgumentException (); 225 } 226 ordinal0 = pos[0]; 227 } 228 229 public int getAxis(int axis) { 230 switch (axis) { 231 case 0: 232 return ordinal0; 233 default: 234 throw new ArrayIndexOutOfBoundsException (axis); 235 } 236 } 237 238 public void setAxis(int axis, int value) { 239 switch (axis) { 240 case 0: 241 ordinal0 = value; 242 break; 243 default: 244 throw new ArrayIndexOutOfBoundsException (axis); 245 } 246 } 247 248 public One copy() { 249 return new One(ordinal0); 250 } 251 252 public boolean equals(Object o) { 253 if (o instanceof One) { 255 One other = (One) o; 256 return (this.ordinal0 == other.ordinal0); 257 } else if (o instanceof Many) { 258 Many many = (Many) o; 259 return many.ordinals.length == 1 && 260 many.ordinals[0] == this.ordinal0; 261 } else { 262 return false; 263 } 264 } 265 266 public String toString() { 267 return "(" + ordinal0 + ")"; 268 } 269 270 public int hashCode() { 271 return 17 + ordinal0; 272 } 273 } 274 275 public class Two implements CellKey { 276 private int ordinal0; 277 private int ordinal1; 278 279 285 private Two(int ordinal0, int ordinal1) { 286 this.ordinal0 = ordinal0; 287 this.ordinal1 = ordinal1; 288 } 289 290 public String toString() { 291 return "(" + ordinal0 + ", " + ordinal1 + ")"; 292 } 293 294 public Two copy() { 295 return new Two(ordinal0, ordinal1); 296 } 297 298 public boolean equals(Object o) { 299 if (o instanceof Two) { 300 Two other = (Two) o; 301 return (other.ordinal0 == this.ordinal0) && 302 (other.ordinal1 == this.ordinal1); 303 } else if (o instanceof Many) { 304 Many many = (Many) o; 305 return many.ordinals.length == 2 && 306 many.ordinals[0] == this.ordinal0 && 307 many.ordinals[1] == this.ordinal1; 308 } else { 309 return false; 310 } 311 } 312 313 public int hashCode() { 314 int h0 = 17 + ordinal0; 315 return h0 * 37 + ordinal1; 316 } 317 318 public int size() { 319 return 2; 320 } 321 322 public int[] getOrdinals() { 323 return new int[] {ordinal0, ordinal1}; 324 } 325 326 public void setOrdinals(int[] pos) { 327 if (pos.length != 2) { 328 throw new IllegalArgumentException (); 329 } 330 ordinal0 = pos[0]; 331 ordinal1 = pos[1]; 332 } 333 334 public int getAxis(int axis) { 335 switch (axis) { 336 case 0: 337 return ordinal0; 338 case 1: 339 return ordinal1; 340 default: 341 throw new ArrayIndexOutOfBoundsException (axis); 342 } 343 } 344 345 public void setAxis(int axis, int value) { 346 switch (axis) { 347 case 0: 348 ordinal0 = value; 349 break; 350 case 1: 351 ordinal1 = value; 352 break; 353 default: 354 throw new ArrayIndexOutOfBoundsException (axis); 355 } 356 } 357 } 358 359 class Three implements CellKey { 360 private int ordinal0; 361 private int ordinal1; 362 private int ordinal2; 363 364 371 private Three(int ordinal0, int ordinal1, int ordinal2) { 372 this.ordinal0 = ordinal0; 373 this.ordinal1 = ordinal1; 374 this.ordinal2 = ordinal2; 375 } 376 377 public String toString() { 378 return "(" + ordinal0 + ", " + ordinal1 + ", " + ordinal2 + ")"; 379 } 380 381 public Three copy() { 382 return new Three(ordinal0, ordinal1, ordinal2); 383 } 384 385 public boolean equals(Object o) { 386 if (o instanceof Three) { 388 Three other = (Three) o; 389 return (other.ordinal0 == this.ordinal0) && 390 (other.ordinal1 == this.ordinal1) && 391 (other.ordinal2 == this.ordinal2); 392 } else if (o instanceof Many) { 393 Many many = (Many) o; 394 return many.ordinals.length == 3 && 395 many.ordinals[0] == this.ordinal0 && 396 many.ordinals[1] == this.ordinal1 && 397 many.ordinals[2] == this.ordinal2; 398 } else { 399 return false; 400 } 401 } 402 403 public int hashCode() { 404 int h0 = 17 + ordinal0; 405 int h1 = h0 * 37 + ordinal1; 406 return h1 * 37 + ordinal2; 407 } 408 409 public int getAxis(int axis) { 410 switch (axis) { 411 case 0: 412 return ordinal0; 413 case 1: 414 return ordinal1; 415 case 2: 416 return ordinal2; 417 default: 418 throw new ArrayIndexOutOfBoundsException (axis); 419 } 420 } 421 422 public void setAxis(int axis, int value) { 423 switch (axis) { 424 case 0: 425 ordinal0 = value; 426 break; 427 case 1: 428 ordinal1 = value; 429 break; 430 case 2: 431 ordinal2 = value; 432 break; 433 default: 434 throw new ArrayIndexOutOfBoundsException (axis); 435 } 436 } 437 438 public int size() { 439 return 3; 440 } 441 442 public int[] getOrdinals() { 443 return new int[] {ordinal0, ordinal1, ordinal2}; 444 } 445 446 public void setOrdinals(int[] pos) { 447 if (pos.length != 3) { 448 throw new IllegalArgumentException (); 449 } 450 ordinal0 = pos[0]; 451 ordinal1 = pos[1]; 452 ordinal2 = pos[2]; 453 } 454 } 455 456 public class Many implements CellKey { 457 private final int[] ordinals; 458 459 463 protected Many(int[] ordinals) { 464 this.ordinals = ordinals; 465 } 466 467 public final int size() { 468 return this.ordinals.length; 469 } 470 471 public final void setOrdinals(int[] pos) { 472 if (ordinals.length != pos.length) { 473 throw new IllegalArgumentException (); 474 } 475 System.arraycopy(pos, 0, this.ordinals, 0, ordinals.length); 476 } 477 public final int[] getOrdinals() { 478 return this.ordinals; 479 } 480 481 public void setAxis(int axis, int value) { 482 this.ordinals[axis] = value; 483 } 484 485 public int getAxis(int axis) { 486 return this.ordinals[axis]; 487 } 488 489 public String toString() { 490 StringBuilder buf = new StringBuilder (); 491 buf.append('('); 492 for (int i = 0; i < ordinals.length; i++) { 493 if (i > 0) { 494 buf.append(','); 495 } 496 buf.append(ordinals[i]); 497 } 498 buf.append(')'); 499 return buf.toString(); 500 } 501 502 public Many copy() { 503 return new Many(this.ordinals.clone()); 504 } 505 506 public int hashCode() { 507 int h = 17; 508 for (int ordinal : ordinals) { 509 h = (h * 37) + ordinal; 510 } 511 return h; 512 } 513 514 public boolean equals(Object o) { 515 if (o instanceof Many) { 516 Many that = (Many) o; 517 return Arrays.equals(this.ordinals, that.ordinals); 518 } else { 519 return o.equals(this); 521 } 522 } 523 } 524 } 525 526 | Popular Tags |