Skip to content

Commit 5abb71a

Browse files
committed
#intFunnel() and #longFunnel() funnels, and a PrimitiveSink --> OutputStream
wrapper. ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=30307214
1 parent 68cc7d9 commit 5abb71a

3 files changed

Lines changed: 132 additions & 20 deletions

File tree

guava-tests/pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<artifactId>guava-tests</artifactId>
1111
<name>Guava Unit Tests</name>
1212
<description>
13-
The unit tests for the Guava libraries - separated into a
13+
The unit tests for the Guava libraries - separated into a
1414
separate artifact to allow for the testlibs to depend on guava
1515
itself.
1616
</description>
@@ -33,6 +33,12 @@
3333
<version>3.0</version>
3434
<scope>test</scope>
3535
</dependency>
36+
<dependency>
37+
<groupId>org.mockito</groupId>
38+
<artifactId>mockito-core</artifactId>
39+
<version>1.8.5</version>
40+
<scope>test</scope>
41+
</dependency>
3642
<dependency>
3743
<groupId>org.junit.contrib</groupId>
3844
<artifactId>truth</artifactId>

guava-tests/test/com/google/common/hash/FunnelsTest.java

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616

1717
package com.google.common.hash;
1818

19+
import static org.mockito.Mockito.mock;
20+
import static org.mockito.Mockito.verify;
21+
1922
import com.google.common.hash.AbstractStreamingHashFunction.AbstractStreamingHasher;
2023

2124
import junit.framework.TestCase;
2225

23-
import org.easymock.EasyMock;
24-
26+
import java.io.OutputStream;
2527
import java.nio.ByteBuffer;
2628

2729
/**
@@ -31,35 +33,45 @@
3133
*/
3234
public class FunnelsTest extends TestCase {
3335
public void testForBytes() {
34-
PrimitiveSink bytePrimitiveSink = EasyMock.createMock(PrimitiveSink.class);
35-
36-
EasyMock.expect(bytePrimitiveSink.putBytes(EasyMock.aryEq(new byte[] { 4, 3, 2, 1})))
37-
.andReturn(bytePrimitiveSink).once();
38-
EasyMock.replay(bytePrimitiveSink);
39-
40-
Funnels.byteArrayFunnel().funnel(new byte[]{4, 3, 2, 1}, bytePrimitiveSink);
41-
42-
EasyMock.verify(bytePrimitiveSink);
36+
PrimitiveSink bytePrimitiveSink = mock(PrimitiveSink.class);
37+
Funnels.byteArrayFunnel().funnel(new byte[] { 4, 3, 2, 1 }, bytePrimitiveSink);
38+
verify(bytePrimitiveSink).putBytes(new byte[] { 4, 3, 2, 1 });
4339
}
4440

4541
public void testForBytes_null() {
4642
assertNullsThrowException(Funnels.byteArrayFunnel());
4743
}
4844

4945
public void testForStrings() {
46+
PrimitiveSink bytePrimitiveSink = mock(PrimitiveSink.class);
47+
Funnels.stringFunnel().funnel("test", bytePrimitiveSink);
48+
verify(bytePrimitiveSink).putString("test");
49+
}
5050

51-
PrimitiveSink bytePrimitiveSink = EasyMock.createMock(PrimitiveSink.class);
51+
public void testForStrings_null() {
52+
assertNullsThrowException(Funnels.stringFunnel());
53+
}
5254

53-
EasyMock.expect(bytePrimitiveSink.putString("test")).andReturn(bytePrimitiveSink).once();
54-
EasyMock.replay(bytePrimitiveSink);
55+
public void testForInts() {
56+
Integer value = 1234;
57+
PrimitiveSink bytePrimitiveSink = mock(PrimitiveSink.class);
58+
Funnels.integerFunnel().funnel(value, bytePrimitiveSink);
59+
verify(bytePrimitiveSink).putInt(1234);
60+
}
5561

56-
Funnels.stringFunnel().funnel("test", bytePrimitiveSink);
62+
public void testForInts_null() {
63+
assertNullsThrowException(Funnels.integerFunnel());
64+
}
5765

58-
EasyMock.verify(bytePrimitiveSink);
66+
public void testForLongs() {
67+
Long value = 1234L;
68+
PrimitiveSink bytePrimitiveSink = mock(PrimitiveSink.class);
69+
Funnels.longFunnel().funnel(value, bytePrimitiveSink);
70+
verify(bytePrimitiveSink).putLong(1234);
5971
}
6072

61-
public void testForStrings_null() {
62-
assertNullsThrowException(Funnels.stringFunnel());
73+
public void testForLongs_null() {
74+
assertNullsThrowException(Funnels.longFunnel());
6375
}
6476

6577
private static void assertNullsThrowException(Funnel<?> funnel) {
@@ -77,4 +89,16 @@ private static void assertNullsThrowException(Funnel<?> funnel) {
7789
fail();
7890
} catch (NullPointerException ok) {}
7991
}
92+
93+
public void testAsOutputStream() throws Exception {
94+
PrimitiveSink sink = mock(PrimitiveSink.class);
95+
OutputStream out = Funnels.asOutputStream(sink);
96+
byte[] bytes = { 1, 2, 3, 4 };
97+
out.write(255);
98+
out.write(bytes);
99+
out.write(bytes, 1, 2);
100+
verify(sink).putByte((byte) 255);
101+
verify(sink).putBytes(bytes);
102+
verify(sink).putBytes(bytes, 1, 2);
103+
}
80104
}

guava/src/com/google/common/hash/Funnels.java

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
package com.google.common.hash;
1616

1717
import com.google.common.annotations.Beta;
18+
import com.google.common.base.Preconditions;
19+
20+
import java.io.OutputStream;
1821

1922
/**
2023
* Funnels for common types. All implementations are serializable.
21-
*
24+
*
2225
* @author Dimitris Andreou
2326
* @since 11.0
2427
*/
@@ -63,4 +66,83 @@ public void funnel(CharSequence from, PrimitiveSink into) {
6366
return "Funnels.stringFunnel()";
6467
}
6568
}
69+
70+
/**
71+
* Returns a funnel for integers.
72+
*
73+
* @since 13.0
74+
*/
75+
public static Funnel<Integer> integerFunnel() {
76+
return IntegerFunnel.INSTANCE;
77+
}
78+
79+
private enum IntegerFunnel implements Funnel<Integer> {
80+
INSTANCE;
81+
82+
public void funnel(Integer from, PrimitiveSink into) {
83+
into.putInt(from);
84+
}
85+
86+
@Override public String toString() {
87+
return "Funnels.integerFunnel()";
88+
}
89+
}
90+
91+
/**
92+
* Returns a funnel for longs.
93+
*
94+
* @since 13.0
95+
*/
96+
public static Funnel<Long> longFunnel() {
97+
return LongFunnel.INSTANCE;
98+
}
99+
100+
private enum LongFunnel implements Funnel<Long> {
101+
INSTANCE;
102+
103+
public void funnel(Long from, PrimitiveSink into) {
104+
into.putLong(from);
105+
}
106+
107+
@Override public String toString() {
108+
return "Funnels.longFunnel()";
109+
}
110+
}
111+
112+
/**
113+
* Wraps a {@code PrimitiveSink} as an {@link OutputStream}, so it is easy to
114+
* {@link Funnel#funnel funnel} an object to a {@code PrimitiveSink}
115+
* if there is already a way to write the contents of the object to an {@code OutputStream}.
116+
*
117+
* <p>The {@code close} and {@code flush} methods of the returned {@code OutputStream}
118+
* do nothing, and no method throws {@code IOException}.
119+
*
120+
* @since 13.0
121+
*/
122+
public static OutputStream asOutputStream(PrimitiveSink sink) {
123+
return new SinkAsStream(sink);
124+
}
125+
126+
private static class SinkAsStream extends OutputStream {
127+
final PrimitiveSink sink;
128+
SinkAsStream(PrimitiveSink sink) {
129+
this.sink = Preconditions.checkNotNull(sink);
130+
}
131+
132+
@Override public void write(int b) {
133+
sink.putByte((byte) b);
134+
}
135+
136+
@Override public void write(byte[] bytes) {
137+
sink.putBytes(bytes);
138+
}
139+
140+
@Override public void write(byte[] bytes, int off, int len) {
141+
sink.putBytes(bytes, off, len);
142+
}
143+
144+
@Override public String toString() {
145+
return "Funnels.asOutputStream(" + sink + ")";
146+
}
147+
}
66148
}

0 commit comments

Comments
 (0)