1 /* 2 * Copyright (C) 2011 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package com.google.common.hash; 16 17 import com.google.common.annotations.Beta; 18 19 /** 20 * Funnels for common types. All implementations are serializable. 21 * 22 * @author Dimitris Andreou 23 * @since 11.0 24 */ 25 @Beta 26 public final class Funnels { Funnels()27 private Funnels() {} 28 29 /** 30 * Returns a funnel that extracts the bytes from a {@code byte} array. 31 */ byteArrayFunnel()32 public static Funnel<byte[]> byteArrayFunnel() { 33 return ByteArrayFunnel.INSTANCE; 34 } 35 36 private enum ByteArrayFunnel implements Funnel<byte[]> { 37 INSTANCE; 38 funnel(byte[] from, Sink into)39 public void funnel(byte[] from, Sink into) { 40 into.putBytes(from); 41 } 42 toString()43 @Override public String toString() { 44 return "Funnels.byteArrayFunnel()"; 45 } 46 } 47 48 /** 49 * Returns a funnel that extracts the characters from a {@code CharSequence}. 50 */ stringFunnel()51 public static Funnel<CharSequence> stringFunnel() { 52 return StringFunnel.INSTANCE; 53 } 54 55 private enum StringFunnel implements Funnel<CharSequence> { 56 INSTANCE; 57 funnel(CharSequence from, Sink into)58 public void funnel(CharSequence from, Sink into) { 59 into.putString(from); 60 } 61 toString()62 @Override public String toString() { 63 return "Funnels.stringFunnel()"; 64 } 65 } 66 } 67