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 com.google.common.annotations.GwtCompatible;
020import java.util.ArrayList;
021import java.util.Arrays;
022import java.util.Collection;
023import java.util.List;
024import java.util.Set;
025import org.checkerframework.checker.nullness.qual.NonNull;
026import org.checkerframework.checker.nullness.qual.Nullable;
027
028/**
029 * A simplistic set which implements the bare minimum so that it can be used in tests without
030 * relying on any specific Set implementations. Slow. Explicitly allows null elements so that they
031 * can be used in the testers.
032 *
033 * @author Regina O'Dell
034 */
035@GwtCompatible
036@ElementTypesAreNonnullByDefault
037public class MinimalSet<E extends @Nullable Object> extends MinimalCollection<E> implements Set<E> {
038
039  @SuppressWarnings("unchecked") // empty Object[] as E[]
040  public static <E extends @Nullable Object> MinimalSet<E> of(E... contents) {
041    return ofClassAndContents(Object.class, (E[]) new Object[0], Arrays.asList(contents));
042  }
043
044  @SuppressWarnings("unchecked") // empty Object[] as E[]
045  public static <E extends @Nullable Object> MinimalSet<E> from(Collection<? extends E> contents) {
046    return ofClassAndContents(Object.class, (E[]) new Object[0], contents);
047  }
048
049  public static <E extends @Nullable Object> MinimalSet<E> ofClassAndContents(
050      Class<? super @NonNull E> type, E[] emptyArrayForContents, Iterable<? extends E> contents) {
051    List<E> setContents = new ArrayList<>();
052    for (E e : contents) {
053      if (!setContents.contains(e)) {
054        setContents.add(e);
055      }
056    }
057    return new MinimalSet<>(type, setContents.toArray(emptyArrayForContents));
058  }
059
060  private MinimalSet(Class<? super @NonNull E> type, E... contents) {
061    super(type, true, contents);
062  }
063
064  /*
065   * equals() and hashCode() are more specific in the Set contract.
066   */
067
068  @Override
069  public boolean equals(@Nullable Object object) {
070    if (object instanceof Set) {
071      Set<?> that = (Set<?>) object;
072      return (this.size() == that.size()) && this.containsAll(that);
073    }
074    return false;
075  }
076
077  @Override
078  public int hashCode() {
079    int hashCodeSum = 0;
080    for (Object o : this) {
081      hashCodeSum += (o == null) ? 0 : o.hashCode();
082    }
083    return hashCodeSum;
084  }
085}