KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > text > SegmentCache


1 /*
2  * @(#)SegmentCache.java 1.5 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing.text;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.List JavaDoc;
11
12 /**
13  * SegmentCache caches <code>Segment</code>s to avoid continually creating
14  * and destroying of <code>Segment</code>s. A common use of this class would
15  * be:
16  * <pre>
17  * Segment segment = segmentCache.getSegment();
18  * // do something with segment
19  * ...
20  * segmentCache.releaseSegment(segment);
21  * </pre>
22  *
23  * @version 1.5 12/19/03
24  */

25 class SegmentCache {
26     /**
27      * A global cache.
28      */

29     private static SegmentCache JavaDoc sharedCache = new SegmentCache JavaDoc();
30
31     /**
32      * A list of the currently unused Segments.
33      */

34     private List JavaDoc segments;
35
36
37     /**
38      * Returns the shared SegmentCache.
39      */

40     public static SegmentCache JavaDoc getSharedInstance() {
41         return sharedCache;
42     }
43
44     /**
45      * A convenience method to get a Segment from the shared
46      * <code>SegmentCache</code>.
47      */

48     public static Segment JavaDoc getSharedSegment() {
49         return getSharedInstance().getSegment();
50     }
51
52     /**
53      * A convenience method to release a Segment to the shared
54      * <code>SegmentCache</code>.
55      */

56     public static void releaseSharedSegment(Segment JavaDoc segment) {
57         getSharedInstance().releaseSegment(segment);
58     }
59
60
61
62     /**
63      * Creates and returns a SegmentCache.
64      */

65     public SegmentCache() {
66         segments = new ArrayList JavaDoc(11);
67     }
68
69     /**
70      * Returns a <code>Segment</code>. When done, the <code>Segment</code>
71      * should be recycled by invoking <code>releaseSegment</code>.
72      */

73     public Segment JavaDoc getSegment() {
74         synchronized(this) {
75             int size = segments.size();
76
77             if (size > 0) {
78                 return (Segment JavaDoc)segments.remove(size - 1);
79             }
80         }
81         return new CachedSegment();
82     }
83
84     /**
85      * Releases a Segment. You should not use a Segment after you release it,
86      * and you should NEVER release the same Segment more than once, eg:
87      * <pre>
88      * segmentCache.releaseSegment(segment);
89      * segmentCache.releaseSegment(segment);
90      * </pre>
91      * Will likely result in very bad things happening!
92      */

93     public void releaseSegment(Segment JavaDoc segment) {
94         if (segment instanceof CachedSegment) {
95             synchronized(this) {
96                 segment.array = null;
97                 segment.count = 0;
98                 segments.add(segment);
99             }
100         }
101     }
102
103
104     /**
105      * CachedSegment is used as a tagging interface to determine if
106      * a Segment can successfully be shared.
107      */

108     private static class CachedSegment extends Segment JavaDoc {
109     }
110 }
111
Popular Tags