All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.bbn.bue.common.symbols.SymbolUtils Maven / Gradle / Ivy

There is a newer version: 4.1.2
Show newest version
package com.bbn.bue.common.symbols;

import com.google.common.annotations.Beta;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Ordering;

import java.util.Comparator;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Utility methods for {link Symbol}s
 *
 * @author rgabbard
 */
@Beta
public class SymbolUtils {

  private SymbolUtils() {
    throw new UnsupportedOperationException();
  }

  /**
   * An ordering which compares Symbols by the Strings used to create them.
   *
   * @author rgabbard
   */
  public static Ordering byStringOrdering() {
    return Ordering.from(new ByString());
  }

  /**
   * Compares Symbols by the Strings used to create them.
   *
   * @author rgabbard
   */
  public static class ByString implements Comparator {

    @Override
    public int compare(final Symbol s1, final Symbol s2) {
      if (s1 == null) {
        if (s2 == null) {
          return 0;
        } else {
          return -1;
        }
      } else if (s2 == null) {
        return 1;
      }
      return s1.toString().compareTo(s2.toString());
    }
  }

  /**
   * For every input String s, returns Symbol.from(s).
   */
  public static final Function Symbolize = new Function() {
    @Override
    public Symbol apply(final String s) {
      return Symbol.from(s);
    }
  };

  /**
   * Creates a Set of Symbols from some strings. The returned Set is
   * immutable.
   *
   * @param strings No string may be null.
   */
  public static ImmutableSet setFrom(final Iterable strings) {
    checkNotNull(strings);

    return FluentIterable.from(strings)
        .transform(Symbolize)
        .toSet();
  }

  /**
   * Creates a List of Symbols from some strings. The returned List is
   * immutable.
   *
   * @param strings No string may be null.
   */
  public static ImmutableList listFrom(final Iterable strings) {
    checkNotNull(strings);
    return FluentIterable.from(strings)
        .transform(Symbolize)
        .toList();
  }

  public static ImmutableSet toStringSet(final Iterable syms) {
    final ImmutableSet.Builder ret = ImmutableSet.builder();

    for (final Symbol sym : syms) {
      ret.add(sym.toString());
    }

    return ret.build();
  }

  public static ImmutableSet setFrom(final String... strings) {
    final ImmutableSet.Builder ret = ImmutableSet.builder();
    for (final String s : strings) {
      ret.add(Symbol.from(s));
    }
    return ret.build();
  }

  /**
   * Returns a lowercased version of the specified symbol, where lowercasing is done by {@link
   * String#toLowerCase()}.
   */
  public static Symbol lowercaseSymbol(Symbol s) {
    return Symbol.from(s.toString().toLowerCase());
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy