2017-04-12 17:40:32 +02:00
|
|
|
/*
|
2018-04-27 20:17:47 +02:00
|
|
|
* Copyright (c) 2012-2018 The original author or authors
|
2017-04-12 17:40:32 +02:00
|
|
|
* ------------------------------------------------------
|
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
|
* are made available under the terms of the Eclipse Public License v1.0
|
|
|
|
|
* and Apache License v2.0 which accompanies this distribution.
|
|
|
|
|
*
|
|
|
|
|
* The Eclipse Public License is available at
|
|
|
|
|
* http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
|
*
|
|
|
|
|
* The Apache License v2.0 is available at
|
|
|
|
|
* http://www.opensource.org/licenses/apache2.0.php
|
|
|
|
|
*
|
|
|
|
|
* You may elect to redistribute this code under either of these licenses.
|
|
|
|
|
*/
|
2017-02-01 15:57:48 +01:00
|
|
|
|
2015-11-20 20:54:27 +01:00
|
|
|
package io.moquette.spi.impl;
|
2015-01-30 00:53:51 +01:00
|
|
|
|
2017-01-28 10:06:30 +01:00
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
|
import io.netty.handler.codec.mqtt.MqttMessage;
|
|
|
|
|
import io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader;
|
2015-01-30 00:53:51 +01:00
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Utility static methods, like Map get with default value, or elvis operator.
|
|
|
|
|
*/
|
2017-04-12 17:40:32 +02:00
|
|
|
public final class Utils {
|
2017-01-28 10:06:30 +01:00
|
|
|
|
2015-01-30 00:53:51 +01:00
|
|
|
public static <T, K> T defaultGet(Map<K, T> map, K key, T defaultValue) {
|
|
|
|
|
T value = map.get(key);
|
|
|
|
|
if (value != null) {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
return defaultValue;
|
|
|
|
|
}
|
2017-01-28 10:06:30 +01:00
|
|
|
|
|
|
|
|
public static int messageId(MqttMessage msg) {
|
|
|
|
|
return ((MqttMessageIdVariableHeader) msg.variableHeader()).messageId();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static byte[] readBytesAndRewind(ByteBuf payload) {
|
|
|
|
|
byte[] payloadContent = new byte[payload.readableBytes()];
|
|
|
|
|
int mark = payload.readerIndex();
|
|
|
|
|
payload.readBytes(payloadContent);
|
|
|
|
|
payload.readerIndex(mark);
|
|
|
|
|
return payloadContent;
|
|
|
|
|
}
|
2017-04-12 17:40:32 +02:00
|
|
|
|
|
|
|
|
private Utils() {
|
|
|
|
|
}
|
2015-01-30 00:53:51 +01:00
|
|
|
}
|