blob: 28eb3a4c87906823421b10673c095fb5b7eccd8b [file] [log] [blame]
tom0eb04ca2014-08-25 14:34:51 -07001package org.projectfloodlight.openflow.util;
2
3import java.util.List;
4import java.util.SortedSet;
5
6import javax.annotation.Nullable;
7
8import 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
19 /** puts a nullable String into a primitive sink. The entry is prepended by a 'presence'
20 * boolean bit and the string length;
21 *
22 *
23 * @param sink the sink to put the object
24 * @param nullableObj the potentially null string to put in the sink
25 */
26 public static void putNullableStringTo(PrimitiveSink sink,
27 @Nullable CharSequence nullableChars) {
28
29 sink.putBoolean(nullableChars != null);
30 if(nullableChars != null) {
31 sink.putInt(nullableChars.length());
32 sink.putUnencodedChars(nullableChars);
33 }
34 }
35
36 /** puts a nullable element into a primitive sink. The entry is prepended by a 'present' bit.
37 *
38 * @param sink the sink to put the object
39 * @param nullableObj the nullable object
40 */
41 public static void putNullableTo(PrimitiveSink sink,
42 @Nullable PrimitiveSinkable nullableObj) {
43 sink.putBoolean(nullableObj != null);
44 if(nullableObj != null)
45 nullableObj.putTo(sink);
46 }
47
48 /** puts the elements of a sorted set 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 */
54 public static void putSortedSetTo(PrimitiveSink sink,
55 SortedSet<? extends PrimitiveSinkable> set) {
56 sink.putInt(set.size());
57 for(PrimitiveSinkable e: set) {
58 e.putTo(sink);
59 }
60 }
61
62 /** puts the elements of a list into the {@link PrimitiveSink}. Does not support null
63 * elements. The elements are assumed to be self-delimitating.
64 *
65 * @param sink
66 * @param set
67 */
68 public static void putListTo(PrimitiveSink sink,
69 List<? extends PrimitiveSinkable> set) {
70 sink.putInt(set.size());
71 for(PrimitiveSinkable e: set) {
72 e.putTo(sink);
73 }
74 }
75}