
com.hazelcast.client.impl.protocol.codec.builtin.MapCodec Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) 2008-2024, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.client.impl.protocol.codec.builtin;
import com.hazelcast.client.impl.protocol.ClientMessage;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Function;
import static com.hazelcast.client.impl.protocol.ClientMessage.BEGIN_FRAME;
import static com.hazelcast.client.impl.protocol.ClientMessage.END_FRAME;
import static com.hazelcast.client.impl.protocol.ClientMessage.NULL_FRAME;
import static com.hazelcast.client.impl.protocol.codec.builtin.CodecUtil.nextFrameIsDataStructureEndFrame;
import static com.hazelcast.client.impl.protocol.codec.builtin.CodecUtil.nextFrameIsNullEndFrame;
public final class MapCodec {
private MapCodec() {
}
public static void encode(ClientMessage clientMessage, Map map,
BiConsumer encodeKeyFunc,
BiConsumer encodeValueFunc) {
clientMessage.add(BEGIN_FRAME.copy());
for (Map.Entry entry : map.entrySet()) {
encodeKeyFunc.accept(clientMessage, entry.getKey());
encodeValueFunc.accept(clientMessage, entry.getValue());
}
clientMessage.add(END_FRAME.copy());
}
public static void encodeNullable(ClientMessage clientMessage, Map map,
BiConsumer encodeKeyFunc,
BiConsumer encodeValueFunc) {
if (map == null) {
clientMessage.add(NULL_FRAME.copy());
} else {
encode(clientMessage, map, encodeKeyFunc, encodeValueFunc);
}
}
public static Map decode(ClientMessage.ForwardFrameIterator iterator,
Function decodeKeyFunc,
Function decodeValueFunc) {
Map result = new HashMap<>();
//begin frame, map
iterator.next();
while (!nextFrameIsDataStructureEndFrame(iterator)) {
K key = decodeKeyFunc.apply(iterator);
V value = decodeValueFunc.apply(iterator);
result.put(key, value);
}
//end frame, map
iterator.next();
return result;
}
public static Map decodeNullable(ClientMessage.ForwardFrameIterator iterator,
Function decodeKeyFunc,
Function decodeValueFunc) {
return nextFrameIsNullEndFrame(iterator) ? null : decode(iterator, decodeKeyFunc, decodeValueFunc);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy