Java Code Examples for java.util.concurrent.TimeUnit#values()
The following examples show how to use
java.util.concurrent.TimeUnit#values() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TimeSpanUtils.java From beihu-boot with Apache License 2.0 | 6 votes |
public static void main(String[] args) { String[] strings = new String[]{ "10s", "1h10s", "0.2h", "200h", "20d20h", "700000s", "0.05d" }; for (String s : strings) { for (TimeUnit unit : TimeUnit.values()) { long spans = TimeSpanUtils.parse(s, unit); System.out.printf("[%s,%s]-> parse:%d\tprecision:%s\n", s, unit.name(), spans, TimeSpanUtils.precisionParse(s, unit)); System.out.printf("[%d %s] -> %s\n", spans, unit.name(), pretty(spans, unit)); } } }
Example 2
Source File: TimeUnitTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 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 3
Source File: TimeUnitTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 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
Source File: TimeUnitTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 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
Source File: Basic.java From openjdk-8 with GNU General Public License v2.0 | 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 6
Source File: TimeUtil.java From UHC with MIT License | 5 votes |
public static String secondsToString(long toConvert) { long seconds = toConvert; final TimeUnit[] units = TimeUnit.values(); final StringBuilder builder = new StringBuilder(); boolean negative = false; if (seconds < 0) { negative = true; seconds *= -1; } for (int i = TimeUnit.DAYS.ordinal(); i >= TimeUnit.SECONDS.ordinal(); i--) { final TimeUnit unit = units[i]; final long count = unit.convert(seconds, TimeUnit.SECONDS); if (count > 0) { builder.append(FORMATTER.format(count)) .append(unit.name().toLowerCase().charAt(0)) .append(" "); seconds -= unit.toSeconds(count); } } if (builder.length() == 0) { return "00s"; } builder.setLength(builder.length() - 1); String built = builder.toString(); if (negative) { built = "-" + built; } return built; }
Example 7
Source File: UtilsTest.java From incubator-pinot with Apache License 2.0 | 5 votes |
@Test public void testTimeUtils() { // Test all time units for (TimeUnit timeUnit : TimeUnit.values()) { assertEquals(TimeUtils.timeUnitFromString(timeUnit.name()), timeUnit); assertEquals(TimeUtils.timeUnitFromString(timeUnit.name().toLowerCase()), timeUnit); } // Test other time unit string assertEquals(TimeUtils.timeUnitFromString("daysSinceEpoch"), TimeUnit.DAYS); assertEquals(TimeUtils.timeUnitFromString("HOURSSINCEEPOCH"), TimeUnit.HOURS); assertEquals(TimeUtils.timeUnitFromString("MinutesSinceEpoch"), TimeUnit.MINUTES); assertEquals(TimeUtils.timeUnitFromString("SeCoNdSsInCeEpOcH"), TimeUnit.SECONDS); assertEquals(TimeUtils.timeUnitFromString("millissinceepoch"), TimeUnit.MILLISECONDS); assertEquals(TimeUtils.timeUnitFromString("MILLISECONDSSINCEEPOCH"), TimeUnit.MILLISECONDS); assertEquals(TimeUtils.timeUnitFromString("microssinceepoch"), TimeUnit.MICROSECONDS); assertEquals(TimeUtils.timeUnitFromString("MICROSECONDSSINCEEPOCH"), TimeUnit.MICROSECONDS); assertEquals(TimeUtils.timeUnitFromString("nanossinceepoch"), TimeUnit.NANOSECONDS); assertEquals(TimeUtils.timeUnitFromString("NANOSECONDSSINCEEPOCH"), TimeUnit.NANOSECONDS); // Test null and empty time unit string assertNull(TimeUtils.timeUnitFromString(null)); assertNull(TimeUtils.timeUnitFromString("")); // Test invalid time unit string try { TimeUtils.timeUnitFromString("unknown"); fail(); } catch (Exception e) { // Expected } }
Example 8
Source File: GenericConnectionPanelTest.java From Ardulink-2 with Apache License 2.0 | 5 votes |
@Test public void hasSubPanelWithConnectionIndividualComponents() { DummyLinkConfig dlc = new DummyLinkConfig(); JPanel panel = sut.createPanel(LinkManager.getInstance().getConfigurer( uri)); JComboBox comboBox = findFirst(JComboBox.class, componentsOf(panel)) .getOrThrow("No %s found on panel %s", JComboBox.class.getName(), panel); comboBox.setSelectedItem("ardulink://dummy"); assertThat(panel, has(row(0).withLabel("1_aIntValue") .withValue(dlc.getIntValue()))); assertThat(panel, has(row(1).withLabel("2_aBooleanValue").withYesNo() .withValue(dlc.getBooleanValue()))); assertThat(panel, has(row(2).withLabel("3_aStringValue").withValue(""))); Object[] choices1 = dlc.someValuesForChoiceWithoutNull().toArray( new String[0]); assertThat(panel, has(row(3).withLabel("4_aStringValueWithChoices") .withChoice(choices1).withValue(choices1[0]))); Object[] choices2 = dlc.someValuesForChoiceWithNull().toArray( new String[0]); assertThat(panel, has(row(4).withLabel("5_aStringValueWithChoicesIncludingNull") .withChoice(choices2).withValue(null))); Object[] timeUnits = TimeUnit.values(); assertThat(panel, has(row(5).withLabel("6_aEnumValue").withChoice(timeUnits) .withValue(dlc.getEnumValue()))); }
Example 9
Source File: TimeDuration.java From incubator-ratis with Apache License 2.0 | 5 votes |
/** @return this * multiplier; the result unit may be changed in order to reduce rounding/overflow error. */ public TimeDuration multiply(double multiplier) { final double product = duration * multiplier; final long rounded = Math.round(product); if (unit.ordinal() != TimeUnit.values().length - 1) { // check overflow error if (rounded == Long.MAX_VALUE || rounded == Long.MIN_VALUE) { if (Math.abs(multiplier) > 2) { return multiply(2).multiply(multiplier / 2); } else { return to(higherUnit(unit)).multiply(multiplier); } } } if (unit.ordinal() != 0) { // check round off error if (Math.abs(product - rounded) > Math.abs(product) * ERROR_THRESHOLD) { if (isMagnitudeLarge(duration) && Math.abs(multiplier) < 0.5d) { return multiply(0.5).multiply(multiplier * 2); } else { return to(lowerUnit(unit)).multiply(multiplier); } } } final TimeDuration t = valueOf(rounded, unit); LOG.debug("{} * {} = {}", this, multiplier, t); return t; }
Example 10
Source File: LabelTimeComposite.java From pentaho-kettle with Apache License 2.0 | 5 votes |
private String[] getTimeUnits() { ArrayList<String> timeUnits = new ArrayList<String>(); for ( TimeUnit timeUnit : TimeUnit.values() ) { timeUnits.add( timeUnit.toString() ); } return timeUnits.toArray( new String[timeUnits.size()] ); }
Example 11
Source File: Basic.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 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 12
Source File: Duration.java From tomee with Apache License 2.0 | 5 votes |
private static TimeUnit lowest(final Duration a, final Duration b) { if (a.unit == null) { return b.unit; } if (b.unit == null) { return a.unit; } if (a.time == 0) { return b.unit; } if (b.time == 0) { return a.unit; } return TimeUnit.values()[Math.min(a.unit.ordinal(), b.unit.ordinal())]; }
Example 13
Source File: Basic.java From jdk8u_jdk with GNU General Public License v2.0 | 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 14
Source File: Basic.java From hottub with GNU General Public License v2.0 | 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
Source File: CustomTags.java From OneBlog with GNU General Public License v3.0 | 4 votes |
public Object sessionTimeOutUnit(Map params) { return TimeUnit.values(); }
Example 16
Source File: AutoCompleteTest.java From picocli with Apache License 2.0 | 4 votes |
private static List<CharSequence> timeUnitValues() { List<CharSequence> result = new ArrayList<CharSequence>(); for (TimeUnit tu : TimeUnit.values()) { result.add(tu.toString()); } return result; }
Example 17
Source File: NpcViewMod.java From L2jOrg with GNU General Public License v3.0 | 4 votes |
private void sendNpcView(Player activeChar, Npc npc) { final NpcHtmlMessage html = new NpcHtmlMessage(); html.setFile(activeChar, "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 Spawn npcSpawn = npc.getSpawn(); if (isNull(npcSpawn) || (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.getStats().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.getStats().getCriticalHit()); html.replace("%evasion%", npc.getEvasionRate()); html.replace("%accuracy%", npc.getStats().getAccuracy()); html.replace("%speed%", (int) npc.getStats().getMoveSpeed()); html.replace("%attributeatktype%", npc.getStats().getAttackElement().name()); html.replace("%attributeatkvalue%", npc.getStats().getAttackElementValue(npc.getStats().getAttackElement())); html.replace("%attributefire%", npc.getStats().getDefenseElementValue(AttributeType.FIRE)); html.replace("%attributewater%", npc.getStats().getDefenseElementValue(AttributeType.WATER)); html.replace("%attributewind%", npc.getStats().getDefenseElementValue(AttributeType.WIND)); html.replace("%attributeearth%", npc.getStats().getDefenseElementValue(AttributeType.EARTH)); html.replace("%attributedark%", npc.getStats().getDefenseElementValue(AttributeType.DARK)); html.replace("%attributeholy%", npc.getStats().getDefenseElementValue(AttributeType.HOLY)); html.replace("%dropListButtons%", getDropListButtons(npc)); activeChar.sendPacket(html); }
Example 18
Source File: TimeUnitTest.java From j2objc with Apache License 2.0 | 4 votes |
/** * a deserialized serialized unit is the same instance */ public void testSerialization() throws Exception { for (TimeUnit x : TimeUnit.values()) assertSame(x, serialClone(x)); }
Example 19
Source File: ElasticSearchBulkMetaTest.java From pentaho-kettle with Apache License 2.0 | 4 votes |
@Override public TimeUnit getTestObject() { return TimeUnit.values()[new Random().nextInt( TimeUnit.values().length )]; }
Example 20
Source File: FormatUtils.java From nifi with Apache License 2.0 | 3 votes |
/** * Returns the next smallest {@link TimeUnit} (i.e. {@code TimeUnit.DAYS -> TimeUnit.HOURS}). * If the parameter is {@code null} or {@code TimeUnit.NANOSECONDS}, an * {@link IllegalArgumentException} is thrown because there is no valid smaller TimeUnit. * * @param originalUnit the TimeUnit * @return the next smaller TimeUnit */ protected static TimeUnit getSmallerTimeUnit(TimeUnit originalUnit) { if (originalUnit == null || TimeUnit.NANOSECONDS == originalUnit) { throw new IllegalArgumentException("Cannot determine a smaller time unit than '" + originalUnit + "'"); } else { return TimeUnit.values()[originalUnit.ordinal() - 1]; } }