Home » openjdk-7 » javax » swing » text » [javadoc | source]

    1   /*
    2    * Copyright (c) 2001, 2008, Oracle and/or its affiliates. All rights reserved.
    3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    4    *
    5    * This code is free software; you can redistribute it and/or modify it
    6    * under the terms of the GNU General Public License version 2 only, as
    7    * published by the Free Software Foundation.  Oracle designates this
    8    * particular file as subject to the "Classpath" exception as provided
    9    * by Oracle in the LICENSE file that accompanied this code.
   10    *
   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!
  109        */
  110       public void releaseSegment(Segment segment) {
  111           if (segment instanceof CachedSegment) {
  112               synchronized(this) {
  113                   segment.array = null;
  114                   segment.count = 0;
  115                   segments.add(segment);
  116               }
  117           }
  118       }
  119   
  120   
  121       /**
  122        * CachedSegment is used as a tagging interface to determine if
  123        * a Segment can successfully be shared.
  124        */
  125       private static class CachedSegment extends Segment {
  126       }
  127   }

Home » openjdk-7 » javax » swing » text » [javadoc | source]