Modifies Unicode set to flatten the strings. - Android java.lang

Android examples for java.lang:String Unicode

Description

Modifies Unicode set to flatten the strings.

Demo Code

/*/*from w w  w .  jav  a  2s  .c om*/
 *******************************************************************************
 * Copyright (C) 1996-2015, International Business Machines Corporation and    *
 * others. All Rights Reserved.                                                *
 *******************************************************************************
 */
import java.util.Collection;
import java.util.Iterator;

import android.icu.text.UnicodeSet;
import android.icu.text.UnicodeSetIterator;

public class Main {
  /**
   * Modifies Unicode set to flatten the strings. Eg [abc{da}] => [abcd] Returns
   * the set for chaining.
   * 
   * @param exemplar1
   * @return
   */
  public static UnicodeSet flatten(UnicodeSet exemplar1) {
    UnicodeSet result = new UnicodeSet();
    boolean gotString = false;
    for (UnicodeSetIterator it = new UnicodeSetIterator(exemplar1); it
        .nextRange();) {
      if (it.codepoint == UnicodeSetIterator.IS_STRING) {
        result.addAll(it.string);
        gotString = true;
      } else {
        result.add(it.codepoint, it.codepointEnd);
      }
    }
    if (gotString)
      exemplar1.set(result);
    return exemplar1;
  }

  /**
   * Add all items in iterator to target collection
   * 
   * @param <T>
   * @param <U>
   * @param source
   * @param target
   * @return
   */
  public static <T, U extends Collection<T>> U addAll(Iterator<T> source,
      U target) {
    while (source.hasNext()) {
      target.add(source.next());
    }
    return target; // for chaining
  }
}

Related Tutorials