📄 intrange.java
字号:
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* http://www.gnu.org/copyleft/gpl.html
*/
package l1j.server.server.utils;
import java.util.Random;
/**
* <p>
* 最低値lowと最大値highによって囲まれた、数値の範囲を指定するクラス。
* </p>
* <p>
* <b>このクラスは同期化されない。</b> 複数のスレッドが同時にこのクラスのインスタンスにアクセスし、
* 1つ以上のスレッドが範囲を変更する場合、外部的な同期化が必要である。
* </p>
*/
public class IntRange {
private static final Random _rnd = new Random();
private int _low;
private int _high;
public IntRange(int low, int high) {
_low = low;
_high = high;
}
public IntRange(IntRange range) {
this(range._low, range._high);
}
/**
* 数値iが、範囲内にあるかを返す。
*
* @param i
* 数値
* @return 範囲内であればtrue
*/
public boolean includes(int i) {
return (_low <= i) && (i <= _high);
}
public static boolean includes(int i, int low, int high) {
return (low <= i) && (i <= high);
}
/**
* 数値iを、この範囲内に丸める。
*
* @param i
* 数値
* @return 丸められた値
*/
public int ensure(int i) {
int r = i;
r = (_low <= r) ? r : _low;
r = (r <= _high) ? r : _high;
return r;
}
public static int ensure(int n, int low, int high) {
int r = n;
r = (low <= r) ? r : low;
r = (r <= high) ? r : high;
return r;
}
/**
* この範囲内からランダムな値を生成する。
*
* @return 範囲内のランダムな値
*/
public int randomValue() {
return _rnd.nextInt(getWidth() + 1) + _low;
}
public int getLow() {
return _low;
}
public int getHigh() {
return _high;
}
public int getWidth() {
return _high - _low;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof IntRange)) {
return false;
}
IntRange range = (IntRange) obj;
return (this._low == range._low) && (this._high == range._high);
}
@Override
public String toString() {
return "low=" + _low + ", high=" + _high;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -