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 {@code
29  * 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   /** Cleans up anything created when creating the source or sink. */
tearDown()47   public abstract void tearDown() throws IOException;
48 
49   /** Factory for byte or char sources. */
50   public interface SourceFactory<S, T> extends SourceSinkFactory<S, T> {
51 
52     /** Creates a new source containing some or all of the given data. */
createSource(T data)53     S createSource(T data) throws IOException;
54   }
55 
56   /** Factory for byte or char sinks. */
57   public interface SinkFactory<S, T> extends SourceSinkFactory<S, T> {
58 
59     /** Creates a new sink. */
createSink()60     S createSink() throws IOException;
61 
62     /** Gets the current content of the created sink. */
getSinkContents()63     T getSinkContents() throws IOException;
64   }
65 
66   /** Factory for {@link ByteSource} instances. */
67   public interface ByteSourceFactory extends SourceFactory<ByteSource, byte[]> {}
68 
69   /** Factory for {@link ByteSink} instances. */
70   public interface ByteSinkFactory extends SinkFactory<ByteSink, byte[]> {}
71 
72   /** Factory for {@link CharSource} instances. */
73   public interface CharSourceFactory extends SourceFactory<CharSource, String> {}
74 
75   /** Factory for {@link CharSink} instances. */
76   public interface CharSinkFactory extends SinkFactory<CharSink, String> {}
77 }
78