guava

Форк
0
138 строк · 3.8 Кб
1
/*
2
 * Copyright (C) 2007 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.AbstractCollection;
21
import java.util.Arrays;
22
import java.util.Collection;
23
import java.util.Iterator;
24
import org.checkerframework.checker.nullness.qual.NonNull;
25
import org.checkerframework.checker.nullness.qual.Nullable;
26

27
/**
28
 * A simplistic collection which implements only the bare minimum allowed by the spec, and throws
29
 * exceptions whenever it can.
30
 *
31
 * @author Kevin Bourrillion
32
 */
33
@GwtCompatible
34
@ElementTypesAreNonnullByDefault
35
public class MinimalCollection<E extends @Nullable Object> extends AbstractCollection<E> {
36
  // TODO: expose allow nulls parameter?
37

38
  public static <E extends @Nullable Object> MinimalCollection<E> of(E... contents) {
39
    return new MinimalCollection<>(Object.class, true, contents);
40
  }
41

42
  // TODO: use this
43
  public static <E extends @Nullable Object> MinimalCollection<E> ofClassAndContents(
44
      Class<? super @NonNull E> type, E... contents) {
45
    return new MinimalCollection<>(type, true, contents);
46
  }
47

48
  private final E[] contents;
49
  private final Class<? super @NonNull E> type;
50
  private final boolean allowNulls;
51

52
  // Package-private so that it can be extended.
53
  MinimalCollection(Class<? super @NonNull E> type, boolean allowNulls, E... contents) {
54
    // TODO: consider making it shuffle the contents to test iteration order.
55
    this.contents = Platform.clone(contents);
56
    this.type = type;
57
    this.allowNulls = allowNulls;
58

59
    if (!allowNulls) {
60
      for (Object element : contents) {
61
        if (element == null) {
62
          throw new NullPointerException();
63
        }
64
      }
65
    }
66
  }
67

68
  @Override
69
  public int size() {
70
    return contents.length;
71
  }
72

73
  @Override
74
  public boolean contains(@Nullable Object object) {
75
    if (!allowNulls) {
76
      // behave badly
77
      if (object == null) {
78
        throw new NullPointerException();
79
      }
80
    }
81
    Platform.checkCast(type, object); // behave badly
82
    return Arrays.asList(contents).contains(object);
83
  }
84

85
  @Override
86
  public boolean containsAll(Collection<?> collection) {
87
    if (!allowNulls) {
88
      for (Object object : collection) {
89
        // behave badly
90
        if (object == null) {
91
          throw new NullPointerException();
92
        }
93
      }
94
    }
95
    return super.containsAll(collection);
96
  }
97

98
  @Override
99
  public Iterator<E> iterator() {
100
    return Arrays.asList(contents).iterator();
101
  }
102

103
  @Override
104
  public @Nullable Object[] toArray() {
105
    @Nullable Object[] result = new @Nullable Object[contents.length];
106
    System.arraycopy(contents, 0, result, 0, contents.length);
107
    return result;
108
  }
109

110
  /*
111
   * a "type A" unmodifiable collection freaks out proactively, even if there
112
   * wasn't going to be any actual work to do anyway
113
   */
114

115
  @Override
116
  public boolean addAll(Collection<? extends E> elementsToAdd) {
117
    throw up();
118
  }
119

120
  @Override
121
  public boolean removeAll(Collection<?> elementsToRemove) {
122
    throw up();
123
  }
124

125
  @Override
126
  public boolean retainAll(Collection<?> elementsToRetain) {
127
    throw up();
128
  }
129

130
  @Override
131
  public void clear() {
132
    throw up();
133
  }
134

135
  private static UnsupportedOperationException up() {
136
    throw new UnsupportedOperationException();
137
  }
138
}
139

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

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

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

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