1 /*
2 * Copyright (c) 1998, 2014, 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
41 * has been added to the <code>java.beans</code> package.
42 * Please see {@link java.beans.XMLEncoder}.
43 *
44 * @author Scott Violet
45 */
46 @SuppressWarnings("serial") // Same-version serialization only
47 public class TabSet implements Serializable
48 {
49 /** TabStops this TabSet contains. */
50 private TabStop[] tabs;
51 /**
52 * Since this class is immutable the hash code could be
53 * calculated once. MAX_VALUE means that it was not initialized
54 * yet. Hash code shouldn't has MAX_VALUE value.
55 */
56 private int hashCode = Integer.MAX_VALUE;
57
58 /**
59 * Creates and returns an instance of TabSet. The array of Tabs
60 * passed in must be sorted in ascending order.
61 */
62 public TabSet(TabStop[] tabs) {
63 // PENDING(sky): If this becomes a problem, make it sort.
64 if(tabs != null) {
65 int tabCount = tabs.length;
66
67 this.tabs = new TabStop[tabCount];
68 System.arraycopy(tabs, 0, this.tabs, 0, tabCount);
69 }
70 else
71 this.tabs = null;
72 }
73
74 /**
75 * Returns the number of Tab instances the receiver contains.
76 */
77 public int getTabCount() {
78 return (tabs == null) ? 0 : tabs.length;
79 }
80
81 /**
82 * Returns the TabStop at index <code>index</code>. This will throw an
83 * IllegalArgumentException if <code>index</code> is outside the range
84 * of tabs.
85 */
86 public TabStop getTab(int index) {
87 int numTabs = getTabCount();
88
89 if(index < 0 || index >= numTabs)
90 throw new IllegalArgumentException(index +
91 " is outside the range of tabs");
92 return tabs[index];
93 }
94
95 /**
96 * Returns the Tab instance after <code>location</code>. This will
97 * return null if there are no tabs after <code>location</code>.
98 */
99 public TabStop getTabAfter(float location) {
100 int index = getTabIndexAfter(location);
101
102 return (index == -1) ? null : tabs[index];
103 }
104
105 /**
106 * @return the index of the TabStop <code>tab</code>, or -1 if
107 * <code>tab</code> is not contained in the receiver.
108 */
109 public int getTabIndex(TabStop tab) {
110 for(int counter = getTabCount() - 1; counter >= 0; counter--)
111 // should this use .equals?
112 if(getTab(counter) == tab)
113 return counter;
114 return -1;
115 }
116
117 /**
118 * Returns the index of the Tab to be used after <code>location</code>.
119 * This will return -1 if there are no tabs after <code>location</code>.
120 */
121 public int getTabIndexAfter(float location) {
122 int current, min, max;
123
124 min = 0;
125 max = getTabCount();
126 while(min != max) {
127 current = (max - min) / 2 + min;
128 if(location > tabs[current].getPosition()) {
129 if(min == current)
130 min = max;
131 else
132 min = current;
133 }
134 else {
135 if(current == 0 || location > tabs[current - 1].getPosition())
136 return current;
137 max = current;
138 }
139 }
|
1 /*
2 * Copyright (c) 1998, 2015, 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
41 * has been added to the <code>java.beans</code> package.
42 * Please see {@link java.beans.XMLEncoder}.
43 *
44 * @author Scott Violet
45 */
46 @SuppressWarnings("serial") // Same-version serialization only
47 public class TabSet implements Serializable
48 {
49 /** TabStops this TabSet contains. */
50 private TabStop[] tabs;
51 /**
52 * Since this class is immutable the hash code could be
53 * calculated once. MAX_VALUE means that it was not initialized
54 * yet. Hash code shouldn't has MAX_VALUE value.
55 */
56 private int hashCode = Integer.MAX_VALUE;
57
58 /**
59 * Creates and returns an instance of TabSet. The array of Tabs
60 * passed in must be sorted in ascending order.
61 * @param tabs the TabStops to initialize the TabSet
62 */
63 public TabSet(TabStop[] tabs) {
64 // PENDING(sky): If this becomes a problem, make it sort.
65 if(tabs != null) {
66 int tabCount = tabs.length;
67
68 this.tabs = new TabStop[tabCount];
69 System.arraycopy(tabs, 0, this.tabs, 0, tabCount);
70 }
71 else
72 this.tabs = null;
73 }
74
75 /**
76 * Returns the number of Tab instances the receiver contains.
77 * @return the number of Tab instances the receiver contains
78 */
79 public int getTabCount() {
80 return (tabs == null) ? 0 : tabs.length;
81 }
82
83 /**
84 * Returns the TabStop at index <code>index</code>. This will throw an
85 * IllegalArgumentException if <code>index</code> is outside the range
86 * of tabs.
87 * @param index which TapStop to return
88 * @return the TabStop at index {@code index}
89 */
90 public TabStop getTab(int index) {
91 int numTabs = getTabCount();
92
93 if(index < 0 || index >= numTabs)
94 throw new IllegalArgumentException(index +
95 " is outside the range of tabs");
96 return tabs[index];
97 }
98
99 /**
100 * Returns the Tab instance after <code>location</code>. This will
101 * return null if there are no tabs after <code>location</code>.
102 * @param location location to find a Tab after
103 * @return the Tab instance after {@code location}
104 */
105 public TabStop getTabAfter(float location) {
106 int index = getTabIndexAfter(location);
107
108 return (index == -1) ? null : tabs[index];
109 }
110
111 /**
112 * Returns the index of the TabStop <code>tab</code>, or -1 if
113 * <code>tab</code> is not contained in the receiver.
114 * @param tab the TabStop to find
115 * @return the index of the TabStop <code>tab</code>, or -1 if
116 * <code>tab</code> is not contained in the receiver.
117 */
118 public int getTabIndex(TabStop tab) {
119 for(int counter = getTabCount() - 1; counter >= 0; counter--)
120 // should this use .equals?
121 if(getTab(counter) == tab)
122 return counter;
123 return -1;
124 }
125
126 /**
127 * Returns the index of the Tab to be used after <code>location</code>.
128 * This will return -1 if there are no tabs after <code>location</code>.
129 * @param location location to find a Tab after
130 * @return the index of the Tab to be used after <code>location</code>
131 */
132 public int getTabIndexAfter(float location) {
133 int current, min, max;
134
135 min = 0;
136 max = getTabCount();
137 while(min != max) {
138 current = (max - min) / 2 + min;
139 if(location > tabs[current].getPosition()) {
140 if(min == current)
141 min = max;
142 else
143 min = current;
144 }
145 else {
146 if(current == 0 || location > tabs[current - 1].getPosition())
147 return current;
148 max = current;
149 }
150 }
|