Andreas Wundsam | a3a1cfe | 2014-05-30 16:14:38 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.util; |
| 2 | |
| 3 | import java.util.List; |
| 4 | import java.util.SortedSet; |
| 5 | |
Andreas Wundsam | 8e92b97 | 2014-06-05 15:44:13 -0700 | [diff] [blame] | 6 | import javax.annotation.Nullable; |
| 7 | |
Andreas Wundsam | a3a1cfe | 2014-05-30 16:14:38 -0700 | [diff] [blame] | 8 | import org.projectfloodlight.openflow.types.PrimitiveSinkable; |
| 9 | |
| 10 | import 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 | */ |
| 16 | public class PrimitiveSinkUtils { |
| 17 | private PrimitiveSinkUtils() {} |
| 18 | |
Andreas Wundsam | a224b13 | 2014-06-05 16:39:01 -0700 | [diff] [blame] | 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. |
Andreas Wundsam | 8e92b97 | 2014-06-05 15:44:13 -0700 | [diff] [blame] | 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) { |
Andreas Wundsam | a224b13 | 2014-06-05 16:39:01 -0700 | [diff] [blame] | 43 | sink.putBoolean(nullableObj != null); |
Andreas Wundsam | 8e92b97 | 2014-06-05 15:44:13 -0700 | [diff] [blame] | 44 | if(nullableObj != null) |
| 45 | nullableObj.putTo(sink); |
Andreas Wundsam | 8e92b97 | 2014-06-05 15:44:13 -0700 | [diff] [blame] | 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 | */ |
Andreas Wundsam | a3a1cfe | 2014-05-30 16:14:38 -0700 | [diff] [blame] | 54 | public static void putSortedSetTo(PrimitiveSink sink, |
| 55 | SortedSet<? extends PrimitiveSinkable> set) { |
Andreas Wundsam | a224b13 | 2014-06-05 16:39:01 -0700 | [diff] [blame] | 56 | sink.putInt(set.size()); |
Andreas Wundsam | a3a1cfe | 2014-05-30 16:14:38 -0700 | [diff] [blame] | 57 | for(PrimitiveSinkable e: set) { |
| 58 | e.putTo(sink); |
| 59 | } |
| 60 | } |
| 61 | |
Andreas Wundsam | 8e92b97 | 2014-06-05 15:44:13 -0700 | [diff] [blame] | 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 | */ |
Andreas Wundsam | a3a1cfe | 2014-05-30 16:14:38 -0700 | [diff] [blame] | 68 | public static void putListTo(PrimitiveSink sink, |
| 69 | List<? extends PrimitiveSinkable> set) { |
Andreas Wundsam | a224b13 | 2014-06-05 16:39:01 -0700 | [diff] [blame] | 70 | sink.putInt(set.size()); |
Andreas Wundsam | a3a1cfe | 2014-05-30 16:14:38 -0700 | [diff] [blame] | 71 | for(PrimitiveSinkable e: set) { |
| 72 | e.putTo(sink); |
| 73 | } |
| 74 | } |
| 75 | } |