com.eliad.util
Class SizeCache

java.lang.Object
  |
  +--com.eliad.util.SizeCache

public class SizeCache
extends java.lang.Object

A mechanism to cache size/position information as defined in Intervals. This will cache information in the zone specified by setCachedRange(int, int). The sizes and positions at the first and last positions are also cached. This gives in the cached range O(1) behavior for getSize and getPosition, and O(Log(k)) for getIndex where k is the size of the cached range. If the Interval object hasFixedSize, no caching is done.

Invariants:
getIntervals() != null
getCount() == getIntervals().getCount()
forall int i in [0, getCount()[ | getSize(i) == getIntervals().getSize(i)
forall int i in [0, getCount()+1[ | getPosition(i) == getIntervals().getPosition(i)
forall int x | getIndex(x) == getIntervals().getIndex(x)
Version:
1.0 08/04/00
Author:
Patrick Mérissert-Coffinières

Constructor Summary
SizeCache(Intervals intervals)
          Constructs a cache above an Intervals object>.
 
Method Summary
 void entriesInserted(int start, int count)
          A post call-back for entries insertion
 void entriesRemoved(int start, int count)
          A post call-back for entries removal
 void entryUpdated(int index, int old, int size)
          A post call-back for entry update
 int getCount()
          Returns the number of entries in the cached Intervals object.
 int getIndex(int position)
          Returns the index of the entry that corresponds to the specified position.
 Intervals getIntervals()
           
 int getPosition(int index)
          Returns the start position for the specified entry.
 int getSize(int index)
          Retrieves the size of the specified entry, from the cache if possible.
 void insertEntries(int start, int length, int value)
          Transmits entries insertion to the underlying Intervals object.
 void invalidateCache(int from, int count)
          Takes into account global modifications, specifically, size modifications beginning with from and extending according the the size of the count.
 void removeEntries(int start, int length)
          Transmits entries removal to the underlying Intervals object.
 void setCachedRange(int start, int length)
          Specifies the region to be cached
 void setSize(int index, int size)
          Transmits an update to the size of the specified entry to the underlying Intervals object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SizeCache

public SizeCache(Intervals intervals)
Constructs a cache above an Intervals object>. There can be any number of caches over a given object.
Method Detail

getIntervals

public Intervals getIntervals()

getCount

public final int getCount()
Returns the number of entries in the cached Intervals object.
Returns:
the number of entries

getSize

public int getSize(int index)
Retrieves the size of the specified entry, from the cache if possible.
Parameters:
index - the index corresponding to the entry
Returns:
the size of the entry

getPosition

public int getPosition(int index)
Returns the start position for the specified entry.
Parameters:
index - the index of the entry with the desired position
Returns:
the starting position of the specified entry

getIndex

public int getIndex(int position)
Returns the index of the entry that corresponds to the specified position.
Parameters:
position - the position of the entry
Returns:
the index of the entry that occupies the specified position

setSize

public void setSize(int index,
                    int size)
Transmits an update to the size of the specified entry to the underlying Intervals object. Whenever the setSize method of the underlying object is invoked, directly or indirectly, the entryUpdated method must be called for any cache on this Intervals otherwise the cache will be broken.
Parameters:
index - the index corresponding to the entry
size - the size of the entry

insertEntries

public void insertEntries(int start,
                          int length,
                          int value)
Transmits entries insertion to the underlying Intervals object. Whenever the insertEntriesmethod of the underlying object is invoked, directly or indirectly, the entriesInserted method must be called for any cache on this Intervals otherwise the cache will be broken.
Parameters:
start - the start index of the insertions
length - the number of inserted entries
value - the value of the new entries.
Preconditions:
start >= 0
length > 0
Postconditions:
getCount() == getCount()@pre - length

removeEntries

public void removeEntries(int start,
                          int length)
Transmits entries removal to the underlying Intervals object. Whenever the removeEntriesmethod of the underlying object is invoked, directly or indirectly, the entriesRemoved method must be called for any cache on this Intervals otherwise the cache will be broken.
Parameters:
start - the start index of the removal
length - the number of removed entries
Preconditions:
start >= 0
length > 0 && start+length < getCount()
Postconditions:
getCount() == getCount()@pre - length

setCachedRange

public void setCachedRange(int start,
                           int length)
Specifies the region to be cached
Parameters:
start - the first position in the cache
length - the number of items we need to cache.
Preconditions:
start >= 0
length >= 0

invalidateCache

public void invalidateCache(int from,
                            int count)
Takes into account global modifications, specifically, size modifications beginning with from and extending according the the size of the count.
Parameters:
index - the first index where something may have changed.
length - the number of entries that may have changed, -1 means to the end

entryUpdated

public void entryUpdated(int index,
                         int old,
                         int size)
A post call-back for entry update
Parameters:
index - the index of the entry whose size was just updated.
size - the new size
old - the old size
Preconditions:
index>=0 implies index < getCount()
index>=0 implies getIntervals().getSize(index) == size

entriesInserted

public void entriesInserted(int start,
                            int count)
A post call-back for entries insertion
Parameters:
start - the index where the first entry was just inserted.
length - the number of entries that were just inserted.
value - the size that was just given to the new entries

entriesRemoved

public void entriesRemoved(int start,
                           int count)
A post call-back for entries removal
Parameters:
start - the index where the first entry was just removed.
count - the number of entries that were just removed.