1 /*
2  * Copyright (C) 2012 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.io;
18 
19 import java.io.File;
20 import java.io.IOException;
21 
22 /**
23  * A test factory for byte or char sources or sinks. In addition to creating sources or sinks, the
24  * factory specifies what content should be expected to be read from a source or contained in a sink
25  * given the content data that was used to create the source or that was written to the sink.
26  *
27  * <p>A single {@code SourceSinkFactory} implementation generally corresponds to one specific way of
28  * creating a source or sink, such as {@link Files#asByteSource(File)}. Implementations of
29  * {@code SourceSinkFactory} for common.io are found in {@link SourceSinkFactories}.
30  *
31  * @param <S> the source or sink type
32  * @param <T> the data type (byte[] or String)
33  * @author Colin Decker
34  */
35 public interface SourceSinkFactory<S, T> {
36 
37   /**
38    * Returns the data to expect the source or sink to contain given the data that was used to create
39    * the source or written to the sink. Typically, this will just return the input directly, but in
40    * some cases it may alter the input. For example, if the factory returns a sliced view of a
41    * source created with some given bytes, this method would return a subsequence of the given
42    * (byte[]) data.
43    */
getExpected(T data)44   T getExpected(T data);
45 
46   /**
47    * Cleans up anything created when creating the source or sink.
48    */
tearDown()49   public abstract void tearDown() throws IOException;
50 
51   /**
52    * Factory for byte or char sources.
53    */
54   public interface SourceFactory<S, T> extends SourceSinkFactory<S, T> {
55 
56     /**
57      * Creates a new source containing some or all of the given data.
58      */
createSource(T data)59     S createSource(T data) throws IOException;
60   }
61 
62   /**
63    * Factory for byte or char sinks.
64    */
65   public interface SinkFactory<S, T> extends SourceSinkFactory<S, T> {
66 
67     /**
68      * Creates a new sink.
69      */
createSink()70     S createSink() throws IOException;
71 
72     /**
73      * Gets the current content of the created sink.
74      */
getSinkContents()75     T getSinkContents() throws IOException;
76   }
77 
78   /**
79    * Factory for {@link ByteSource} instances.
80    */
81   public interface ByteSourceFactory extends SourceFactory<ByteSource, byte[]> {
82   }
83 
84   /**
85    * Factory for {@link ByteSink} instances.
86    */
87   public interface ByteSinkFactory extends SinkFactory<ByteSink, byte[]> {
88   }
89 
90   /**
91    * Factory for {@link CharSource} instances.
92    */
93   public interface CharSourceFactory extends SourceFactory<CharSource, String> {
94   }
95 
96   /**
97    * Factory for {@link CharSink} instances.
98    */
99   public interface CharSinkFactory extends SinkFactory<CharSink, String> {
100   }
101 }
102