11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package javax.swing.text; 26 27 import java.util.ArrayList; 28 import java.util.List; 29 30 /** 31 * SegmentCache caches <code>Segment</code>s to avoid continually creating 32 * and destroying of <code>Segment</code>s. A common use of this class would 33 * be: 34 * <pre> 35 * Segment segment = segmentCache.getSegment(); 36 * // do something with segment 37 * ... 38 * segmentCache.releaseSegment(segment); 39 * </pre> 40 * 41 */ 42 class SegmentCache { 43 /** 44 * A global cache. 45 */ 46 private static SegmentCache sharedCache = new SegmentCache(); 47 48 /** 49 * A list of the currently unused Segments. 50 */ 51 private List<Segment> segments; 52 53 54 /** 55 * Returns the shared SegmentCache. 56 */ 57 public static SegmentCache getSharedInstance() { 58 return sharedCache; 59 } 60 61 /** 62 * A convenience method to get a Segment from the shared 63 * <code>SegmentCache</code>. 64 */ 65 public static Segment getSharedSegment() { 66 return getSharedInstance().getSegment(); 67 } 68 69 /** 70 * A convenience method to release a Segment to the shared 71 * <code>SegmentCache</code>. 72 */ 73 public static void releaseSharedSegment(Segment segment) { 74 getSharedInstance().releaseSegment(segment); 75 } 76 77 78 79 /** 80 * Creates and returns a SegmentCache. 81 */ 82 public SegmentCache() { 83 segments = new ArrayList<Segment>(11); 84 } 85 86 /** 87 * Returns a <code>Segment</code>. When done, the <code>Segment</code> 88 * should be recycled by invoking <code>releaseSegment</code>. 89 */ 90 public Segment getSegment() { 91 synchronized(this) { 92 int size = segments.size(); 93 94 if (size > 0) { 95 return segments.remove(size - 1); 96 } 97 } 98 return new CachedSegment(); 99 } 100 101 /** 102 * Releases a Segment. You should not use a Segment after you release it, 103 * and you should NEVER release the same Segment more than once, eg: 104 * <pre> 105 * segmentCache.releaseSegment(segment); 106 * segmentCache.releaseSegment(segment); 107 * </pre> 108 * Will likely result in very bad things happening! | 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package javax.swing.text; 26 27 import java.util.ArrayList; 28 import java.util.List; 29 30 /** 31 * SegmentCache caches {@code Segment}s to avoid continually creating 32 * and destroying of {@code Segment}s. A common use of this class would 33 * be: 34 * <pre> 35 * Segment segment = segmentCache.getSegment(); 36 * // do something with segment 37 * ... 38 * segmentCache.releaseSegment(segment); 39 * </pre> 40 * 41 */ 42 class SegmentCache { 43 /** 44 * A global cache. 45 */ 46 private static SegmentCache sharedCache = new SegmentCache(); 47 48 /** 49 * A list of the currently unused Segments. 50 */ 51 private List<Segment> segments; 52 53 54 /** 55 * Returns the shared SegmentCache. 56 */ 57 public static SegmentCache getSharedInstance() { 58 return sharedCache; 59 } 60 61 /** 62 * A convenience method to get a Segment from the shared 63 * {@code SegmentCache}. 64 */ 65 public static Segment getSharedSegment() { 66 return getSharedInstance().getSegment(); 67 } 68 69 /** 70 * A convenience method to release a Segment to the shared 71 * {@code SegmentCache}. 72 */ 73 public static void releaseSharedSegment(Segment segment) { 74 getSharedInstance().releaseSegment(segment); 75 } 76 77 78 79 /** 80 * Creates and returns a SegmentCache. 81 */ 82 public SegmentCache() { 83 segments = new ArrayList<Segment>(11); 84 } 85 86 /** 87 * Returns a {@code Segment}. When done, the {@code Segment} 88 * should be recycled by invoking {@code releaseSegment}. 89 */ 90 public Segment getSegment() { 91 synchronized(this) { 92 int size = segments.size(); 93 94 if (size > 0) { 95 return segments.remove(size - 1); 96 } 97 } 98 return new CachedSegment(); 99 } 100 101 /** 102 * Releases a Segment. You should not use a Segment after you release it, 103 * and you should NEVER release the same Segment more than once, eg: 104 * <pre> 105 * segmentCache.releaseSegment(segment); 106 * segmentCache.releaseSegment(segment); 107 * </pre> 108 * Will likely result in very bad things happening! |