Tune write low/highwatermark
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / connection / OutboundQueueCache.java
1 /*
2  * Copyright (c) 2015 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.openflowjava.protocol.impl.core.connection;
9
10 import java.util.HashMap;
11 import java.util.Map;
12
13 final class OutboundQueueCache {
14     private static final OutboundQueueCache INSTANCE = new OutboundQueueCache();
15
16     private final Map<Integer, OutboundQueueCacheSlice> slices = new HashMap<>();
17
18     private OutboundQueueCache() {
19
20     }
21
22     static OutboundQueueCache getInstance() {
23         return INSTANCE;
24     }
25
26     synchronized OutboundQueueCacheSlice getSlice(final int queueSize) {
27         final OutboundQueueCacheSlice oldSlice = slices.get(queueSize);
28         if (oldSlice != null) {
29             oldSlice.incRef();
30             return oldSlice;
31         }
32
33         final OutboundQueueCacheSlice newSlice = new OutboundQueueCacheSlice(queueSize);
34         slices.put(queueSize, newSlice);
35         return newSlice;
36     }
37
38     synchronized void putSlice(final OutboundQueueCacheSlice slice) {
39         if (slice.decRef()) {
40             slices.remove(slice.getQueueSize());
41         }
42     }
43 }