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 | 8e92b97 | 2014-06-05 15:44:13 -0700 | [diff] [blame^] | 19 | /** 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 Wundsam | a3a1cfe | 2014-05-30 16:14:38 -0700 | [diff] [blame] | 41 | public static void putSortedSetTo(PrimitiveSink sink, |
| 42 | SortedSet<? extends PrimitiveSinkable> set) { |
| 43 | for(PrimitiveSinkable e: set) { |
| 44 | e.putTo(sink); |
| 45 | } |
| 46 | } |
| 47 | |
Andreas Wundsam | 8e92b97 | 2014-06-05 15:44:13 -0700 | [diff] [blame^] | 48 | /** 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 Wundsam | a3a1cfe | 2014-05-30 16:14:38 -0700 | [diff] [blame] | 54 | public static void putListTo(PrimitiveSink sink, |
| 55 | List<? extends PrimitiveSinkable> set) { |
| 56 | for(PrimitiveSinkable e: set) { |
| 57 | e.putTo(sink); |
| 58 | } |
| 59 | } |
| 60 | } |