aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBenoit Pradelle <pradelle@java.net>2008-10-09 01:46:23 +0000
committerBenoit Pradelle <pradelle@java.net>2008-10-09 01:46:23 +0000
commit429a89f34ac1da15ccae719a848a1baa12b9ae33 (patch)
tree51bb8e1a62254ff191c1c8e78732541f63a8f7a9
parentfce24e42566cffc4227d83d3ff8e914d6ada85c9 (diff)
downloadjitsi-429a89f34ac1da15ccae719a848a1baa12b9ae33.zip
jitsi-429a89f34ac1da15ccae719a848a1baa12b9ae33.tar.gz
jitsi-429a89f34ac1da15ccae719a848a1baa12b9ae33.tar.bz2
Add the GenericBuffer util.
-rw-r--r--src/net/java/sip/communicator/util/GenericBuffer.java106
1 files changed, 106 insertions, 0 deletions
diff --git a/src/net/java/sip/communicator/util/GenericBuffer.java b/src/net/java/sip/communicator/util/GenericBuffer.java
new file mode 100644
index 0000000..fcb083f
--- /dev/null
+++ b/src/net/java/sip/communicator/util/GenericBuffer.java
@@ -0,0 +1,106 @@
+/*
+ * SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
+ *
+ * Distributable under LGPL license.
+ * See terms of license at gnu.org.
+ */
+
+package net.java.sip.communicator.util;
+
+import java.util.*;
+
+/**
+ * The GenericBuffer class provides a way to minimize the effort needed to
+ * bufferize any kind of information. This class is particularly suited to
+ * optimizations based on reusing already computed data.
+ *
+ * @author Benoit Pradelle
+ */
+public class GenericBuffer<T> {
+ private final Hashtable<String, GenericBufferPair> buffer;
+ private int minAge = 0;
+ private int curAge = 0;
+ private final int maxCapacity;
+
+ /**
+ * Sole constructor.
+ *
+ * @param bufferSize The buffer size. Adding data to a full buffer will
+ * cause the oldest data present in the buffer to be overwritten;
+ */
+ public GenericBuffer(final int bufferSize) {
+ assert bufferSize > 0;
+
+ buffer = new Hashtable<String, GenericBufferPair>(bufferSize);
+ maxCapacity = bufferSize;
+ }
+
+ /**
+ * Adds a value to the buffer. If the buffer is full, the oldest value in
+ * the buffer will be overwritten by this new value.
+ *
+ * @param value The value to add. Can't be null.
+ * @param context The context for which this value is valid. This basically
+ * represents the current value of all the variables which control the value
+ * is correct. The context is used to find this value in the buffer. If
+ * the context is already associated in the buffer with a value, nothing is
+ * added nor modified.
+ */
+ public void addValue(final T value, final String context) {
+ assert value != null && context != null;
+
+ GenericBufferPair storage = buffer.get(context);
+
+ if (storage == null) {
+ storage = new GenericBufferPair();
+ } else {
+ return; // don't override values
+ }
+
+ // if the amount of data has reach the limit, search the oldest data
+ if (buffer.size() == maxCapacity) {
+ for (Map.Entry<String, GenericBufferPair> e : buffer.entrySet()) {
+ if (e.getValue().age == minAge) {
+ buffer.remove(e.getKey());
+ minAge++;
+ break;
+ }
+ }
+ }
+
+ storage.age = curAge++;
+ storage.value = value;
+
+ buffer.put(context, storage);
+ }
+
+ /**
+ * Retrieves the value in the buffer corresponding to the context if it
+ * exists.
+ *
+ * @param context The context of the searched value. The context represents
+ * all the variables values for which this value is correct.
+ *
+ * @return The bufferized value with the searched context if it exists or
+ * null if no value is found.
+ */
+ public T getValue(final String context) {
+ assert context != null;
+
+ GenericBufferPair res = buffer.get(context);
+
+ if (res == null) {
+ return null;
+ }
+
+ return res.value;
+ }
+
+ /**
+ * This class is a simple structure to store a pair context-value
+ */
+ private class GenericBufferPair {
+ public T value = null;
+ public int age = 0;
+ }
+}