src/java.desktop/share/classes/sun/java2d/Spans.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 35667 jdk/src/java.desktop/share/classes/sun/java2d/Spans.java@ed476aba94de
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
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: 2
diff changeset
     2
 * Copyright (c) 1998, 2000, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.java2d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Maintains a list of half-open intervals, called Spans.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * A Span can be tested against the list of Spans
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * for intersection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class Spans {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * This class will sort and collapse its span
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     * entries after this many span additions via
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    44
     * the {@code add} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static final int kMaxAddsSinceSort = 256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * Holds a list of individual
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * Span instances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     */
24522
3a0bbf9f5e81 8038644: Fix raw and unchecked warnings in sun.java2d.*
henryjen
parents: 5506
diff changeset
    52
    private List<Span> mSpans = new Vector<>(kMaxAddsSinceSort);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    55
     * The number of {@code Span}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * instances that have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * to this object without a sort
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * and collapse taking place.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private int mAddsSinceSort = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public Spans() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Add a span covering the half open interval
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    68
     * including {@code start} up to
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    69
     * but not including {@code end}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public void add(float start, float end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if (mSpans != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            mSpans.add(new Span(start, end));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            if (++mAddsSinceSort >= kMaxAddsSinceSort) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                sortAndCollapse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Add a span which covers the entire range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * This call is logically equivalent to
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    85
     * {@code add(Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * The result of making this call is that
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    87
     * all future {@code add} calls are ignored
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    88
     * and the {@code intersects} method always
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public void addInfinite() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        mSpans = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Returns true if the span defined by the half-open
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    97
     * interval from {@code start} up to,
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    98
     * but not including, {@code end} intersects
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * any of the spans defined by this instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public boolean intersects(float start, float end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        boolean doesIntersect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (mSpans != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            /* If we have added any spans since we last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
             * sorted and collapsed our list of spans
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
             * then we need to resort and collapse.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            if (mAddsSinceSort > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                sortAndCollapse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            /* The SpanIntersection comparator considers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
             * two spans equal if they intersect. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
             * the search finds a match then we have an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
             * intersection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            int found = Collections.binarySearch(mSpans,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                                                 new Span(start, end),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                                                 SpanIntersection.instance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            doesIntersect = found >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        /* The addInfinite() method has been invoked so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
         * everything intersect this instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
           doesIntersect = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return doesIntersect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Sort the spans in ascending order by their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * start position. After the spans are sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * collapse any spans that intersect into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * single span. The result is a sorted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * non-overlapping list of spans.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private void sortAndCollapse() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        Collections.sort(mSpans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        mAddsSinceSort = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
24522
3a0bbf9f5e81 8038644: Fix raw and unchecked warnings in sun.java2d.*
henryjen
parents: 5506
diff changeset
   147
        Iterator<Span> iter = mSpans.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        /* Have 'span' start at the first span in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
         * the collection. The collection may be empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
         * so we're careful.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        Span span = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (iter.hasNext()) {
24522
3a0bbf9f5e81 8038644: Fix raw and unchecked warnings in sun.java2d.*
henryjen
parents: 5506
diff changeset
   155
            span = iter.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        /* Loop over the spans collapsing those that intersect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
         * into a single span.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
24522
3a0bbf9f5e81 8038644: Fix raw and unchecked warnings in sun.java2d.*
henryjen
parents: 5506
diff changeset
   163
            Span nextSpan = iter.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            /* The spans are in ascending start position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
             * order and so the next span's starting point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
             * is either in the span we are trying to grow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
             * or it is beyond the first span and thus the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
             * two spans do not intersect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
             * span:    <----------<
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
             * nextSpan:        <------         (intersects)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
             * nextSpan:                <------ (doesn't intersect)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
             * If the spans intersect then we'll remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
             * nextSpan from the list. If nextSpan's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
             * ending was beyond the first's then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
             * we extend the first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
             * span:    <----------<
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
             * nextSpan:   <-----<              (don't change span)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
             * nextSpan:        <-----------<   (grow span)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            if (span.subsume(nextSpan)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                iter.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            /* The next span did not intersect the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
             * span and so it can not be collapsed. Instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
             * it becomes the start of the next set of spans
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
             * to be collapsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                span = nextSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    // For debugging.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    private void printSpans() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        System.out.println("----------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (mSpans != null) {
24522
3a0bbf9f5e81 8038644: Fix raw and unchecked warnings in sun.java2d.*
henryjen
parents: 5506
diff changeset
   205
            Iterator<Span> iter = mSpans.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            while (iter.hasNext()) {
24522
3a0bbf9f5e81 8038644: Fix raw and unchecked warnings in sun.java2d.*
henryjen
parents: 5506
diff changeset
   207
                Span span = iter.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                System.out.println(span);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        System.out.println("----------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Holds a single half-open interval.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
24522
3a0bbf9f5e81 8038644: Fix raw and unchecked warnings in sun.java2d.*
henryjen
parents: 5506
diff changeset
   219
    static class Span implements Comparable<Span> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
         * The span includes the starting point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        private float mStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
         * The span goes up to but does not include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
         * the ending point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        private float mEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
         * Create a half-open interval including
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   234
         * {@code start} but not including
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   235
         * {@code end}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        Span(float start, float end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            mStart = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            mEnd = end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   243
         * Return the start of the {@code Span}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
         * The start is considered part of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
         * half-open interval.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        final float getStart() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            return mStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   252
         * Return the end of the {@code Span}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
         * The end is not considered part of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
         * half-open interval.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        final float getEnd() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            return mEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
         * Change the initial position of the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   262
         * {@code Span}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        final void setStart(float start) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            mStart = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
         * Change the terminal position of the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   270
         * {@code Span}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        final void setEnd(float end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            mEnd = end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   277
         * Attempt to alter this {@code Span}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   278
         * to include {@code otherSpan} without
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
         * altering this span's starting position.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   280
         * If {@code otherSpan} can be so consumed
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   281
         * by this {@code Span} then {@code true}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
         * is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        boolean subsume(Span otherSpan) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            /* We can only subsume 'otherSpan' if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
             * its starting position lies in our
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
             * interval.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            boolean isSubsumed = contains(otherSpan.mStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            /* If the other span's starting position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
             * was in our interval and the other span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
             * was longer than this span, then we need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
             * to grow this span to cover the difference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            if (isSubsumed && otherSpan.mEnd > mEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                mEnd = otherSpan.mEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            return isSubsumed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
         * Return true if the passed in position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
         * lies in the half-open interval defined
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   307
         * by this {@code Span}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        boolean contains(float pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            return mStart <= pos && pos < mEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
         * Rank spans according to their starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
         * position. The end position is ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
         * in this ranking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
         */
24522
3a0bbf9f5e81 8038644: Fix raw and unchecked warnings in sun.java2d.*
henryjen
parents: 5506
diff changeset
   318
        public int compareTo(Span otherSpan) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            float otherStart = otherSpan.getStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            int result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            if (mStart < otherStart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                result = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            } else if (mStart > otherStart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            return "Span: " + mStart + " to " + mEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   340
     * This class ranks a pair of {@code Span}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * instances. If the instances intersect they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * are deemed equal otherwise they are ranked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * by their relative position. Use
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   344
     * {@code SpanIntersection.instance} to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * get the single instance of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     */
24522
3a0bbf9f5e81 8038644: Fix raw and unchecked warnings in sun.java2d.*
henryjen
parents: 5506
diff changeset
   347
    static class SpanIntersection implements Comparator<Span> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
         * This class is a Singleton and the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
         * is the single instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        static final SpanIntersection instance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                                      new SpanIntersection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
         * Only this class can create instances of itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        private SpanIntersection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
24522
3a0bbf9f5e81 8038644: Fix raw and unchecked warnings in sun.java2d.*
henryjen
parents: 5506
diff changeset
   363
        public int compare(Span span1, Span span2) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            int result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            /* Span 1 is entirely to the left of span2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
             * span1:   <-----<
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
             * span2:            <-----<
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            if (span1.getEnd() <= span2.getStart()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                result = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            /* Span 2 is entirely to the right of span2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
             * span1:                     <-----<
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
             * span2:            <-----<
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            } else if (span1.getStart() >= span2.getEnd()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                result = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            /* Otherwise they intersect and we declare them equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
}