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

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

The newest version!
package com.bbn.bue.common.symbols;

import com.bbn.bue.common.converters.StringConverter;

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.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Ordering;

import java.util.Comparator;
import java.util.Map;

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

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

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

  private static final Ordering symbolStringOrdering =
      Ordering.natural().onResultOf(desymbolizeFunction());

  /**
   * An ordering which compares Symbols by the Strings used to create
   * them.
   */
  public static Ordering byStringOrdering() {
    return symbolStringOrdering;
  }

  /**
   * Compares {@link Symbol}s by the {@link String}s used to create them. Use {@link
   * #byStringOrdering()} instead.
   *
   * @author rgabbard
   * @see #byStringOrdering()
   */
  @Deprecated
  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);
    }
  };

  public static StringConverter StringToSymbol() {
    return StringToSymbolConverter.INSTANCE;
  }

  private enum StringToSymbolConverter implements StringConverter {
    INSTANCE;

    @Override
    public Symbol decode(final String s) {
      return Symbol.from(s);
    }
  }

  /**
   * Returns a function that transforms a {@link Symbol} into a {@link String} using {@link
   * Symbol#asString()}.
   */
  public static Function desymbolizeFunction() {
    return DesymbolizeFunction.INSTANCE;
  }

  private enum DesymbolizeFunction implements Function {
    INSTANCE;

    @Override
    public String apply(final Symbol s) {
      return s.asString();
    }
  }

  /**
   * Returns a function that transforms a {@link String} into a {@link Symbol} using {@link
   * Symbol#from(String)}.
   */
  public static Function symbolizeFunction() {
    return SymbolizeFunction.INSTANCE;
  }

  private enum SymbolizeFunction implements Function {
    INSTANCE;

    @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());
  }

  /**
   * Creates a map of {@link Symbol}s from a map of {@link String}s.  No keys or values may be
   * null.
   */
  public static ImmutableMap mapFrom(Map stringMap) {
    final ImmutableMap.Builder ret = ImmutableMap.builder();

    for (Map.Entry stringEntry : stringMap.entrySet()) {
      ret.put(Symbol.from(stringEntry.getKey()),
          Symbol.from(stringEntry.getValue()));
    }

    return ret.build();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy