org.apache.jcs.utils.struct
Class SortedPreferentialArray<T extends Comparable<? super T>>

java.lang.Object
  extended by org.apache.jcs.utils.struct.SortedPreferentialArray<T>

public class SortedPreferentialArray<T extends Comparable<? super T>>
extends Object

This maintains a sorted array with a preferential replacement policy when full.

Insertion time is n, search is log(n)

Clients must manage thread safety on previous version. I synchronized the public methods to add easy thread safety. I synchronized all public methods that make modifications.


Constructor Summary
SortedPreferentialArray(int maxSize)
          Construct the array with the maximum size.
 
Method Summary
 void add(T obj)
          If the array is full this will remove the smallest if preferLarge==true and if obj is bigger, or the largest if preferLarge=false and obj is smaller than the largest.
protected  String dumpArray()
          Debugging method to return a human readable display of array data.
 T getLargest()
          Returns the largest without removing it from the array.
 T getSmallest()
          Returns the smallest element without removing it from the array.
 void setPreferLarge(boolean pref)
          Determines whether the preference is for large or small.
 int size()
          Returns the current size of the array.
 T takeNearestLargerOrEqual(T obj)
          Returns and removes the nearer larger or equal object from the aray.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SortedPreferentialArray

public SortedPreferentialArray(int maxSize)
Construct the array with the maximum size.

Parameters:
maxSize - int
Method Detail

add

public void add(T obj)
If the array is full this will remove the smallest if preferLarge==true and if obj is bigger, or the largest if preferLarge=false and obj is smaller than the largest.

Parameters:
obj - Object

getLargest

public T getLargest()
Returns the largest without removing it from the array.

Returns:
Comparable

getSmallest

public T getSmallest()
Returns the smallest element without removing it from the array.

Returns:
Comparable

setPreferLarge

public void setPreferLarge(boolean pref)
Determines whether the preference is for large or small.

Parameters:
pref - boolean

takeNearestLargerOrEqual

public T takeNearestLargerOrEqual(T obj)
Returns and removes the nearer larger or equal object from the aray.

Parameters:
obj - Comparable
Returns:
Comparable, null if arg is null or none was found.

size

public int size()
Returns the current size of the array.

Returns:
int

dumpArray

protected String dumpArray()
Debugging method to return a human readable display of array data.

Returns:
String representation of the contents.


Copyright © 2002-2013 The Apache Software Foundation. All Rights Reserved.