001/* 002 * Copyright (C) 2011 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 */ 014 015package com.google.common.hash; 016 017import com.google.common.annotations.Beta; 018import com.google.common.base.Preconditions; 019import java.io.OutputStream; 020import java.io.Serializable; 021import java.nio.charset.Charset; 022import org.checkerframework.checker.nullness.compatqual.NullableDecl; 023 024/** 025 * Funnels for common types. All implementations are serializable. 026 * 027 * @author Dimitris Andreou 028 * @since 11.0 029 */ 030@Beta 031public final class Funnels { 032 private Funnels() {} 033 034 /** Returns a funnel that extracts the bytes from a {@code byte} array. */ 035 public static Funnel<byte[]> byteArrayFunnel() { 036 return ByteArrayFunnel.INSTANCE; 037 } 038 039 private enum ByteArrayFunnel implements Funnel<byte[]> { 040 INSTANCE; 041 042 public void funnel(byte[] from, PrimitiveSink into) { 043 into.putBytes(from); 044 } 045 046 @Override 047 public String toString() { 048 return "Funnels.byteArrayFunnel()"; 049 } 050 } 051 052 /** 053 * Returns a funnel that extracts the characters from a {@code CharSequence}, a character at a 054 * time, without performing any encoding. If you need to use a specific encoding, use {@link 055 * Funnels#stringFunnel(Charset)} instead. 056 * 057 * @since 15.0 (since 11.0 as {@code Funnels.stringFunnel()}. 058 */ 059 public static Funnel<CharSequence> unencodedCharsFunnel() { 060 return UnencodedCharsFunnel.INSTANCE; 061 } 062 063 private enum UnencodedCharsFunnel implements Funnel<CharSequence> { 064 INSTANCE; 065 066 public void funnel(CharSequence from, PrimitiveSink into) { 067 into.putUnencodedChars(from); 068 } 069 070 @Override 071 public String toString() { 072 return "Funnels.unencodedCharsFunnel()"; 073 } 074 } 075 076 /** 077 * Returns a funnel that encodes the characters of a {@code CharSequence} with the specified 078 * {@code Charset}. 079 * 080 * @since 15.0 081 */ 082 public static Funnel<CharSequence> stringFunnel(Charset charset) { 083 return new StringCharsetFunnel(charset); 084 } 085 086 private static class StringCharsetFunnel implements Funnel<CharSequence>, Serializable { 087 private final Charset charset; 088 089 StringCharsetFunnel(Charset charset) { 090 this.charset = Preconditions.checkNotNull(charset); 091 } 092 093 public void funnel(CharSequence from, PrimitiveSink into) { 094 into.putString(from, charset); 095 } 096 097 @Override 098 public String toString() { 099 return "Funnels.stringFunnel(" + charset.name() + ")"; 100 } 101 102 @Override 103 public boolean equals(@NullableDecl Object o) { 104 if (o instanceof StringCharsetFunnel) { 105 StringCharsetFunnel funnel = (StringCharsetFunnel) o; 106 return this.charset.equals(funnel.charset); 107 } 108 return false; 109 } 110 111 @Override 112 public int hashCode() { 113 return StringCharsetFunnel.class.hashCode() ^ charset.hashCode(); 114 } 115 116 Object writeReplace() { 117 return new SerializedForm(charset); 118 } 119 120 private static class SerializedForm implements Serializable { 121 private final String charsetCanonicalName; 122 123 SerializedForm(Charset charset) { 124 this.charsetCanonicalName = charset.name(); 125 } 126 127 private Object readResolve() { 128 return stringFunnel(Charset.forName(charsetCanonicalName)); 129 } 130 131 private static final long serialVersionUID = 0; 132 } 133 } 134 135 /** 136 * Returns a funnel for integers. 137 * 138 * @since 13.0 139 */ 140 public static Funnel<Integer> integerFunnel() { 141 return IntegerFunnel.INSTANCE; 142 } 143 144 private enum IntegerFunnel implements Funnel<Integer> { 145 INSTANCE; 146 147 public void funnel(Integer from, PrimitiveSink into) { 148 into.putInt(from); 149 } 150 151 @Override 152 public String toString() { 153 return "Funnels.integerFunnel()"; 154 } 155 } 156 157 /** 158 * Returns a funnel that processes an {@code Iterable} by funneling its elements in iteration 159 * order with the specified funnel. No separators are added between the elements. 160 * 161 * @since 15.0 162 */ 163 public static <E> Funnel<Iterable<? extends E>> sequentialFunnel(Funnel<E> elementFunnel) { 164 return new SequentialFunnel<E>(elementFunnel); 165 } 166 167 private static class SequentialFunnel<E> implements Funnel<Iterable<? extends E>>, Serializable { 168 private final Funnel<E> elementFunnel; 169 170 SequentialFunnel(Funnel<E> elementFunnel) { 171 this.elementFunnel = Preconditions.checkNotNull(elementFunnel); 172 } 173 174 public void funnel(Iterable<? extends E> from, PrimitiveSink into) { 175 for (E e : from) { 176 elementFunnel.funnel(e, into); 177 } 178 } 179 180 @Override 181 public String toString() { 182 return "Funnels.sequentialFunnel(" + elementFunnel + ")"; 183 } 184 185 @Override 186 public boolean equals(@NullableDecl Object o) { 187 if (o instanceof SequentialFunnel) { 188 SequentialFunnel<?> funnel = (SequentialFunnel<?>) o; 189 return elementFunnel.equals(funnel.elementFunnel); 190 } 191 return false; 192 } 193 194 @Override 195 public int hashCode() { 196 return SequentialFunnel.class.hashCode() ^ elementFunnel.hashCode(); 197 } 198 } 199 200 /** 201 * Returns a funnel for longs. 202 * 203 * @since 13.0 204 */ 205 public static Funnel<Long> longFunnel() { 206 return LongFunnel.INSTANCE; 207 } 208 209 private enum LongFunnel implements Funnel<Long> { 210 INSTANCE; 211 212 public void funnel(Long from, PrimitiveSink into) { 213 into.putLong(from); 214 } 215 216 @Override 217 public String toString() { 218 return "Funnels.longFunnel()"; 219 } 220 } 221 222 /** 223 * Wraps a {@code PrimitiveSink} as an {@link OutputStream}, so it is easy to {@link Funnel#funnel 224 * funnel} an object to a {@code PrimitiveSink} if there is already a way to write the contents of 225 * the object to an {@code OutputStream}. 226 * 227 * <p>The {@code close} and {@code flush} methods of the returned {@code OutputStream} do nothing, 228 * and no method throws {@code IOException}. 229 * 230 * @since 13.0 231 */ 232 public static OutputStream asOutputStream(PrimitiveSink sink) { 233 return new SinkAsStream(sink); 234 } 235 236 private static class SinkAsStream extends OutputStream { 237 final PrimitiveSink sink; 238 239 SinkAsStream(PrimitiveSink sink) { 240 this.sink = Preconditions.checkNotNull(sink); 241 } 242 243 @Override 244 public void write(int b) { 245 sink.putByte((byte) b); 246 } 247 248 @Override 249 public void write(byte[] bytes) { 250 sink.putBytes(bytes); 251 } 252 253 @Override 254 public void write(byte[] bytes, int off, int len) { 255 sink.putBytes(bytes, off, len); 256 } 257 258 @Override 259 public String toString() { 260 return "Funnels.asOutputStream(" + sink + ")"; 261 } 262 } 263}