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 @Override 043 public void funnel(byte[] from, PrimitiveSink into) { 044 into.putBytes(from); 045 } 046 047 @Override 048 public String toString() { 049 return "Funnels.byteArrayFunnel()"; 050 } 051 } 052 053 /** 054 * Returns a funnel that extracts the characters from a {@code CharSequence}, a character at a 055 * time, without performing any encoding. If you need to use a specific encoding, use {@link 056 * Funnels#stringFunnel(Charset)} instead. 057 * 058 * @since 15.0 (since 11.0 as {@code Funnels.stringFunnel()}. 059 */ 060 public static Funnel<CharSequence> unencodedCharsFunnel() { 061 return UnencodedCharsFunnel.INSTANCE; 062 } 063 064 private enum UnencodedCharsFunnel implements Funnel<CharSequence> { 065 INSTANCE; 066 067 @Override 068 public void funnel(CharSequence from, PrimitiveSink into) { 069 into.putUnencodedChars(from); 070 } 071 072 @Override 073 public String toString() { 074 return "Funnels.unencodedCharsFunnel()"; 075 } 076 } 077 078 /** 079 * Returns a funnel that encodes the characters of a {@code CharSequence} with the specified 080 * {@code Charset}. 081 * 082 * @since 15.0 083 */ 084 public static Funnel<CharSequence> stringFunnel(Charset charset) { 085 return new StringCharsetFunnel(charset); 086 } 087 088 private static class StringCharsetFunnel implements Funnel<CharSequence>, Serializable { 089 private final Charset charset; 090 091 StringCharsetFunnel(Charset charset) { 092 this.charset = Preconditions.checkNotNull(charset); 093 } 094 095 @Override 096 public void funnel(CharSequence from, PrimitiveSink into) { 097 into.putString(from, charset); 098 } 099 100 @Override 101 public String toString() { 102 return "Funnels.stringFunnel(" + charset.name() + ")"; 103 } 104 105 @Override 106 public boolean equals(@NullableDecl Object o) { 107 if (o instanceof StringCharsetFunnel) { 108 StringCharsetFunnel funnel = (StringCharsetFunnel) o; 109 return this.charset.equals(funnel.charset); 110 } 111 return false; 112 } 113 114 @Override 115 public int hashCode() { 116 return StringCharsetFunnel.class.hashCode() ^ charset.hashCode(); 117 } 118 119 Object writeReplace() { 120 return new SerializedForm(charset); 121 } 122 123 private static class SerializedForm implements Serializable { 124 private final String charsetCanonicalName; 125 126 SerializedForm(Charset charset) { 127 this.charsetCanonicalName = charset.name(); 128 } 129 130 private Object readResolve() { 131 return stringFunnel(Charset.forName(charsetCanonicalName)); 132 } 133 134 private static final long serialVersionUID = 0; 135 } 136 } 137 138 /** 139 * Returns a funnel for integers. 140 * 141 * @since 13.0 142 */ 143 public static Funnel<Integer> integerFunnel() { 144 return IntegerFunnel.INSTANCE; 145 } 146 147 private enum IntegerFunnel implements Funnel<Integer> { 148 INSTANCE; 149 150 @Override 151 public void funnel(Integer from, PrimitiveSink into) { 152 into.putInt(from); 153 } 154 155 @Override 156 public String toString() { 157 return "Funnels.integerFunnel()"; 158 } 159 } 160 161 /** 162 * Returns a funnel that processes an {@code Iterable} by funneling its elements in iteration 163 * order with the specified funnel. No separators are added between the elements. 164 * 165 * @since 15.0 166 */ 167 public static <E> Funnel<Iterable<? extends E>> sequentialFunnel(Funnel<E> elementFunnel) { 168 return new SequentialFunnel<E>(elementFunnel); 169 } 170 171 private static class SequentialFunnel<E> implements Funnel<Iterable<? extends E>>, Serializable { 172 private final Funnel<E> elementFunnel; 173 174 SequentialFunnel(Funnel<E> elementFunnel) { 175 this.elementFunnel = Preconditions.checkNotNull(elementFunnel); 176 } 177 178 @Override 179 public void funnel(Iterable<? extends E> from, PrimitiveSink into) { 180 for (E e : from) { 181 elementFunnel.funnel(e, into); 182 } 183 } 184 185 @Override 186 public String toString() { 187 return "Funnels.sequentialFunnel(" + elementFunnel + ")"; 188 } 189 190 @Override 191 public boolean equals(@NullableDecl Object o) { 192 if (o instanceof SequentialFunnel) { 193 SequentialFunnel<?> funnel = (SequentialFunnel<?>) o; 194 return elementFunnel.equals(funnel.elementFunnel); 195 } 196 return false; 197 } 198 199 @Override 200 public int hashCode() { 201 return SequentialFunnel.class.hashCode() ^ elementFunnel.hashCode(); 202 } 203 } 204 205 /** 206 * Returns a funnel for longs. 207 * 208 * @since 13.0 209 */ 210 public static Funnel<Long> longFunnel() { 211 return LongFunnel.INSTANCE; 212 } 213 214 private enum LongFunnel implements Funnel<Long> { 215 INSTANCE; 216 217 @Override 218 public void funnel(Long from, PrimitiveSink into) { 219 into.putLong(from); 220 } 221 222 @Override 223 public String toString() { 224 return "Funnels.longFunnel()"; 225 } 226 } 227 228 /** 229 * Wraps a {@code PrimitiveSink} as an {@link OutputStream}, so it is easy to {@link Funnel#funnel 230 * funnel} an object to a {@code PrimitiveSink} if there is already a way to write the contents of 231 * the object to an {@code OutputStream}. 232 * 233 * <p>The {@code close} and {@code flush} methods of the returned {@code OutputStream} do nothing, 234 * and no method throws {@code IOException}. 235 * 236 * @since 13.0 237 */ 238 public static OutputStream asOutputStream(PrimitiveSink sink) { 239 return new SinkAsStream(sink); 240 } 241 242 private static class SinkAsStream extends OutputStream { 243 final PrimitiveSink sink; 244 245 SinkAsStream(PrimitiveSink sink) { 246 this.sink = Preconditions.checkNotNull(sink); 247 } 248 249 @Override 250 public void write(int b) { 251 sink.putByte((byte) b); 252 } 253 254 @Override 255 public void write(byte[] bytes) { 256 sink.putBytes(bytes); 257 } 258 259 @Override 260 public void write(byte[] bytes, int off, int len) { 261 sink.putBytes(bytes, off, len); 262 } 263 264 @Override 265 public String toString() { 266 return "Funnels.asOutputStream(" + sink + ")"; 267 } 268 } 269}