本文整理汇总了Java中java.util.concurrent.TimeUnit.values方法的典型用法代码示例。如果您正苦于以下问题:Java TimeUnit.values方法的具体用法?Java TimeUnit.values怎么用?Java TimeUnit.values使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.TimeUnit
的用法示例。
在下文中一共展示了TimeUnit.values方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: toStringAsLargestTimeUnit
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
/**
* Returns a string representation of this duration with the largest time unit this duration can be converted to where <code>length > 0</code>.
* Returns {@link #toString()} if no such time unit exists excluding this duration's time unit, or <code>length = 0</code>.
*
* @param length the length
* @param unit the unit
* @return a string representation of this duration with largest time unit this duration can be converted to where <code>length > 0</code>
*/
public static String toStringAsLargestTimeUnit(final long length, final TimeUnit unit) {
if (length > 0 && !unit.equals(TimeUnit.DAYS)) {
final TimeUnit[] units = TimeUnit.values();
int i = units.length - 1;
TimeUnit new_unit = units[i];
// Find the largest TimeUnit that can present this duration in a non-zero length. The unit is always bigger than or equal to the current unit.
while (!unit.equals(new_unit)) {
final long length_in_new_unit = new_unit.convert(length, unit);
if (length_in_new_unit != 0) {
return toString(length_in_new_unit, new_unit);
}
new_unit = units[i--];
}
}
return toString(length, unit);
}
示例2: testTimeUnit
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
@Test
public void testTimeUnit() throws IOException {
final EnumSet<TimeUnit> optimizedTimeUnits =
EnumSet.range(TimeUnit.NANOSECONDS, TimeUnit.SECONDS);
for (TimeUnit v : TimeUnit.values()) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(baos);
DataSerializer.writeObject(v, out);
byte[] bytes = baos.toByteArray();
String type = DataType.getDataType(bytes); // 4?
if (optimizedTimeUnits.contains(v)) {
assertEquals("for enum " + v, "java.util.concurrent.TimeUnit", type);
} else {
assertEquals("for enum " + v, "java.lang.Enum:java.util.concurrent.TimeUnit", type);
}
}
}
示例3: testToMicrosSaturate
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
/**
* toMicros saturates positive too-large values to Long.MAX_VALUE
* and negative to LONG.MIN_VALUE
*/
public void testToMicrosSaturate() {
for (TimeUnit x : TimeUnit.values()) {
long ratio = x.toNanos(1) / MICROSECONDS.toNanos(1);
if (ratio >= 1) {
long max = Long.MAX_VALUE/ratio;
for (long z : new long[] {0, 1, -1, max, -max})
assertEquals(z * ratio, x.toMicros(z));
if (max < Long.MAX_VALUE) {
assertEquals(Long.MAX_VALUE, x.toMicros(max + 1));
assertEquals(Long.MIN_VALUE, x.toMicros(-max - 1));
assertEquals(Long.MIN_VALUE, x.toMicros(Long.MIN_VALUE + 1));
}
assertEquals(Long.MAX_VALUE, x.toMicros(Long.MAX_VALUE));
assertEquals(Long.MIN_VALUE, x.toMicros(Long.MIN_VALUE));
if (max < Integer.MAX_VALUE) {
assertEquals(Long.MAX_VALUE, x.toMicros(Integer.MAX_VALUE));
assertEquals(Long.MIN_VALUE, x.toMicros(Integer.MIN_VALUE));
}
}
}
}
示例4: testToMillisSaturate
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
/**
* toMillis saturates positive too-large values to Long.MAX_VALUE
* and negative to LONG.MIN_VALUE
*/
public void testToMillisSaturate() {
for (TimeUnit x : TimeUnit.values()) {
long ratio = x.toNanos(1) / MILLISECONDS.toNanos(1);
if (ratio >= 1) {
long max = Long.MAX_VALUE/ratio;
for (long z : new long[] {0, 1, -1, max, -max})
assertEquals(z * ratio, x.toMillis(z));
if (max < Long.MAX_VALUE) {
assertEquals(Long.MAX_VALUE, x.toMillis(max + 1));
assertEquals(Long.MIN_VALUE, x.toMillis(-max - 1));
assertEquals(Long.MIN_VALUE, x.toMillis(Long.MIN_VALUE + 1));
}
assertEquals(Long.MAX_VALUE, x.toMillis(Long.MAX_VALUE));
assertEquals(Long.MIN_VALUE, x.toMillis(Long.MIN_VALUE));
if (max < Integer.MAX_VALUE) {
assertEquals(Long.MAX_VALUE, x.toMillis(Integer.MAX_VALUE));
assertEquals(Long.MIN_VALUE, x.toMillis(Integer.MIN_VALUE));
}
}
}
}
示例5: testToSecondsSaturate
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
/**
* toSeconds saturates positive too-large values to Long.MAX_VALUE
* and negative to LONG.MIN_VALUE
*/
public void testToSecondsSaturate() {
for (TimeUnit x : TimeUnit.values()) {
long ratio = x.toNanos(1) / SECONDS.toNanos(1);
if (ratio >= 1) {
long max = Long.MAX_VALUE/ratio;
for (long z : new long[] {0, 1, -1, max, -max})
assertEquals(z * ratio, x.toSeconds(z));
if (max < Long.MAX_VALUE) {
assertEquals(Long.MAX_VALUE, x.toSeconds(max + 1));
assertEquals(Long.MIN_VALUE, x.toSeconds(-max - 1));
assertEquals(Long.MIN_VALUE, x.toSeconds(Long.MIN_VALUE + 1));
}
assertEquals(Long.MAX_VALUE, x.toSeconds(Long.MAX_VALUE));
assertEquals(Long.MIN_VALUE, x.toSeconds(Long.MIN_VALUE));
if (max < Integer.MAX_VALUE) {
assertEquals(Long.MAX_VALUE, x.toSeconds(Integer.MAX_VALUE));
assertEquals(Long.MIN_VALUE, x.toSeconds(Integer.MIN_VALUE));
}
}
}
}
示例6: testToMinutesSaturate
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
/**
* toMinutes saturates positive too-large values to Long.MAX_VALUE
* and negative to LONG.MIN_VALUE
*/
public void testToMinutesSaturate() {
for (TimeUnit x : TimeUnit.values()) {
long ratio = x.toNanos(1) / MINUTES.toNanos(1);
if (ratio > 1) {
long max = Long.MAX_VALUE/ratio;
for (long z : new long[] {0, 1, -1, max, -max})
assertEquals(z * ratio, x.toMinutes(z));
assertEquals(Long.MAX_VALUE, x.toMinutes(max + 1));
assertEquals(Long.MIN_VALUE, x.toMinutes(-max - 1));
assertEquals(Long.MAX_VALUE, x.toMinutes(Long.MAX_VALUE));
assertEquals(Long.MIN_VALUE, x.toMinutes(Long.MIN_VALUE));
assertEquals(Long.MIN_VALUE, x.toMinutes(Long.MIN_VALUE + 1));
}
}
}
示例7: testToHoursSaturate
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
/**
* toHours saturates positive too-large values to Long.MAX_VALUE
* and negative to LONG.MIN_VALUE
*/
public void testToHoursSaturate() {
for (TimeUnit x : TimeUnit.values()) {
long ratio = x.toNanos(1) / HOURS.toNanos(1);
if (ratio >= 1) {
long max = Long.MAX_VALUE/ratio;
for (long z : new long[] {0, 1, -1, max, -max})
assertEquals(z * ratio, x.toHours(z));
if (max < Long.MAX_VALUE) {
assertEquals(Long.MAX_VALUE, x.toHours(max + 1));
assertEquals(Long.MIN_VALUE, x.toHours(-max - 1));
assertEquals(Long.MIN_VALUE, x.toHours(Long.MIN_VALUE + 1));
}
assertEquals(Long.MAX_VALUE, x.toHours(Long.MAX_VALUE));
assertEquals(Long.MIN_VALUE, x.toHours(Long.MIN_VALUE));
}
}
}
示例8: doFillUnitItems
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
public ListBoxModel doFillUnitItems() {
ListBoxModel r = new ListBoxModel();
for (TimeUnit unit : TimeUnit.values()) {
r.add(unit.name());
}
return r;
}
示例9: to
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
static void to(long v, TimeUnit unit) {
FileTime t = FileTime.from(v, unit);
for (TimeUnit u: TimeUnit.values()) {
long result = t.to(u);
long expected = u.convert(v, unit);
if (result != expected) {
throw new RuntimeException("unexpected result");
}
}
}
示例10: testToChronoUnit
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
/**
* tests for toChronoUnit.
*/
public void testToChronoUnit() throws Exception {
assertSame(ChronoUnit.NANOS, NANOSECONDS.toChronoUnit());
assertSame(ChronoUnit.MICROS, MICROSECONDS.toChronoUnit());
assertSame(ChronoUnit.MILLIS, MILLISECONDS.toChronoUnit());
assertSame(ChronoUnit.SECONDS, SECONDS.toChronoUnit());
assertSame(ChronoUnit.MINUTES, MINUTES.toChronoUnit());
assertSame(ChronoUnit.HOURS, HOURS.toChronoUnit());
assertSame(ChronoUnit.DAYS, DAYS.toChronoUnit());
// Every TimeUnit has a defined ChronoUnit equivalent
for (TimeUnit x : TimeUnit.values())
assertSame(x, TimeUnit.of(x.toChronoUnit()));
}
示例11: testConvertSaturate
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
/**
* convert saturates positive too-large values to Long.MAX_VALUE
* and negative to LONG.MIN_VALUE
*/
public void testConvertSaturate() {
assertEquals(Long.MAX_VALUE,
NANOSECONDS.convert(Long.MAX_VALUE / 2, SECONDS));
assertEquals(Long.MIN_VALUE,
NANOSECONDS.convert(-Long.MAX_VALUE / 4, SECONDS));
assertEquals(Long.MAX_VALUE,
NANOSECONDS.convert(Long.MAX_VALUE / 2, MINUTES));
assertEquals(Long.MIN_VALUE,
NANOSECONDS.convert(-Long.MAX_VALUE / 4, MINUTES));
assertEquals(Long.MAX_VALUE,
NANOSECONDS.convert(Long.MAX_VALUE / 2, HOURS));
assertEquals(Long.MIN_VALUE,
NANOSECONDS.convert(-Long.MAX_VALUE / 4, HOURS));
assertEquals(Long.MAX_VALUE,
NANOSECONDS.convert(Long.MAX_VALUE / 2, DAYS));
assertEquals(Long.MIN_VALUE,
NANOSECONDS.convert(-Long.MAX_VALUE / 4, DAYS));
for (TimeUnit x : TimeUnit.values())
for (TimeUnit y : TimeUnit.values()) {
long ratio = x.toNanos(1) / y.toNanos(1);
if (ratio >= 1) {
assertEquals(ratio, y.convert(1, x));
assertEquals(1, x.convert(ratio, y));
long max = Long.MAX_VALUE/ratio;
assertEquals(max * ratio, y.convert(max, x));
assertEquals(-max * ratio, y.convert(-max, x));
assertEquals(max, x.convert(max * ratio, y));
assertEquals(-max, x.convert(-max * ratio, y));
if (max < Long.MAX_VALUE) {
assertEquals(Long.MAX_VALUE, y.convert(max + 1, x));
assertEquals(Long.MIN_VALUE, y.convert(-max - 1, x));
assertEquals(Long.MIN_VALUE, y.convert(Long.MIN_VALUE + 1, x));
}
assertEquals(Long.MAX_VALUE, y.convert(Long.MAX_VALUE, x));
assertEquals(Long.MIN_VALUE, y.convert(Long.MIN_VALUE, x));
}
}
}
示例12: testToNanosSaturate
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
/**
* toNanos saturates positive too-large values to Long.MAX_VALUE
* and negative to LONG.MIN_VALUE
*/
public void testToNanosSaturate() {
assertEquals(Long.MAX_VALUE,
MILLISECONDS.toNanos(Long.MAX_VALUE / 2));
assertEquals(Long.MIN_VALUE,
MILLISECONDS.toNanos(-Long.MAX_VALUE / 3));
for (TimeUnit x : TimeUnit.values()) {
long ratio = x.toNanos(1) / NANOSECONDS.toNanos(1);
if (ratio >= 1) {
long max = Long.MAX_VALUE/ratio;
for (long z : new long[] {0, 1, -1, max, -max})
assertEquals(z * ratio, x.toNanos(z));
if (max < Long.MAX_VALUE) {
assertEquals(Long.MAX_VALUE, x.toNanos(max + 1));
assertEquals(Long.MIN_VALUE, x.toNanos(-max - 1));
assertEquals(Long.MIN_VALUE, x.toNanos(Long.MIN_VALUE + 1));
}
assertEquals(Long.MAX_VALUE, x.toNanos(Long.MAX_VALUE));
assertEquals(Long.MIN_VALUE, x.toNanos(Long.MIN_VALUE));
if (max < Integer.MAX_VALUE) {
assertEquals(Long.MAX_VALUE, x.toNanos(Integer.MAX_VALUE));
assertEquals(Long.MIN_VALUE, x.toNanos(Integer.MIN_VALUE));
}
}
}
}
示例13: compress
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
public TimeAmount compress() {
TimeUnit lossless = this.unit;
TimeUnit[] timeUnits = TimeUnit.values();
for (int i = lossless.ordinal() + 1; i < timeUnits.length; i++) {
TimeUnit timeUnit = timeUnits[i];
//If do back and forth conversion is lossless
if (unit.convert(timeUnit.convert(amount, unit), timeUnit) == amount) lossless = timeUnit;
else break;
}
return convertTo(lossless);
}
示例14: randomTimeUnit
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
private TimeUnit randomTimeUnit() {
return TimeUnit.values()[between(0, TimeUnit.values().length - 1)];
}
示例15: sendNpcView
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
public static void sendNpcView(L2PcInstance activeChar, L2Npc npc)
{
final NpcHtmlMessage html = new NpcHtmlMessage();
html.setFile(activeChar.getHtmlPrefix(), "data/html/mods/NpcView/Info.htm");
html.replace("%name%", npc.getName());
html.replace("%hpGauge%", HtmlUtil.getHpGauge(250, (long) npc.getCurrentHp(), npc.getMaxHp(), false));
html.replace("%mpGauge%", HtmlUtil.getMpGauge(250, (long) npc.getCurrentMp(), npc.getMaxMp(), false));
final L2Spawn npcSpawn = npc.getSpawn();
if ((npcSpawn == null) || (npcSpawn.getRespawnMinDelay() == 0))
{
html.replace("%respawn%", "None");
}
else
{
TimeUnit timeUnit = TimeUnit.MILLISECONDS;
long min = Long.MAX_VALUE;
for (TimeUnit tu : TimeUnit.values())
{
final long minTimeFromMillis = tu.convert(npcSpawn.getRespawnMinDelay(), TimeUnit.MILLISECONDS);
final long maxTimeFromMillis = tu.convert(npcSpawn.getRespawnMaxDelay(), TimeUnit.MILLISECONDS);
if ((TimeUnit.MILLISECONDS.convert(minTimeFromMillis, tu) == npcSpawn.getRespawnMinDelay()) && (TimeUnit.MILLISECONDS.convert(maxTimeFromMillis, tu) == npcSpawn.getRespawnMaxDelay()))
{
if (min > minTimeFromMillis)
{
min = minTimeFromMillis;
timeUnit = tu;
}
}
}
final long minRespawnDelay = timeUnit.convert(npcSpawn.getRespawnMinDelay(), TimeUnit.MILLISECONDS);
final long maxRespawnDelay = timeUnit.convert(npcSpawn.getRespawnMaxDelay(), TimeUnit.MILLISECONDS);
final String timeUnitName = timeUnit.name().charAt(0) + timeUnit.name().toLowerCase().substring(1);
if (npcSpawn.hasRespawnRandom())
{
html.replace("%respawn%", minRespawnDelay + "-" + maxRespawnDelay + " " + timeUnitName);
}
else
{
html.replace("%respawn%", minRespawnDelay + " " + timeUnitName);
}
}
html.replace("%atktype%", CommonUtil.capitalizeFirst(npc.getAttackType().name().toLowerCase()));
html.replace("%atkrange%", npc.getStat().getPhysicalAttackRange());
html.replace("%patk%", npc.getPAtk());
html.replace("%pdef%", npc.getPDef());
html.replace("%matk%", npc.getMAtk());
html.replace("%mdef%", npc.getMDef());
html.replace("%atkspd%", npc.getPAtkSpd());
html.replace("%castspd%", npc.getMAtkSpd());
html.replace("%critrate%", npc.getStat().getCriticalHit());
html.replace("%evasion%", npc.getEvasionRate());
html.replace("%accuracy%", npc.getStat().getAccuracy());
html.replace("%speed%", (int) npc.getStat().getMoveSpeed());
html.replace("%attributeatktype%", npc.getStat().getAttackElement().name());
html.replace("%attributeatkvalue%", npc.getStat().getAttackElementValue(npc.getStat().getAttackElement()));
html.replace("%attributefire%", npc.getStat().getDefenseElementValue(AttributeType.FIRE));
html.replace("%attributewater%", npc.getStat().getDefenseElementValue(AttributeType.WATER));
html.replace("%attributewind%", npc.getStat().getDefenseElementValue(AttributeType.WIND));
html.replace("%attributeearth%", npc.getStat().getDefenseElementValue(AttributeType.EARTH));
html.replace("%attributedark%", npc.getStat().getDefenseElementValue(AttributeType.DARK));
html.replace("%attributeholy%", npc.getStat().getDefenseElementValue(AttributeType.HOLY));
html.replace("%dropListButtons%", getDropListButtons(npc));
activeChar.sendPacket(html);
}