guava

Форк
0
85 строк · 2.8 Кб
1
/*
2
 * Copyright (C) 2009 The Guava Authors
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 * http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package com.google.common.collect.testing;
18

19
import com.google.common.annotations.GwtCompatible;
20
import java.util.ArrayList;
21
import java.util.Arrays;
22
import java.util.Collection;
23
import java.util.List;
24
import java.util.Set;
25
import org.checkerframework.checker.nullness.qual.NonNull;
26
import org.checkerframework.checker.nullness.qual.Nullable;
27

28
/**
29
 * A simplistic set which implements the bare minimum so that it can be used in tests without
30
 * relying on any specific Set implementations. Slow. Explicitly allows null elements so that they
31
 * can be used in the testers.
32
 *
33
 * @author Regina O'Dell
34
 */
35
@GwtCompatible
36
@ElementTypesAreNonnullByDefault
37
public class MinimalSet<E extends @Nullable Object> extends MinimalCollection<E> implements Set<E> {
38

39
  @SuppressWarnings("unchecked") // empty Object[] as E[]
40
  public static <E extends @Nullable Object> MinimalSet<E> of(E... contents) {
41
    return ofClassAndContents(Object.class, (E[]) new Object[0], Arrays.asList(contents));
42
  }
43

44
  @SuppressWarnings("unchecked") // empty Object[] as E[]
45
  public static <E extends @Nullable Object> MinimalSet<E> from(Collection<? extends E> contents) {
46
    return ofClassAndContents(Object.class, (E[]) new Object[0], contents);
47
  }
48

49
  public static <E extends @Nullable Object> MinimalSet<E> ofClassAndContents(
50
      Class<? super @NonNull E> type, E[] emptyArrayForContents, Iterable<? extends E> contents) {
51
    List<E> setContents = new ArrayList<>();
52
    for (E e : contents) {
53
      if (!setContents.contains(e)) {
54
        setContents.add(e);
55
      }
56
    }
57
    return new MinimalSet<>(type, setContents.toArray(emptyArrayForContents));
58
  }
59

60
  private MinimalSet(Class<? super @NonNull E> type, E... contents) {
61
    super(type, true, contents);
62
  }
63

64
  /*
65
   * equals() and hashCode() are more specific in the Set contract.
66
   */
67

68
  @Override
69
  public boolean equals(@Nullable Object object) {
70
    if (object instanceof Set) {
71
      Set<?> that = (Set<?>) object;
72
      return (this.size() == that.size()) && this.containsAll(that);
73
    }
74
    return false;
75
  }
76

77
  @Override
78
  public int hashCode() {
79
    int hashCodeSum = 0;
80
    for (Object o : this) {
81
      hashCodeSum += (o == null) ? 0 : o.hashCode();
82
    }
83
    return hashCodeSum;
84
  }
85
}
86

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.