Alachisoft.NCache.Common.DataStructures.QueueDictionary Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nc-common Show documentation
Show all versions of nc-common Show documentation
Internal package of Alachisoft.
package Alachisoft.NCache.Common.DataStructures;
// Copyright (c) 2020 Alachisoft
//
// 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
import java.util.Iterator;
/**
* The StackDictionary provide impelemntation for IStore, behave as Stack and Dictionary and the same time
*/
public class QueueDictionary implements Iterable {
private java.util.Map> store = null;
private QEntry head = null;
private QEntry tail = null;
public QueueDictionary() {
store = new java.util.HashMap<>();
}
public final int getCount() {
return store.size();
}
public boolean Enqueue(V value) {
if (store.containsKey(value)) {
return false;
}
QEntry entry = new QEntry(value);
store.put(value, entry);
if (head == null && tail == null) {
head = entry;
} else if (tail != null || head != null) {
entry.Prev = tail;
tail.Next = entry;
}
tail = entry;
return true;
}
public boolean Remove(V value) {
QEntry entry = null;
entry = store.get(value);
if (entry != null) {
if (store.remove(value) == null) {
return false;
}
if (store.isEmpty()) {
head = tail = null;
return true;
}
if (entry.Prev == null) {
// if peek is being removed then set peek to next and remove this one
head = head.Next;
if (head != null) {
head.Prev = null;
}
} else {
entry.Prev.Next = entry.Next;
//if not last entry of store then connect next of entry with previous
if (entry.Next != null) {
entry.Next.Prev = entry.Prev;
} else {
tail = entry.Prev;
}
}
return true;
}
return false;
}
public boolean Contains(V value) {
return store.containsKey(value);
}
public V Peek() {
if (head == null || head.Value == null) {
return null;
}
return head.Value;
}
public V Dequeue() {
if (head == null) {
return null;
}
V takeAway = head.Value;
if (!Remove(takeAway)) {
return null;
}
return takeAway;
}
public void dispose() {
if (store != null) {
store.clear();
store = null;
}
tail = head = null;
}
public final java.util.Collection getKeys() {
return store.keySet();
}
@Override
public Iterator iterator() {
return store.entrySet().iterator();
}
}