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