jdk/src/share/classes/javax/swing/text/SegmentCache.java
author malenkov
Fri, 04 Apr 2014 20:18:53 +0400
changeset 24158 6afb40c4e9f8
parent 5506 202f599c92aa
permissions -rw-r--r--
8039137: KSS: JTextComponent.isProcessInputMethodEventOverridden Reviewed-by: alexsch, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     2
 * Copyright (c) 2001, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * SegmentCache caches <code>Segment</code>s to avoid continually creating
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * and destroying of <code>Segment</code>s. A common use of this class would
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * be:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *   Segment segment = segmentCache.getSegment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *   // do something with segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *   ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *   segmentCache.releaseSegment(segment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
class SegmentCache {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * A global cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static SegmentCache sharedCache = new SegmentCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * A list of the currently unused Segments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
    51
    private List<Segment> segments;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Returns the shared SegmentCache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public static SegmentCache getSharedInstance() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        return sharedCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * A convenience method to get a Segment from the shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * <code>SegmentCache</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public static Segment getSharedSegment() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        return getSharedInstance().getSegment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * A convenience method to release a Segment to the shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * <code>SegmentCache</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public static void releaseSharedSegment(Segment segment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        getSharedInstance().releaseSegment(segment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Creates and returns a SegmentCache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public SegmentCache() {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
    83
        segments = new ArrayList<Segment>(11);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Returns a <code>Segment</code>. When done, the <code>Segment</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * should be recycled by invoking <code>releaseSegment</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public Segment getSegment() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            int size = segments.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            if (size > 0) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
    95
                return segments.remove(size - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return new CachedSegment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Releases a Segment. You should not use a Segment after you release it,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * and you should NEVER release the same Segment more than once, eg:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *   segmentCache.releaseSegment(segment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *   segmentCache.releaseSegment(segment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Will likely result in very bad things happening!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public void releaseSegment(Segment segment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (segment instanceof CachedSegment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                segment.array = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                segment.count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                segments.add(segment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * CachedSegment is used as a tagging interface to determine if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * a Segment can successfully be shared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private static class CachedSegment extends Segment {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
}