001/*
002 * Copyright (C) 2009 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect.testing;
018
019import static java.util.Arrays.asList;
020
021import com.google.common.annotations.GwtCompatible;
022import java.util.ArrayList;
023import java.util.Collection;
024import java.util.List;
025import java.util.Set;
026import org.checkerframework.checker.nullness.qual.NonNull;
027import org.checkerframework.checker.nullness.qual.Nullable;
028
029/**
030 * A simplistic set which implements the bare minimum so that it can be used in tests without
031 * relying on any specific Set implementations. Slow. Explicitly allows null elements so that they
032 * can be used in the testers.
033 *
034 * @author Regina O'Dell
035 */
036@GwtCompatible
037@ElementTypesAreNonnullByDefault
038public class MinimalSet<E extends @Nullable Object> extends MinimalCollection<E> implements Set<E> {
039
040  @SuppressWarnings("unchecked") // empty Object[] as E[]
041  public static <E extends @Nullable Object> MinimalSet<E> of(E... contents) {
042    return ofClassAndContents(Object.class, (E[]) new Object[0], asList(contents));
043  }
044
045  @SuppressWarnings("unchecked") // empty Object[] as E[]
046  public static <E extends @Nullable Object> MinimalSet<E> from(Collection<? extends E> contents) {
047    return ofClassAndContents(Object.class, (E[]) new Object[0], contents);
048  }
049
050  public static <E extends @Nullable Object> MinimalSet<E> ofClassAndContents(
051      Class<? super @NonNull E> type, E[] emptyArrayForContents, Iterable<? extends E> contents) {
052    List<E> setContents = new ArrayList<>();
053    for (E e : contents) {
054      if (!setContents.contains(e)) {
055        setContents.add(e);
056      }
057    }
058    return new MinimalSet<>(type, setContents.toArray(emptyArrayForContents));
059  }
060
061  private MinimalSet(Class<? super @NonNull E> type, E... contents) {
062    super(type, true, contents);
063  }
064
065  /*
066   * equals() and hashCode() are more specific in the Set contract.
067   */
068
069  @Override
070  public boolean equals(@Nullable Object object) {
071    if (object instanceof Set) {
072      Set<?> that = (Set<?>) object;
073      return (this.size() == that.size()) && this.containsAll(that);
074    }
075    return false;
076  }
077
078  @Override
079  public int hashCode() {
080    int hashCodeSum = 0;
081    for (Object o : this) {
082      hashCodeSum += (o == null) ? 0 : o.hashCode();
083    }
084    return hashCodeSum;
085  }
086}