guava

Форк
0
/
ReserializingTestCollectionGenerator.java 
80 строк · 2.4 Кб
1
/*
2
 * Copyright (C) 2008 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.GwtIncompatible;
20
import java.io.ByteArrayInputStream;
21
import java.io.ByteArrayOutputStream;
22
import java.io.IOException;
23
import java.io.ObjectInputStream;
24
import java.io.ObjectOutputStream;
25
import java.util.Collection;
26
import java.util.List;
27

28
/**
29
 * Reserializes the sets created by another test set generator.
30
 *
31
 * <p>TODO: make CollectionTestSuiteBuilder test reserialized collections
32
 *
33
 * @author Jesse Wilson
34
 */
35
@GwtIncompatible
36
public class ReserializingTestCollectionGenerator<E> implements TestCollectionGenerator<E> {
37
  private final TestCollectionGenerator<E> delegate;
38

39
  ReserializingTestCollectionGenerator(TestCollectionGenerator<E> delegate) {
40
    this.delegate = delegate;
41
  }
42

43
  public static <E> ReserializingTestCollectionGenerator<E> newInstance(
44
      TestCollectionGenerator<E> delegate) {
45
    return new ReserializingTestCollectionGenerator<>(delegate);
46
  }
47

48
  @Override
49
  public Collection<E> create(Object... elements) {
50
    return reserialize(delegate.create(elements));
51
  }
52

53
  @SuppressWarnings("unchecked")
54
  static <T> T reserialize(T object) {
55
    try {
56
      ByteArrayOutputStream bytes = new ByteArrayOutputStream();
57
      ObjectOutputStream out = new ObjectOutputStream(bytes);
58
      out.writeObject(object);
59
      ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()));
60
      return (T) in.readObject();
61
    } catch (IOException | ClassNotFoundException e) {
62
      throw new AssertionError(e);
63
    }
64
  }
65

66
  @Override
67
  public SampleElements<E> samples() {
68
    return delegate.samples();
69
  }
70

71
  @Override
72
  public E[] createArray(int length) {
73
    return delegate.createArray(length);
74
  }
75

76
  @Override
77
  public Iterable<E> order(List<E> insertionOrder) {
78
    return delegate.order(insertionOrder);
79
  }
80
}
81

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

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

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

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