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