1 /** 2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 * SPDX-License-Identifier: Apache-2.0. 4 */ 5 6 package software.amazon.awssdk.crt.mqtt; 7 8 import java.util.Map; 9 import java.util.function.Function; 10 import java.util.stream.Collectors; 11 import java.util.stream.Stream; 12 13 /** 14 * Quality of Service associated with a publish action or subscription [MQTT-4.3]. 15 */ 16 public enum QualityOfService { 17 /** 18 * Message will be delivered at most once, or may not be delivered at all. There will be no ACK, and the message 19 * will not be stored. 20 */ 21 AT_MOST_ONCE(0), 22 23 /** 24 * Message will be delivered at least once. It may be resent multiple times if errors occur before an ACK is 25 * returned to the sender. The message will be stored in case it has to be re-sent. This is the most common QualityOfService. 26 */ 27 AT_LEAST_ONCE(1), 28 29 /** 30 * The message is always delivered exactly once. This is the safest, but slowest QualityOfService, because multiple levels 31 * of handshake must happen to guarantee no duplication of messages. 32 */ 33 EXACTLY_ONCE(2); 34 /* reserved = 3 */ 35 36 private int qos; 37 QualityOfService(int value)38 QualityOfService(int value) { 39 qos = value; 40 } 41 42 /** 43 * @return the native enum integer value associated with this Java enum value 44 */ getValue()45 public int getValue() { 46 return qos; 47 } 48 49 /** 50 * Creates a Java QualityOfService enum value from a native integer value 51 * @param value native integer value for quality of service 52 * @return a new QualityOfService value 53 */ getEnumValueFromInteger(int value)54 public static QualityOfService getEnumValueFromInteger(int value) { 55 QualityOfService enumValue = enumMapping.get(value); 56 if (enumValue != null) { 57 return enumValue; 58 } 59 throw new RuntimeException("Illegal QualityOfService"); 60 } 61 buildEnumMapping()62 private static Map<Integer, QualityOfService> buildEnumMapping() { 63 return Stream.of(QualityOfService.values()) 64 .collect(Collectors.toMap(QualityOfService::getValue, Function.identity())); 65 } 66 67 private static Map<Integer, QualityOfService> enumMapping = buildEnumMapping(); 68 } 69