|
8 | 8 | import org.json.JSONException;
|
9 | 9 |
|
10 | 10 | import java.util.ArrayList;
|
| 11 | +import java.util.Arrays; |
| 12 | +import java.util.Collection; |
| 13 | +import java.util.Collections; |
11 | 14 | import java.util.List;
|
12 | 15 |
|
13 | 16 | /*
|
@@ -422,16 +425,151 @@ public Integer getHitsPerPage() {
|
422 | 425 |
|
423 | 426 | private static final String KEY_IGNORE_PLURALS = "ignorePlurals";
|
424 | 427 |
|
| 428 | + /** |
| 429 | + * A value of the {@code ignorePlurals} setting. |
| 430 | + * Can represent either a boolean or a list of language codes, see https://www.algolia.com/doc/faq/searching/how-does-ignoreplurals-work. |
| 431 | + */ |
| 432 | + public static final class IgnorePlurals { |
| 433 | + /** Wether plurals are ignored. */ |
| 434 | + public final boolean enabled; |
| 435 | + |
| 436 | + /** A list containing every active language's code. When {@code null}, all supported languages are be used. */ |
| 437 | + public final List<String> languageCodes; |
| 438 | + |
| 439 | + /** |
| 440 | + * Construct an IgnorePlurals object for a boolean value. |
| 441 | + * |
| 442 | + * @param b if {@code true}, the engine will ignore plurals in all supported languages. |
| 443 | + */ |
| 444 | + public IgnorePlurals(boolean b) { |
| 445 | + this.enabled = b; |
| 446 | + languageCodes = null; |
| 447 | + } |
| 448 | + |
| 449 | + /** |
| 450 | + * Construct an IgnorePlurals object for a {@link Collection} of language codes. |
| 451 | + * |
| 452 | + * @param codes a list of language codes to ignore plurals from. if {@code null}, |
| 453 | + * the engine will ignore plurals in all supported languages. |
| 454 | + */ |
| 455 | + public IgnorePlurals(@Nullable Collection<String> codes) { |
| 456 | + this.enabled = !isEmptyCollection(codes); |
| 457 | + languageCodes = codes != null ? new ArrayList<>(codes) : null; |
| 458 | + } |
| 459 | + |
| 460 | + /** |
| 461 | + * Construct an IgnorePlurals object for some language codes. |
| 462 | + * |
| 463 | + * @param codes one or several language codes to ignore plurals from. |
| 464 | + * if {@code null}, the engine will ignore plurals in all supported languages. |
| 465 | + */ |
| 466 | + public IgnorePlurals(@Nullable String... codes) { |
| 467 | + this(codes == null ? null : Arrays.asList(codes)); |
| 468 | + } |
| 469 | + |
| 470 | + private boolean isEmptyCollection(@Nullable Collection<String> codesList) { |
| 471 | + return codesList == null || codesList.size() == 0; |
| 472 | + } |
| 473 | + |
| 474 | + @Override |
| 475 | + public String toString() { |
| 476 | + if (!enabled) { |
| 477 | + return "false"; |
| 478 | + } else { |
| 479 | + if (isEmptyCollection(languageCodes)) { // enabled without specific language |
| 480 | + return "true"; |
| 481 | + } else { |
| 482 | + return TextUtils.join(",", languageCodes); |
| 483 | + } |
| 484 | + } |
| 485 | + } |
| 486 | + |
| 487 | + static @NonNull IgnorePlurals parse(String s) { |
| 488 | + if (s == null || s.length() == 0 || s.equals("null")) { |
| 489 | + return new IgnorePlurals(false); |
| 490 | + } else if ("true".equals(s) || "false".equals(s)) { |
| 491 | + return new IgnorePlurals(parseBoolean(s)); |
| 492 | + } else { |
| 493 | + ArrayList<String> codesList = new ArrayList<>(); |
| 494 | + //ignorePlurals=["en","fi"] |
| 495 | + try { |
| 496 | + JSONArray codesArray = new JSONArray(s); |
| 497 | + for (int i = 0; i < codesArray.length(); i++) { |
| 498 | + codesList.add(codesArray.getJSONObject(i).toString()); |
| 499 | + } |
| 500 | + return new IgnorePlurals(codesList); |
| 501 | + } catch (JSONException e) { |
| 502 | + // s was not a JSONArray of strings. Maybe it is a comma-separated list? |
| 503 | + final String[] split = TextUtils.split(s, ","); |
| 504 | + if (split != null && split.length != 0) { |
| 505 | + Collections.addAll(codesList, split); |
| 506 | + return new IgnorePlurals(codesList); |
| 507 | + } else { |
| 508 | + final String msg = "Error while parsing `" + s + "`: invalid ignorePlurals value."; |
| 509 | + throw new RuntimeException(msg); |
| 510 | + } |
| 511 | + } |
| 512 | + } |
| 513 | + } |
| 514 | + |
| 515 | + @Override |
| 516 | + public boolean equals(Object o) { |
| 517 | + if (this == o) { |
| 518 | + return true; |
| 519 | + } |
| 520 | + if (o == null || getClass() != o.getClass()) { |
| 521 | + return false; |
| 522 | + } |
| 523 | + |
| 524 | + IgnorePlurals that = (IgnorePlurals) o; |
| 525 | + |
| 526 | + if (enabled != that.enabled) { |
| 527 | + return false; |
| 528 | + } |
| 529 | + return languageCodes != null ? languageCodes.equals(that.languageCodes) : that.languageCodes == null; |
| 530 | + |
| 531 | + } |
| 532 | + |
| 533 | + @Override |
| 534 | + public int hashCode() { |
| 535 | + int result = (enabled ? 1 : 0); |
| 536 | + result = 31 * result + (languageCodes != null ? languageCodes.hashCode() : 0); |
| 537 | + return result; |
| 538 | + } |
| 539 | + } |
| 540 | + |
425 | 541 | /**
|
426 | 542 | * If set to true, plural won't be considered as a typo (for example
|
427 | 543 | * car/cars will be considered as equals). Default to false.
|
428 | 544 | */
|
429 |
| - public @NonNull Query setIgnorePlurals(Boolean enabled) { |
| 545 | + public |
| 546 | + @NonNull |
| 547 | + Query setIgnorePlurals(boolean enabled) { |
430 | 548 | return set(KEY_IGNORE_PLURALS, enabled);
|
431 | 549 | }
|
432 | 550 |
|
433 |
| - public Boolean getIgnorePlurals() { |
434 |
| - return parseBoolean(get(KEY_IGNORE_PLURALS)); |
| 551 | + /** |
| 552 | + * A list of language codes for which plural won't be considered as a typo (for example |
| 553 | + * car/cars will be considered as equals). If empty or null, this disables the feature. |
| 554 | + */ |
| 555 | + public |
| 556 | + @NonNull |
| 557 | + Query setIgnorePlurals(@Nullable Collection<String> languageISOCodes) { |
| 558 | + return set(KEY_IGNORE_PLURALS, new IgnorePlurals(languageISOCodes)); |
| 559 | + } |
| 560 | + |
| 561 | + /** |
| 562 | + * One or several language codes for which plural won't be considered as a typo (for example |
| 563 | + * car/cars will be considered as equals). If empty or null, this disables the feature. |
| 564 | + */ |
| 565 | + public |
| 566 | + @NonNull |
| 567 | + Query setIgnorePlurals(@Nullable String... languageISOCodes) { |
| 568 | + return set(KEY_IGNORE_PLURALS, new IgnorePlurals(languageISOCodes)); |
| 569 | + } |
| 570 | + |
| 571 | + public @NonNull IgnorePlurals getIgnorePlurals() { |
| 572 | + return IgnorePlurals.parse(get(KEY_IGNORE_PLURALS)); |
435 | 573 | }
|
436 | 574 |
|
437 | 575 | /**
|
|
0 commit comments