- fix the bug related to the profiler4j path in build.xml. Thanks to Damian for reporting it.

- GenericBuffer now respect the SC code convention
cusax-fix
Benoit Pradelle 17 years ago
parent 3739a09e82
commit 5aa4692abf

@ -598,7 +598,7 @@
depends="bundle-plugin-profiler4j">
<antcall target="run">
<param name="profiler.args" value="-Xmx128m -javaagent:${lib.noinst}\profiler4j-1.0-beta3-SC.jar" />
<param name="profiler.args" value="-Xmx128m -javaagent:${lib.noinst}/profiler4j-1.0-beta3-SC.jar" />
<param name="profiler.bootdelegation" value="net.sf.profiler4j.agent.*" />
<param name="profiler.autostart" value="reference:file:sc-bundles/profiler4j.jar" />
</antcall>

@ -1,8 +1,7 @@
/*
* SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*
* Distributable under LGPL license. See terms of license at gnu.org.
*/
package net.java.sip.communicator.util;
@ -16,91 +15,106 @@
*
* @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;
}
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;
}
}

Loading…
Cancel
Save