com.google.common.annotations.GwtCompatible Java Examples

The following examples show how to use com.google.common.annotations.GwtCompatible. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Maps.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Returns an immutable map instance containing the given entries.
 * Internally, the returned map will be backed by an {@link EnumMap}.
 *
 * <p>The iteration order of the returned map follows the enum's iteration
 * order, not the order in which the elements appear in the given map.
 *
 * @param map the map to make an immutable copy of
 * @return an immutable map containing those entries
 * @since 14.0
 */

@GwtCompatible(serializable = true)
@Beta
public static <K extends Enum<K>, V> ImmutableMap<K, V> immutableEnumMap(Map<K, ? extends V> map) {
  if (map instanceof ImmutableEnumMap) {

    @SuppressWarnings("unchecked") // safe covariant cast
    ImmutableEnumMap<K, V> result = (ImmutableEnumMap<K, V>) map;
    return result;
  } else if (map.isEmpty()) {
    return ImmutableMap.of();
  } else {
    for (Map.Entry<K, ? extends V> entry : map.entrySet()) {
      checkNotNull(entry.getKey());
      checkNotNull(entry.getValue());
    }
    return ImmutableEnumMap.asImmutable(new EnumMap<K, V>(map));
  }
}
 
Example #2
Source File: Sets.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Returns an immutable set instance containing the given enum elements.
 * Internally, the returned set will be backed by an {@link EnumSet}.
 *
 * <p>The iteration order of the returned set follows the enum's iteration
 * order, not the order in which the elements appear in the given collection.
 *
 * @param elements the elements, all of the same {@code enum} type, that the
 *     set should contain
 * @return an immutable set containing those elements, minus duplicates
 */
// http://code.google.com/p/google-web-toolkit/issues/detail?id=3028
@GwtCompatible(serializable = true)
public static <E extends Enum<E>> ImmutableSet<E> immutableEnumSet(Iterable<E> elements) {
  if (elements instanceof ImmutableEnumSet) {
    return (ImmutableEnumSet<E>) elements;
  } else if (elements instanceof Collection) {
    Collection<E> collection = (Collection<E>) elements;
    if (collection.isEmpty()) {
      return ImmutableSet.of();
    } else {
      return ImmutableEnumSet.asImmutable(EnumSet.copyOf(collection));
    }
  } else {
    Iterator<E> itr = elements.iterator();
    if (itr.hasNext()) {
      EnumSet<E> enumSet = EnumSet.of(itr.next());
      Iterators.addAll(enumSet, itr);
      return ImmutableEnumSet.asImmutable(enumSet);
    } else {
      return ImmutableSet.of();
    }
  }
}
 
Example #3
Source File: Ordering.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns an ordering that treats {@code null} as less than all other values
 * and uses {@code this} to compare non-null values.
 */
// type parameter <S> lets us avoid the extra <String> in statements like:
// Ordering<String> o = Ordering.<String>natural().nullsFirst();

@GwtCompatible(serializable = true)
public <S extends T> Ordering<S> nullsFirst() {
  return new NullsFirstOrdering<S>(this);
}
 
Example #4
Source File: Lists.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Creates a <i>mutable</i> {@code ArrayList} instance containing the given
 * elements; a very thin shortcut for creating an empty list and then calling
 * {@link Iterators#addAll}.
 *
 * <p><b>Note:</b> if mutability is not required and the elements are
 * non-null, use {@link ImmutableList#copyOf(Iterator)} instead.
 */

@CanIgnoreReturnValue // TODO(kak): Remove this
@GwtCompatible(serializable = true)
public static <E> ArrayList<E> newArrayList(Iterator<? extends E> elements) {
  ArrayList<E> list = newArrayList();
  Iterators.addAll(list, elements);
  return list;
}
 
Example #5
Source File: Ordering.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns the reverse of this ordering; the {@code Ordering} equivalent to
 * {@link Collections#reverseOrder(Comparator)}.
 */
// type parameter <S> lets us avoid the extra <String> in statements like:
// Ordering<String> o = Ordering.<String>natural().reverse();

@GwtCompatible(serializable = true)
public <S extends T> Ordering<S> reverse() {
  return new ReverseOrdering<S>(this);
}
 
Example #6
Source File: Ordering.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Simply returns its argument.
 *
 * @deprecated no need to use this
 */

@GwtCompatible(serializable = true)
@Deprecated
public static <T> Ordering<T> from(Ordering<T> ordering) {
  return checkNotNull(ordering);
}
 
Example #7
Source File: Ordering.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns the reverse of this ordering; the {@code Ordering} equivalent to
 * {@link Collections#reverseOrder(Comparator)}.
 */
// type parameter <S> lets us avoid the extra <String> in statements like:
// Ordering<String> o = Ordering.<String>natural().reverse();

@GwtCompatible(serializable = true)
public <S extends T> Ordering<S> reverse() {
  return new ReverseOrdering<S>(this);
}
 
Example #8
Source File: Ordering.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns an ordering that treats {@code null} as less than all other values
 * and uses {@code this} to compare non-null values.
 */
// type parameter <S> lets us avoid the extra <String> in statements like:
// Ordering<String> o = Ordering.<String>natural().nullsFirst();

@GwtCompatible(serializable = true)
public <S extends T> Ordering<S> nullsFirst() {
  return new NullsFirstOrdering<S>(this);
}
 
Example #9
Source File: Ordering.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns an ordering that treats {@code null} as less than all other values
 * and uses {@code this} to compare non-null values.
 */
// type parameter <S> lets us avoid the extra <String> in statements like:
// Ordering<String> o = Ordering.<String>natural().nullsFirst();

@GwtCompatible(serializable = true)
public <S extends T> Ordering<S> nullsFirst() {
  return new NullsFirstOrdering<S>(this);
}
 
Example #10
Source File: Ordering.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns an ordering that treats {@code null} as greater than all other
 * values and uses this ordering to compare non-null values.
 */
// type parameter <S> lets us avoid the extra <String> in statements like:
// Ordering<String> o = Ordering.<String>natural().nullsLast();

@GwtCompatible(serializable = true)
public <S extends T> Ordering<S> nullsLast() {
  return new NullsLastOrdering<S>(this);
}
 
Example #11
Source File: Ordering.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns the reverse of this ordering; the {@code Ordering} equivalent to
 * {@link Collections#reverseOrder(Comparator)}.
 */
// type parameter <S> lets us avoid the extra <String> in statements like:
// Ordering<String> o = Ordering.<String>natural().reverse();
@GwtCompatible(serializable = true)
public <S extends T> Ordering<S> reverse() {
  return new ReverseOrdering<S>(this);
}
 
Example #12
Source File: Lists.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Creates a <i>mutable</i> {@code ArrayList} instance containing the given
 * elements; a very thin shortcut for creating an empty list and then calling
 * {@link Iterators#addAll}.
 *
 * <p><b>Note:</b> if mutability is not required and the elements are
 * non-null, use {@link ImmutableList#copyOf(Iterator)} instead.
 */

@CanIgnoreReturnValue // TODO(kak): Remove this
@GwtCompatible(serializable = true)
public static <E> ArrayList<E> newArrayList(Iterator<? extends E> elements) {
  ArrayList<E> list = newArrayList();
  Iterators.addAll(list, elements);
  return list;
}
 
Example #13
Source File: Ordering.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns an ordering that treats {@code null} as greater than all other
 * values and uses this ordering to compare non-null values.
 */
// type parameter <S> lets us avoid the extra <String> in statements like:
// Ordering<String> o = Ordering.<String>natural().nullsLast();

@GwtCompatible(serializable = true)
public <S extends T> Ordering<S> nullsLast() {
  return new NullsLastOrdering<S>(this);
}
 
Example #14
Source File: Ordering.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns an ordering that treats {@code null} as greater than all other
 * values and uses this ordering to compare non-null values.
 */
// type parameter <S> lets us avoid the extra <String> in statements like:
// Ordering<String> o = Ordering.<String>natural().nullsLast();

@GwtCompatible(serializable = true)
public <S extends T> Ordering<S> nullsLast() {
  return new NullsLastOrdering<S>(this);
}
 
Example #15
Source File: Ordering.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns an ordering that treats {@code null} as greater than all other
 * values and uses this ordering to compare non-null values.
 */
// type parameter <S> lets us avoid the extra <String> in statements like:
// Ordering<String> o = Ordering.<String>natural().nullsLast();
@GwtCompatible(serializable = true)
public <S extends T> Ordering<S> nullsLast() {
  return new NullsLastOrdering<S>(this);
}
 
Example #16
Source File: Ordering.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Simply returns its argument.
 *
 * @deprecated no need to use this
 */

@GwtCompatible(serializable = true)
@Deprecated
public static <T> Ordering<T> from(Ordering<T> ordering) {
  return checkNotNull(ordering);
}
 
Example #17
Source File: Predicates.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Returns a predicate that always evaluates to {@code true}.
 */

@GwtCompatible(serializable = true)
public static <T> Predicate<T> alwaysTrue() {
  return ObjectPredicate.ALWAYS_TRUE.withNarrowedType();
}
 
Example #18
Source File: Ordering.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Simply returns its argument.
 *
 * @deprecated no need to use this
 */
@GwtCompatible(serializable = true)
@Deprecated
public static <T> Ordering<T> from(Ordering<T> ordering) {
  return checkNotNull(ordering);
}
 
Example #19
Source File: Predicates.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Returns a predicate that always evaluates to {@code false}.
 */

@GwtCompatible(serializable = true)
public static <T> Predicate<T> alwaysFalse() {
  return ObjectPredicate.ALWAYS_FALSE.withNarrowedType();
}
 
Example #20
Source File: Predicates.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
* Returns a predicate that always evaluates to {@code true}.
*/

 @GwtCompatible(serializable = true)
 public static <T> Predicate<T> alwaysTrue() {
 return ObjectPredicate.ALWAYS_TRUE.withNarrowedType();
 }
 
Example #21
Source File: Predicates.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Returns a predicate that always evaluates to {@code true}.
 */

@GwtCompatible(serializable = true)
public static <T> Predicate<T> alwaysTrue() {
  return ObjectPredicate.ALWAYS_TRUE.withNarrowedType();
}
 
Example #22
Source File: Predicates.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Returns a predicate that always evaluates to {@code false}.
 */

@GwtCompatible(serializable = true)
public static <T> Predicate<T> alwaysFalse() {
  return ObjectPredicate.ALWAYS_FALSE.withNarrowedType();
}
 
Example #23
Source File: Predicates.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Returns a predicate that evaluates to {@code true} if the object reference being tested is
 * null.
 */

@GwtCompatible(serializable = true)
public static <T> Predicate<T> isNull() {
  return ObjectPredicate.IS_NULL.withNarrowedType();
}
 
Example #24
Source File: Predicates.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Returns a predicate that evaluates to {@code true} if the object reference being tested is not
 * null.
 */

@GwtCompatible(serializable = true)
public static <T> Predicate<T> notNull() {
  return ObjectPredicate.NOT_NULL.withNarrowedType();
}
 
Example #25
Source File: Predicates.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Returns a predicate that evaluates to {@code true} if the object reference being tested is
 * null.
 */

@GwtCompatible(serializable = true)
public static <T> Predicate<T> isNull() {
  return ObjectPredicate.IS_NULL.withNarrowedType();
}
 
Example #26
Source File: Predicates.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Returns a predicate that evaluates to {@code true} if the object reference being tested is not
 * null.
 */
@GwtCompatible(serializable = true)
public static <T> Predicate<T> notNull() {
  return ObjectPredicate.NOT_NULL.withNarrowedType();
}
 
Example #27
Source File: Predicates.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Returns a predicate that evaluates to {@code true} if the object reference being tested is not
 * null.
 */

@GwtCompatible(serializable = true)
public static <T> Predicate<T> notNull() {
  return ObjectPredicate.NOT_NULL.withNarrowedType();
}
 
Example #28
Source File: Predicates.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Returns a predicate that always evaluates to {@code true}.
 */

@GwtCompatible(serializable = true)
public static <T> Predicate<T> alwaysTrue() {
  return ObjectPredicate.ALWAYS_TRUE.withNarrowedType();
}
 
Example #29
Source File: Lists.java    From codebuff with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * Creates a <i>mutable</i> {@code ArrayList} instance containing the given
 * elements.
 *
 * <p><b>Note:</b> essentially the only reason to use this method is when you
 * will need to add or remove elements later. Otherwise, for non-null elements
 * use {@link ImmutableList#of()} (for varargs) or {@link
 * ImmutableList#copyOf(Object[])} (for an array) instead. If any elements
 * might be null, or you need support for {@link List#set(int, Object)}, use
 * {@link Arrays#asList}.
 *
 * <p>Note that even when you do need the ability to add or remove, this method
 * provides only a tiny bit of syntactic sugar for {@code newArrayList(}{@link
 * Arrays#asList asList}{@code (...))}, or for creating an empty list then
 * calling {@link Collections#addAll}. This method is not actually very useful
 * and will likely be deprecated in the future.
 */

@CanIgnoreReturnValue // TODO(kak): Remove this
@GwtCompatible(serializable = true)
public static <E> ArrayList<E> newArrayList(E... elements) {
  checkNotNull(elements); // for GWT
  // Avoid integer overflow when a large array is passed in
  int capacity = computeArrayListCapacity(elements.length);
  ArrayList<E> list = new ArrayList<E>(capacity);
  Collections.addAll(list, elements);
  return list;
}
 
Example #30
Source File: Lists.java    From codebuff with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * Creates a <i>mutable</i> {@code ArrayList} instance containing the given
 * elements; a very thin shortcut for creating an empty list then calling
 * {@link Iterables#addAll}.
 *
 * <p><b>Note:</b> if mutability is not required and the elements are
 * non-null, use {@link ImmutableList#copyOf(Iterable)} instead. (Or, change
 * {@code elements} to be a {@link FluentIterable} and call
 * {@code elements.toList()}.)
 *
 * <p><b>Note for Java 7 and later:</b> if {@code elements} is a {@link
 * Collection}, you don't need this method. Use the {@code ArrayList}
 * {@linkplain ArrayList#ArrayList(Collection) constructor} directly, taking
 * advantage of the new <a href="http://goo.gl/iz2Wi">"diamond" syntax</a>.
 */

@CanIgnoreReturnValue // TODO(kak): Remove this
@GwtCompatible(serializable = true)
public static <E> ArrayList<E> newArrayList(Iterable<? extends E> elements) {
  checkNotNull(elements); // for GWT
  // Let ArrayList's sizing logic work, if possible
  return (elements instanceof Collection)
    ? new ArrayList<E>(Collections2.cast(elements))
    : newArrayList(elements.iterator());
}