FileDocCategorySizeDatePackage
SegmentCache.javaAPI DocJava SE 5 API2782Fri Aug 26 14:58:16 BST 2005javax.swing.text

SegmentCache

public class SegmentCache extends Object
SegmentCache caches Segments to avoid continually creating and destroying of Segments. A common use of this class would be:
Segment segment = segmentCache.getSegment();
// do something with segment
...
segmentCache.releaseSegment(segment);
version
1.5 12/19/03

Fields Summary
private static SegmentCache
sharedCache
A global cache.
private List
segments
A list of the currently unused Segments.
Constructors Summary
public SegmentCache()
Creates and returns a SegmentCache.

        segments = new ArrayList(11);
    
Methods Summary
public javax.swing.text.SegmentgetSegment()
Returns a Segment. When done, the Segment should be recycled by invoking releaseSegment.

        synchronized(this) {
            int size = segments.size();

            if (size > 0) {
                return (Segment)segments.remove(size - 1);
            }
        }
        return new CachedSegment();
    
public static javax.swing.text.SegmentCachegetSharedInstance()
Returns the shared SegmentCache.



             
        
        return sharedCache;
    
public static javax.swing.text.SegmentgetSharedSegment()
A convenience method to get a Segment from the shared SegmentCache.

        return getSharedInstance().getSegment();
    
public voidreleaseSegment(javax.swing.text.Segment segment)
Releases a Segment. You should not use a Segment after you release it, and you should NEVER release the same Segment more than once, eg:
segmentCache.releaseSegment(segment);
segmentCache.releaseSegment(segment);
Will likely result in very bad things happening!

        if (segment instanceof CachedSegment) {
            synchronized(this) {
                segment.array = null;
                segment.count = 0;
                segments.add(segment);
            }
        }
    
public static voidreleaseSharedSegment(javax.swing.text.Segment segment)
A convenience method to release a Segment to the shared SegmentCache.

        getSharedInstance().releaseSegment(segment);