blob: 518cfcc3fb85865244fcbee7dc130882b55226a1 [file] [log] [blame]
Anton Chigrin4af4f872019-01-14 17:29:56 +02001/*
2 * Copyright 2019-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.openflow.controller.impl;
17
18import java.util.Collection;
19import java.util.concurrent.BlockingQueue;
20import java.util.concurrent.LinkedBlockingQueue;
21
22/**
23 * This class wrap existing class LinkedBlockingQueue for solution problem
24 * with handling and processed received messages in onos.
25 *
26 * @see java.util.concurrent.LinkedBlockingQueue
27 */
28public class LinkedBlockingMessagesQueue<T> {
29
30 /**
31 * Identifier of queue.
32 */
33 private int idQueue;
34
35 /**
36 * Size of queue.
37 */
38 private int sizeOfQueue;
39
40 /**
41 * Maximal bulk of messages that will be processed.
42 */
43 private int bulk;
44
45 /**
46 * Queue of messages.
47 */
48 private BlockingQueue<T> queue;
49
50 /**
51 * Constructor.
52 *
53 * @param idQueue Identifier of queue
54 * @param sizeOfQueue Size of queue
55 * @param bulk Maximal bulk of messages that will be processed
56 */
57 public LinkedBlockingMessagesQueue(int idQueue, int sizeOfQueue, int bulk) {
58 this.idQueue = idQueue;
59 this.sizeOfQueue = sizeOfQueue;
60 this.queue = new LinkedBlockingQueue<>(this.sizeOfQueue);
61 this.bulk = bulk;
62 }
63
64 /**
65 * Returns the identifier of this queue.
66 *
67 * @return the id of this queue
68 */
69 public int idQueue() {
70 return idQueue;
71 }
72
73 /**
74 * Return the size of this queue.
75 *
76 * @return the size of this queue
77 */
78 public int sizeOfQueue() {
79 return sizeOfQueue;
80 }
81
82 /**
83 * Set size for this queue.
84 *
85 * @param sizeOfQueue Size of queue
86 */
87 public void setSizeOfQueue(int sizeOfQueue) {
88 this.sizeOfQueue = sizeOfQueue;
89 this.queue = new LinkedBlockingQueue<>(this.sizeOfQueue);
90 }
91
92 /**
93 * Offer new message to this queue.
94 *
95 * @param message elemet to add
96 * @return <code>true</code> if the element was added to this queue, else <code>false</code>
97 */
98 public boolean offer(T message) {
99 return this.queue.offer(message);
100 }
101
102 /**
103 * Transfer bulk of elements from this queue to the <code>messages</code> collection.
104 *
105 * @param messages the collection to transfer bulk of elements from this queue
106 * @return the numbers of elements transfered
107 */
108 public int drainTo(Collection<? super T> messages) {
109 return this.queue.drainTo(messages, this.bulk);
110 }
111
112 /**
113 * Return the elements count in this queue.
114 *
115 * @return the elements count
116 */
117 public int size() {
118 return this.queue.size();
119 }
120
121 /**
122 * Return the maximal bulk of messages for this queue.
123 *
124 * @return maximal bulk of messages that will be processed
125 */
126 public int bulk() {
127 return bulk;
128 }
129
130 /**
131 * Set the maximal bulk of messages for this queue.
132 *
133 * @param bulk Maximal bulk of messages that will be processed
134 */
135 public void setBulk(int bulk) {
136 this.bulk = bulk;
137 }
138
139}