blob: 0f7400e4c806e48c1e0b0a8eac5d85f1cda49fc6 [file] [log] [blame]
Andreas Wundsama3a1cfe2014-05-30 16:14:38 -07001package org.projectfloodlight.openflow.util;
2
3import java.util.List;
4import java.util.SortedSet;
5
Andreas Wundsam8e92b972014-06-05 15:44:13 -07006import javax.annotation.Nullable;
7
Andreas Wundsama3a1cfe2014-05-30 16:14:38 -07008import org.projectfloodlight.openflow.types.PrimitiveSinkable;
9
10import com.google.common.hash.PrimitiveSink;
11
12/** Utility methods for dumping collections into primitive sinks.
13 *
14 * @author Andreas Wundsam <andreas.wundsam@bigswitch.com>
15 */
16public class PrimitiveSinkUtils {
17 private PrimitiveSinkUtils() {}
18
Andreas Wundsam8e92b972014-06-05 15:44:13 -070019 /** puts a nullable element into a primitive sink. The entry is terminated by a null byte
20 * to disambiguate null elements.
21 *
22 * @param sink the sink to put the object
23 * @param nullableObj the nullable object
24 */
25 public static void putNullableTo(PrimitiveSink sink,
26 @Nullable PrimitiveSinkable nullableObj) {
27 if(nullableObj != null)
28 nullableObj.putTo(sink);
29
30 // terminate this object representation by a null byte. this ensures that we get
31 // unique digests even if some values are null
32 sink.putByte((byte) 0);
33 }
34
35 /** puts the elements of a sorted set into the {@link PrimitiveSink}. Does not support null
36 * elements. The elements are assumed to be self-delimitating.
37 *
38 * @param sink
39 * @param set
40 */
Andreas Wundsama3a1cfe2014-05-30 16:14:38 -070041 public static void putSortedSetTo(PrimitiveSink sink,
42 SortedSet<? extends PrimitiveSinkable> set) {
43 for(PrimitiveSinkable e: set) {
44 e.putTo(sink);
45 }
46 }
47
Andreas Wundsam8e92b972014-06-05 15:44:13 -070048 /** puts the elements of a list into the {@link PrimitiveSink}. Does not support null
49 * elements. The elements are assumed to be self-delimitating.
50 *
51 * @param sink
52 * @param set
53 */
Andreas Wundsama3a1cfe2014-05-30 16:14:38 -070054 public static void putListTo(PrimitiveSink sink,
55 List<? extends PrimitiveSinkable> set) {
56 for(PrimitiveSinkable e: set) {
57 e.putTo(sink);
58 }
59 }
60}