| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| AsyncQueue |
|
| 0.0;0 | ||||
| AsyncQueue$AsyncQueueEntry |
|
| 0.0;0 |
| 1 | /* | |
| 2 | * | |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
| 4 | * | |
| 5 | * Copyright 2007-2008 Sun Microsystems, Inc. All rights reserved. | |
| 6 | * | |
| 7 | * The contents of this file are subject to the terms of either the GNU | |
| 8 | * General Public License Version 2 only ("GPL") or the Common Development | |
| 9 | * and Distribution License("CDDL") (collectively, the "License"). You | |
| 10 | * may not use this file except in compliance with the License. You can obtain | |
| 11 | * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html | |
| 12 | * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific | |
| 13 | * language governing permissions and limitations under the License. | |
| 14 | * | |
| 15 | * When distributing the software, include this License Header Notice in each | |
| 16 | * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt. | |
| 17 | * Sun designates this particular file as subject to the "Classpath" exception | |
| 18 | * as provided by Sun in the GPL Version 2 section of the License file that | |
| 19 | * accompanied this code. If applicable, add the following below the License | |
| 20 | * Header, with the fields enclosed by brackets [] replaced by your own | |
| 21 | * identifying information: "Portions Copyrighted [year] | |
| 22 | * [name of copyright owner]" | |
| 23 | * | |
| 24 | * Contributor(s): | |
| 25 | * | |
| 26 | * If you wish your version of this file to be governed by only the CDDL or | |
| 27 | * only the GPL Version 2, indicate your decision by adding "[Contributor] | |
| 28 | * elects to include this software in this distribution under the [CDDL or GPL | |
| 29 | * Version 2] license." If you don't indicate a single choice of license, a | |
| 30 | * recipient has the option to distribute your version of this file under | |
| 31 | * either the CDDL, the GPL Version 2 or to extend the choice of license to | |
| 32 | * its licensees as provided above. However, if you add GPL Version 2 code | |
| 33 | * and therefore, elected the GPL Version 2 license, then the option applies | |
| 34 | * only if the new code is made subject to such option by the copyright | |
| 35 | * holder. | |
| 36 | * | |
| 37 | */ | |
| 38 | ||
| 39 | package com.sun.grizzly.async; | |
| 40 | ||
| 41 | import java.util.Map; | |
| 42 | import java.util.concurrent.ConcurrentHashMap; | |
| 43 | import java.util.concurrent.ConcurrentLinkedQueue; | |
| 44 | import java.util.concurrent.atomic.AtomicReference; | |
| 45 | import java.util.concurrent.locks.ReentrantLock; | |
| 46 | ||
| 47 | /** | |
| 48 | * Class implements {@link Map}-like collection, maps keys to values, where | |
| 49 | * single key could have queue of correspondent values. | |
| 50 | * | |
| 51 | * @author Alexey Stashok | |
| 52 | */ | |
| 53 | 272 | public class AsyncQueue<K, E> { |
| 54 | 272 | private Map<K, AsyncQueueEntry> queueMap = |
| 55 | new ConcurrentHashMap<K, AsyncQueueEntry>(); | |
| 56 | ||
| 57 | /** | |
| 58 | * Add data to the {@link AsyncQueue}, corresponding to the given | |
| 59 | * <code>E</code> key | |
| 60 | * | |
| 61 | * @param key <code>E</code> | |
| 62 | * @param queueRecord data unit | |
| 63 | */ | |
| 64 | public void offer(K key, E queueRecord) { | |
| 65 | 0 | AsyncQueueEntry entry = obtainAsyncQueueEntry(key); |
| 66 | 0 | entry.queue.offer(queueRecord); |
| 67 | 0 | } |
| 68 | ||
| 69 | /** | |
| 70 | * Get head element of <code>E</code> key related queue. | |
| 71 | * Element will not be removed from queue. | |
| 72 | * | |
| 73 | * @param key <code>K</code> | |
| 74 | * | |
| 75 | * @return <code>E</code> data unit | |
| 76 | */ | |
| 77 | public E peek(K key) { | |
| 78 | 0 | AsyncQueueEntry entry = queueMap.get(key); |
| 79 | 0 | if (entry != null) { |
| 80 | 0 | return entry.queue.peek(); |
| 81 | } | |
| 82 | ||
| 83 | 0 | return null; |
| 84 | } | |
| 85 | ||
| 86 | /** | |
| 87 | * Get head element of <code>K</code> key related queue. | |
| 88 | * Element will be removed from queue. | |
| 89 | * | |
| 90 | * @param key <code>K</code> | |
| 91 | * | |
| 92 | * @return <code>E</code> data unit | |
| 93 | */ | |
| 94 | public E poll(K key) { | |
| 95 | 0 | AsyncQueueEntry entry = queueMap.get(key); |
| 96 | 0 | if (entry != null) { |
| 97 | 0 | return entry.queue.poll(); |
| 98 | } | |
| 99 | ||
| 100 | 0 | return null; |
| 101 | } | |
| 102 | ||
| 103 | /** | |
| 104 | * Remove head element of <code>K</code> key related queue. | |
| 105 | * | |
| 106 | * @param key <code>K</code> | |
| 107 | */ | |
| 108 | public void removeEntry(K key) { | |
| 109 | 878 | queueMap.remove(key); |
| 110 | 878 | } |
| 111 | ||
| 112 | /** | |
| 113 | * Get the size of <code>K</code> key related queue. | |
| 114 | * | |
| 115 | * @param key <code>K</code> | |
| 116 | * @return size of <code>K</code> key related queue. | |
| 117 | */ | |
| 118 | public int size(K key) { | |
| 119 | 0 | AsyncQueueEntry entry = queueMap.get(key); |
| 120 | 0 | return entry == null ? 0 : entry.queue.size(); |
| 121 | } | |
| 122 | ||
| 123 | /** | |
| 124 | * Checks if <code>K</code> key related queue is empty. | |
| 125 | * | |
| 126 | * @param key <code>K</code> | |
| 127 | * @return true, if <code>K</code> key related queue is empty, false otherwise | |
| 128 | */ | |
| 129 | public boolean isEmpty(K key) { | |
| 130 | 0 | AsyncQueueEntry entry = queueMap.get(key); |
| 131 | 0 | return entry == null || entry.queue.isEmpty(); |
| 132 | } | |
| 133 | ||
| 134 | public void clear() { | |
| 135 | 272 | queueMap.clear(); |
| 136 | 272 | } |
| 137 | ||
| 138 | protected AsyncQueueEntry obtainAsyncQueueEntry(K key) { | |
| 139 | 918902 | AsyncQueueEntry entry = queueMap.get(key); |
| 140 | 918901 | if (entry == null) { |
| 141 | 345 | synchronized(key) { |
| 142 | 345 | entry = queueMap.get(key); |
| 143 | 345 | if (entry == null) { |
| 144 | 312 | entry = new AsyncQueueEntry(); |
| 145 | 312 | queueMap.put(key, entry); |
| 146 | } | |
| 147 | 345 | } |
| 148 | } | |
| 149 | 918696 | return entry; |
| 150 | } | |
| 151 | ||
| 152 | protected AsyncQueueEntry getAsyncQueueEntry(K key) { | |
| 153 | 161176 | return queueMap.get(key); |
| 154 | } | |
| 155 | /** | |
| 156 | * {@link AsyncQueue} data unit | |
| 157 | */ | |
| 158 | 272 | protected class AsyncQueueEntry { |
| 159 | public ConcurrentLinkedQueue<E> queue; | |
| 160 | public AtomicReference<E> currentElement; | |
| 161 | public ReentrantLock queuedActionLock; | |
| 162 | ||
| 163 | 312 | public AsyncQueueEntry() { |
| 164 | 312 | queue = new ConcurrentLinkedQueue<E>(); |
| 165 | 312 | currentElement = new AtomicReference<E>(); |
| 166 | 312 | queuedActionLock = new ReentrantLock(); |
| 167 | 312 | } |
| 168 | } | |
| 169 | } |