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