0de71344b4dd7e2fbd57622d8bb475e35921a336
2 * This file is part of lanterna (http://code.google.com/p/lanterna/).
4 * lanterna is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 * Copyright (C) 2010-2015 Martin
19 package com
.googlecode
.lanterna
;
22 * Terminal dimensions in 2-d space, measured in number of rows and columns. This class is immutable and cannot change
23 * its internal state after creation.
27 public class TerminalSize
{
28 public static final TerminalSize ZERO
= new TerminalSize(0, 0);
29 public static final TerminalSize ONE
= new TerminalSize(1, 1);
31 private final int columns
;
32 private final int rows
;
35 * Creates a new terminal size representation with a given width (columns) and height (rows)
36 * @param columns Width, in number of columns
37 * @param rows Height, in number of columns
39 public TerminalSize(int columns
, int rows
) {
41 throw new IllegalArgumentException("TerminalSize.columns cannot be less than 0!");
44 throw new IllegalArgumentException("TerminalSize.rows cannot be less than 0!");
46 this.columns
= columns
;
51 * @return Returns the width of this size representation, in number of columns
53 public int getColumns() {
58 * Creates a new size based on this size, but with a different width
59 * @param columns Width of the new size, in columns
60 * @return New size based on this one, but with a new width
62 public TerminalSize
withColumns(int columns
) {
63 if(this.columns
== columns
) {
66 if(columns
== 0 && this.rows
== 0) {
69 return new TerminalSize(columns
, this.rows
);
74 * @return Returns the height of this size representation, in number of rows
76 public int getRows() {
81 * Creates a new size based on this size, but with a different height
82 * @param rows Height of the new size, in rows
83 * @return New size based on this one, but with a new height
85 public TerminalSize
withRows(int rows
) {
86 if(this.rows
== rows
) {
89 if(rows
== 0 && this.columns
== 0) {
92 return new TerminalSize(this.columns
, rows
);
96 * Creates a new TerminalSize object representing a size with the same number of rows, but with a column size offset by a
97 * supplied value. Calling this method with delta 0 will return this, calling it with a positive delta will return
98 * a terminal size <i>delta</i> number of columns wider and for negative numbers shorter.
99 * @param delta Column offset
100 * @return New terminal size based off this one but with an applied transformation
102 public TerminalSize
withRelativeColumns(int delta
) {
106 return withColumns(columns
+ delta
);
110 * Creates a new TerminalSize object representing a size with the same number of columns, but with a row size offset by a
111 * supplied value. Calling this method with delta 0 will return this, calling it with a positive delta will return
112 * a terminal size <i>delta</i> number of rows longer and for negative numbers shorter.
113 * @param delta Row offset
114 * @return New terminal size based off this one but with an applied transformation
116 public TerminalSize
withRelativeRows(int delta
) {
120 return withRows(rows
+ delta
);
124 * Creates a new TerminalSize object representing a size based on this object's size but with a delta applied.
125 * This is the same as calling
126 * <code>withRelativeColumns(delta.getColumns()).withRelativeRows(delta.getRows())</code>
127 * @param delta Column and row offset
128 * @return New terminal size based off this one but with an applied resize
130 public TerminalSize
withRelative(TerminalSize delta
) {
131 return withRelative(delta
.getColumns(), delta
.getRows());
135 * Creates a new TerminalSize object representing a size based on this object's size but with a delta applied.
136 * This is the same as calling
137 * <code>withRelativeColumns(deltaColumns).withRelativeRows(deltaRows)</code>
138 * @param deltaColumns How many extra columns the new TerminalSize will have (negative values are allowed)
139 * @param deltaRows How many extra rows the new TerminalSize will have (negative values are allowed)
140 * @return New terminal size based off this one but with an applied resize
142 public TerminalSize
withRelative(int deltaColumns
, int deltaRows
) {
143 return withRelativeRows(deltaRows
).withRelativeColumns(deltaColumns
);
147 * Takes a different TerminalSize and returns a new TerminalSize that has the largest dimensions of the two,
148 * measured separately. So calling 3x5 on a 5x3 will return 5x5.
149 * @param other Other TerminalSize to compare with
150 * @return TerminalSize that combines the maximum width between the two and the maximum height
152 public TerminalSize
max(TerminalSize other
) {
153 return withColumns(Math
.max(columns
, other
.columns
))
154 .withRows(Math
.max(rows
, other
.rows
));
158 * Takes a different TerminalSize and returns a new TerminalSize that has the smallest dimensions of the two,
159 * measured separately. So calling 3x5 on a 5x3 will return 3x3.
160 * @param other Other TerminalSize to compare with
161 * @return TerminalSize that combines the minimum width between the two and the minimum height
163 public TerminalSize
min(TerminalSize other
) {
164 return withColumns(Math
.min(columns
, other
.columns
))
165 .withRows(Math
.min(rows
, other
.rows
));
169 * Returns itself if it is equal to the supplied size, otherwise the supplied size. You can use this if you have a
170 * size field which is frequently recalculated but often resolves to the same size; it will keep the same object
171 * in memory instead of swapping it out every cycle.
172 * @param size Size you want to return
173 * @return Itself if this size equals the size passed in, otherwise the size passed in
175 public TerminalSize
with(TerminalSize size
) {
183 public String
toString() {
184 return "{" + columns
+ "x" + rows
+ "}";
188 public boolean equals(Object obj
) {
192 if (!(obj
instanceof TerminalSize
)) {
196 TerminalSize other
= (TerminalSize
) obj
;
197 return columns
== other
.columns
198 && rows
== other
.rows
;
202 public int hashCode() {
204 hash
= 53 * hash
+ this.columns
;
205 hash
= 53 * hash
+ this.rows
;