class MemoryRetainedRepository implements IRetainedRepository {

    private final ConcurrentMap<Topic, RetainedMessage> storage = new ConcurrentHashMap<>();
    private final ConcurrentMap<Topic, RetainedMessage> storageExpire = new ConcurrentHashMap<>();

    @Override
    public void cleanRetained(Topic topic) {
        storage.remove(topic);
        storageExpire.remove(topic);
    }

    @Override
    public void retain(Topic topic, MqttPublishMessage msg) {
        byte[] rawPayload = payloadToByteArray(msg);
        final RetainedMessage toStore = new RetainedMessage(topic, msg.fixedHeader().qosLevel(), rawPayload, extractPropertiesArray(msg));
        storage.put(topic, toStore);
    }

    @Override
    public void retain(Topic topic, MqttPublishMessage msg, Instant expiryTime) {
        byte[] rawPayload = payloadToByteArray(msg);
        final RetainedMessage toStore = new RetainedMessage(topic, msg.fixedHeader().qosLevel(), rawPayload, extractPropertiesArray(msg), expiryTime);
        storageExpire.put(topic, toStore);
    }

    private static MqttProperties.MqttProperty[] extractPropertiesArray(MqttPublishMessage msg) {
        MqttProperties properties = msg.variableHeader().properties();
        return properties.listAll().toArray(new MqttProperties.MqttProperty[0]);
    }

    private static byte[] payloadToByteArray(MqttPublishMessage msg) {
        final ByteBuf payload = msg.content();
        byte[] rawPayload = new byte[payload.readableBytes()];
        payload.getBytes(0, rawPayload);
        return rawPayload;
    }

    @Override
    public boolean isEmpty() {
        return storage.isEmpty() && storageExpire.isEmpty();
    }

    @Override
    public Collection<RetainedMessage> retainedOnTopic(String topic) {
        final Topic searchTopic = new Topic(topic);
        final List<RetainedMessage> matchingMessages = new ArrayList<>();
        matchingMessages.addAll(findMatching(searchTopic, storage));
        matchingMessages.addAll(findMatching(searchTopic, storageExpire));
        return matchingMessages;
    }

    @Override
    public Collection<RetainedMessage> listExpirable() {
        return storageExpire.values();
    }

    private List<RetainedMessage> findMatching(Topic searchTopic, ConcurrentMap<Topic, RetainedMessage> map) {<FILL_FUNCTION_BODY>}
}

        final List<RetainedMessage> matchingMessages = new ArrayList<>();
        for (Map.Entry<Topic, RetainedMessage> entry : map.entrySet()) {
            final Topic scanTopic = entry.getKey();
            if (scanTopic.match(searchTopic)) {
                matchingMessages.add(entry.getValue());
            }
        }
        return matchingMessages;
    
<no_super_class>