Java Code Examples for java.util.concurrent.TimeUnit.values()

The following are Jave code examples for showing how to use values() of the java.util.concurrent.TimeUnit class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: shabdiz   File: Duration.java   View Source Code Vote up 6 votes
/**
 * 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);
}
 
Example 2
Project: monarch   File: DataTypeJUnitTest.java   View Source Code Vote up 6 votes
@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);
    }
  }
}
 
Example 3
Project: openjdk-jdk10   File: TimeUnitTest.java   View Source Code Vote up 6 votes
/**
 * 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));
            }
        }
    }
}
 
Example 4
Project: openjdk-jdk10   File: TimeUnitTest.java   View Source Code Vote up 6 votes
/**
 * 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));
            }
        }
    }
}
 
Example 5
Project: openjdk-jdk10   File: TimeUnitTest.java   View Source Code Vote up 6 votes
/**
 * 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));
            }
        }
    }
}
 
Example 6
Project: openjdk-jdk10   File: TimeUnitTest.java   View Source Code Vote up 6 votes
/**
 * 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));
        }
    }
}
 
Example 7
Project: openjdk-jdk10   File: TimeUnitTest.java   View Source Code Vote up 6 votes
/**
 * 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));
        }
    }
}
 
Example 8
Project: Jenkins-Plugin-Examples   File: TimeoutStep.java   View Source Code Vote up 5 votes
public ListBoxModel doFillUnitItems() {
    ListBoxModel r = new ListBoxModel();
    for (TimeUnit unit : TimeUnit.values()) {
        r.add(unit.name());
    }
    return r;
}
 
Example 9
Project: Jenkins-Plugin-Examples   File: SleepStep.java   View Source Code Vote up 5 votes
public ListBoxModel doFillUnitItems() {
    ListBoxModel r = new ListBoxModel();
    for (TimeUnit unit : TimeUnit.values()) {
        r.add(unit.name());
    }
    return r;
}
 
Example 10
Project: jdk8u-jdk   File: Basic.java   View Source Code Vote up 5 votes
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");
        }
    }
}
 
Example 11
Project: openjdk-jdk10   File: TimeUnit8Test.java   View Source Code Vote up 5 votes
/**
 * 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()));
}
 
Example 12
Project: openjdk-jdk10   File: TimeUnitTest.java   View Source Code Vote up 5 votes
/**
 * 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));
            }
        }
}
 
Example 13
Project: openjdk-jdk10   File: TimeUnitTest.java   View Source Code Vote up 5 votes
/**
 * 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));
            }
        }
    }
}
 
Example 14
Project: openjdk-jdk10   File: Basic.java   View Source Code Vote up 5 votes
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");
        }
    }
}
 
Example 15
Project: BRjLibs   File: TimeAmount.java   View Source Code Vote up 5 votes
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);
}
 
Example 16
Project: elasticsearch_my   File: EsExecutorsTests.java   View Source Code Vote up 4 votes
private TimeUnit randomTimeUnit() {
    return TimeUnit.values()[between(0, TimeUnit.values().length - 1)];
}
 
Example 17
Project: L2J-Global   File: NpcViewMod.java   View Source Code Vote up 4 votes
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);
}
 
Example 18
Project: openjdk-jdk10   File: TimeUnitTest.java   View Source Code Vote up 4 votes
/**
 * convert correctly converts sample values across the units
 */
public void testConvert() {
    for (long t = 0; t < 88888; ++t) {
        assertEquals(t*60*60*24,
                     SECONDS.convert(t, DAYS));
        assertEquals(t*60*60,
                     SECONDS.convert(t, HOURS));
        assertEquals(t*60,
                     SECONDS.convert(t, MINUTES));
        assertEquals(t,
                     SECONDS.convert(t, SECONDS));
        assertEquals(t,
                     SECONDS.convert(1000L*t, MILLISECONDS));
        assertEquals(t,
                     SECONDS.convert(1000000L*t, MICROSECONDS));
        assertEquals(t,
                     SECONDS.convert(1000000000L*t, NANOSECONDS));

        assertEquals(1000L*t*60*60*24,
                     MILLISECONDS.convert(t, DAYS));
        assertEquals(1000L*t*60*60,
                     MILLISECONDS.convert(t, HOURS));
        assertEquals(1000L*t*60,
                     MILLISECONDS.convert(t, MINUTES));
        assertEquals(1000L*t,
                     MILLISECONDS.convert(t, SECONDS));
        assertEquals(t,
                     MILLISECONDS.convert(t, MILLISECONDS));
        assertEquals(t,
                     MILLISECONDS.convert(1000L*t, MICROSECONDS));
        assertEquals(t,
                     MILLISECONDS.convert(1000000L*t, NANOSECONDS));

        assertEquals(1000000L*t*60*60*24,
                     MICROSECONDS.convert(t, DAYS));
        assertEquals(1000000L*t*60*60,
                     MICROSECONDS.convert(t, HOURS));
        assertEquals(1000000L*t*60,
                     MICROSECONDS.convert(t, MINUTES));
        assertEquals(1000000L*t,
                     MICROSECONDS.convert(t, SECONDS));
        assertEquals(1000L*t,
                     MICROSECONDS.convert(t, MILLISECONDS));
        assertEquals(t,
                     MICROSECONDS.convert(t, MICROSECONDS));
        assertEquals(t,
                     MICROSECONDS.convert(1000L*t, NANOSECONDS));

        assertEquals(1000000000L*t*60*60*24,
                     NANOSECONDS.convert(t, DAYS));
        assertEquals(1000000000L*t*60*60,
                     NANOSECONDS.convert(t, HOURS));
        assertEquals(1000000000L*t*60,
                     NANOSECONDS.convert(t, MINUTES));
        assertEquals(1000000000L*t,
                     NANOSECONDS.convert(t, SECONDS));
        assertEquals(1000000L*t,
                     NANOSECONDS.convert(t, MILLISECONDS));
        assertEquals(1000L*t,
                     NANOSECONDS.convert(t, MICROSECONDS));
        assertEquals(t,
                     NANOSECONDS.convert(t, NANOSECONDS));
    }

    for (TimeUnit x : TimeUnit.values()) {
        long[] zs = {
            0, 1, -1,
            Integer.MAX_VALUE, Integer.MIN_VALUE,
            Long.MAX_VALUE, Long.MIN_VALUE,
        };
        for (long z : zs) assertEquals(z, x.convert(z, x));
    }
}
 
Example 19
Project: openjdk-jdk10   File: TimeUnitTest.java   View Source Code Vote up 4 votes
/**
 * a deserialized/reserialized unit is the same instance
 */
public void testSerialization() throws Exception {
    for (TimeUnit x : TimeUnit.values())
        assertSame(x, serialClone(x));
}