Content of file MultiQueue.java.html
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>MultiQueue.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">TheseFoolishThings :: MessageBus :: Spring</a> > <a href="../index.html" class="el_bundle">it-tidalwave-messagebus</a> > <a href="index.source.html" class="el_package">it.tidalwave.messagebus.spi</a> > <span class="el_source">MultiQueue.java</span></div><h1>MultiQueue.java</h1><pre class="source lang-java linenums">/*
* *********************************************************************************************************************
*
* TheseFoolishThings: Miscellaneous utilities
* http://tidalwave.it/projects/thesefoolishthings
*
* Copyright (C) 2009 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
*
* *********************************************************************************************************************
*
* 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.
*
* *********************************************************************************************************************
*
* git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
* git clone https://github.com/tidalwave-it/thesefoolishthings-src
*
* *********************************************************************************************************************
*/
package it.tidalwave.messagebus.spi;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.LinkedBlockingQueue;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
*
**********************************************************************************************************************/
<span class="nc" id="L47">@Slf4j</span>
<span class="nc" id="L48">public class MultiQueue </span>
{
<span class="nc bnc" id="L50" title="All 4 branches missed."> @RequiredArgsConstructor @Getter @ToString</span>
static class TopicAndMessage<T>
{
@Nonnull
<span class="nc" id="L54"> private final Class<T> topic;</span>
@Nonnull
<span class="nc" id="L57"> private final T message;</span>
}
<span class="nc" id="L60"> private final ConcurrentNavigableMap<Class<?>, Queue<?>> queueMapByTopic =</span>
<span class="nc" id="L61"> new ConcurrentSkipListMap<>(Comparator.comparing(Class::getName));</span>
<span class="nc" id="L63"> private Class<?> latestSentTopic = null;</span>
/*******************************************************************************************************************
*
* Adds a message of the given topic to this queue and issues a notification.
*
* @param <T> the static type of the message
* @param topic the dynamic type of the message
* @param message the message
*
******************************************************************************************************************/
public synchronized <T> void add (@Nonnull final Class<T> topic, @Nonnull final T message)
{
<span class="nc" id="L76"> getQueue(topic).add(message);</span>
<span class="nc" id="L77"> notifyAll();</span>
<span class="nc" id="L78"> }</span>
/*******************************************************************************************************************
*
* Removes and returns the next pair (topic, message) from the queue. Blocks until one is available.
*
* @param <T> the static type of the topic
* @return the topic and message
* @throws InterruptedException if interrupted while waiting
*
******************************************************************************************************************/
@Nonnull
public synchronized <T> TopicAndMessage<T> remove()
throws InterruptedException
{
for (;;)
{
<span class="nc bnc" id="L95" title="All 2 branches missed."> for (final var topic : reorderedTopics())</span>
{
<span class="nc" id="L97"> final var queue = queueMapByTopic.get(topic);</span>
<span class="nc" id="L98"> final var message = queue.poll();</span>
<span class="nc bnc" id="L100" title="All 2 branches missed."> if (message != null)</span>
{
<span class="nc" id="L102"> latestSentTopic = topic;</span>
<span class="nc bnc" id="L104" title="All 2 branches missed."> if (log.isTraceEnabled())</span>
{
<span class="nc" id="L106"> log.trace("stats {}", stats());</span>
}
<span class="nc" id="L109"> return new TopicAndMessage<>((Class<T>)topic, (T)message);</span>
}
<span class="nc" id="L111"> }</span>
<span class="nc bnc" id="L113" title="All 2 branches missed."> if (log.isTraceEnabled())</span>
{
<span class="nc" id="L115"> log.trace("all queues empty; stats {}", stats());</span>
}
<span class="nc" id="L118"> wait(); </span>
}
}
/*******************************************************************************************************************
*
* Returns the list of topics reordered, so it starts just after latestSentTopic and wraps around.
*
******************************************************************************************************************/
@Nonnull
private List<Class<?>> reorderedTopics()
{
<span class="nc" id="L130"> final var keySet = queueMapByTopic.navigableKeySet();</span>
<span class="nc" id="L131"> final List<Class<?>> scanSet = new ArrayList<>();</span>
<span class="nc bnc" id="L133" title="All 2 branches missed."> if (latestSentTopic == null)</span>
{
<span class="nc" id="L135"> scanSet.addAll(keySet);</span>
}
else
{
<span class="nc" id="L139"> scanSet.addAll(keySet.subSet(latestSentTopic, false, keySet.last(), true));</span>
<span class="nc" id="L140"> scanSet.addAll(keySet.subSet(keySet.first(), true, latestSentTopic, true));</span>
}
<span class="nc" id="L143"> return scanSet;</span>
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
private synchronized String stats()
{
<span class="nc" id="L152"> final var b = new StringBuilder();</span>
<span class="nc" id="L153"> var separator = "";</span>
<span class="nc bnc" id="L155" title="All 2 branches missed."> for (final var e : queueMapByTopic.entrySet())</span>
{
<span class="nc" id="L157"> b.append(separator).append(String.format("%s[%s]: %d", </span>
<span class="nc bnc" id="L158" title="All 2 branches missed."> e.getKey().getSimpleName(), e.getKey().equals(latestSentTopic) ? "X" : " ", e.getValue().size()));</span>
<span class="nc" id="L159"> separator = ", ";</span>
<span class="nc" id="L160"> }</span>
<span class="nc" id="L162"> return b.toString();</span>
}
/*******************************************************************************************************************
*
* Returns the queue associated to a given topic. The queue is created if the topic is new.
*
* @param topic the topic
* @return the queue
*
******************************************************************************************************************/
@Nonnull
private synchronized <T> Queue<T> getQueue (@Nonnull final Class<T> topic)
{
// TODO Java 8 would make this easier
| Java 8 would make this easier | |
<span class="nc" id="L177"> var queue = (Queue<T>)queueMapByTopic.get(topic);</span>
<span class="nc bnc" id="L179" title="All 2 branches missed."> if (queue == null)</span>
{
<span class="nc" id="L181"> queue = new LinkedBlockingQueue<>();</span>
<span class="nc" id="L182"> queueMapByTopic.put(topic, queue);</span>
}
<span class="nc" id="L185"> return queue;</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.9.202303310957</span></div></body></html>