Java Code Examples for java.time.Duration#ofHours()

The following examples show how to use java.time.Duration#ofHours() . 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: ADStateManagerTests.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    modelManager = mock(ModelManager.class);
    when(modelManager.getPartitionedForestSizes(any(AnomalyDetector.class))).thenReturn(new SimpleImmutableEntry<>(2, 20));
    client = mock(Client.class);
    clientUtil = mock(ClientUtil.class);
    settings = Settings
        .builder()
        .put("opendistro.anomaly_detection.max_retry_for_unresponsive_node", 3)
        .put("opendistro.anomaly_detection.ad_mute_minutes", TimeValue.timeValueMinutes(10))
        .build();
    clock = mock(Clock.class);
    duration = Duration.ofHours(1);
    context = TestHelpers.createThreadPool();
    throttler = new Throttler(clock);

    stateManager = new ADStateManager(client, xContentRegistry(), modelManager, settings, clientUtil, clock, duration);

}
 
Example 2
Source File: DefaultDeadlineInterceptorTest.java    From grpc-java-contrib with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void interceptorShouldNotModifyExplicitDeadline() {
    AtomicBoolean called = new AtomicBoolean(false);

    DefaultDeadlineInterceptor interceptor = new DefaultDeadlineInterceptor(Duration.ofHours(1));

    interceptor.interceptCall(null, CallOptions.DEFAULT.withDeadlineAfter(10, TimeUnit.HOURS), new Channel() {
        @Override
        public <RequestT, ResponseT> ClientCall<RequestT, ResponseT> newCall(MethodDescriptor<RequestT, ResponseT> methodDescriptor, CallOptions callOptions) {
            called.set(true);
            assertThat(callOptions.getDeadline().timeRemaining(TimeUnit.HOURS)).isEqualTo(9);
            return null;
        }

        @Override
        public String authority() {
            return null;
        }
    });

    assertThat(called.get()).isTrue();
}
 
Example 3
Source File: Policy.java    From spring-vault with Apache License 2.0 6 votes vote down vote up
@Override
public Duration convert(String value) {

	try {
		return Duration.ofSeconds(Long.parseLong(value));
	}
	catch (NumberFormatException e) {

		Matcher matcher = SECONDS.matcher(value);
		if (matcher.matches()) {
			return Duration.ofSeconds(Long.parseLong(matcher.group(1)));
		}

		matcher = MINUTES.matcher(value);
		if (matcher.matches()) {
			return Duration.ofMinutes(Long.parseLong(matcher.group(1)));
		}

		matcher = HOURS.matcher(value);
		if (matcher.matches()) {
			return Duration.ofHours(Long.parseLong(matcher.group(1)));
		}

		throw new IllegalArgumentException("Unsupported duration value: " + value);
	}
}
 
Example 4
Source File: TCKYear.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@DataProvider(name="minusInvalidUnit")
Object[][] data_minusInvalidUnit() {
    return new Object[][] {
            {Period.of(0, 1, 0)},
            {Period.of(0, 0, 1)},
            {Period.of(0, 1, 1)},
            {Period.of(1, 1, 1)},
            {Duration.ofDays(1)},
            {Duration.ofHours(1)},
            {Duration.ofMinutes(1)},
            {Duration.ofSeconds(1)},
    };
}
 
Example 5
Source File: TCKYearMonth.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@DataProvider(name="minus_TemporalAmount")
Object[][] data_minus_TemporalAmount() {
    return new Object[][] {
        {YearMonth.of(1, 1), Period.ofYears(1), YearMonth.of(0, 1), null},
        {YearMonth.of(1, 1), Period.ofYears(-12), YearMonth.of(13, 1), null},
        {YearMonth.of(1, 1), Period.ofYears(0), YearMonth.of(1, 1), null},
        {YearMonth.of(999999999, 12), Period.ofYears(0), YearMonth.of(999999999, 12), null},
        {YearMonth.of(-999999999, 1), Period.ofYears(0), YearMonth.of(-999999999, 1), null},
        {YearMonth.of(0, 1), Period.ofYears(999999999), YearMonth.of(-999999999, 1), null},
        {YearMonth.of(0, 12), Period.ofYears(-999999999), YearMonth.of(999999999, 12), null},

        {YearMonth.of(1, 1), Period.ofMonths(1), YearMonth.of(0, 12), null},
        {YearMonth.of(1, 1), Period.ofMonths(-12), YearMonth.of(2, 1), null},
        {YearMonth.of(1, 1), Period.ofMonths(121), YearMonth.of(-10, 12), null},
        {YearMonth.of(1, 1), Period.ofMonths(0), YearMonth.of(1, 1), null},
        {YearMonth.of(999999999, 12), Period.ofMonths(0), YearMonth.of(999999999, 12), null},
        {YearMonth.of(-999999999, 1), Period.ofMonths(0), YearMonth.of(-999999999, 1), null},
        {YearMonth.of(-999999999, 2), Period.ofMonths(1), YearMonth.of(-999999999, 1), null},
        {YearMonth.of(999999999, 11), Period.ofMonths(-1), YearMonth.of(999999999, 12), null},

        {YearMonth.of(1, 1), Period.ofYears(1).withMonths(2), YearMonth.of(-1, 11), null},
        {YearMonth.of(1, 1), Period.ofYears(-12).withMonths(-1), YearMonth.of(13, 2), null},

        {YearMonth.of(1, 1), Period.ofMonths(2).withYears(1), YearMonth.of(-1, 11), null},
        {YearMonth.of(1, 1), Period.ofMonths(-1).withYears(-12), YearMonth.of(13, 2), null},

        {YearMonth.of(1, 1), Period.ofDays(365), null, DateTimeException.class},
        {YearMonth.of(1, 1), Duration.ofDays(365), null, DateTimeException.class},
        {YearMonth.of(1, 1), Duration.ofHours(365*24), null, DateTimeException.class},
        {YearMonth.of(1, 1), Duration.ofMinutes(365*24*60), null, DateTimeException.class},
        {YearMonth.of(1, 1), Duration.ofSeconds(365*24*3600), null, DateTimeException.class},
        {YearMonth.of(1, 1), Duration.ofNanos(365*24*3600*1000000000), null, DateTimeException.class},
    };
}
 
Example 6
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * Those kinds of types are declared differently in the GraphSON type deserializer, check that all are handled
 * properly.
 */
@Test
public void shouldKeepTypesWhenDeserializingSerializedTinkerGraph() throws IOException {
    final TinkerGraph tg = TinkerGraph.open();

    final Vertex v = tg.addVertex("vertexTest");
    final UUID uuidProp = UUID.randomUUID();
    final Duration durationProp = Duration.ofHours(3);
    final Long longProp = 2L;
    final ByteBuffer byteBufferProp = ByteBuffer.wrap("testbb".getBytes());
    final InetAddress inetAddressProp = InetAddress.getByName("10.10.10.10");

    // One Java util type natively supported by Jackson
    v.property("uuid", uuidProp);
    // One custom time type added by the GraphSON module
    v.property("duration", durationProp);
    // One Java native type not handled by JSON natively
    v.property("long", longProp);
    // One Java util type added by GraphSON
    v.property("bytebuffer", byteBufferProp);
    v.property("inetaddress", inetAddressProp);


    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);
    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeGraph(out, tg);
        final String json = out.toString();
        final TinkerGraph read = TinkerGraph.open();
        reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
        final Vertex vRead = read.traversal().V().hasLabel("vertexTest").next();
        assertEquals(vRead.property("uuid").value(), uuidProp);
        assertEquals(vRead.property("duration").value(), durationProp);
        assertEquals(vRead.property("long").value(), longProp);
        assertEquals(vRead.property("bytebuffer").value(), byteBufferProp);
        assertEquals(vRead.property("inetaddress").value(), inetAddressProp);
    }
}
 
Example 7
Source File: TCKYear.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@DataProvider
public static Object[][] data_plusInvalidUnit() {
    return new Object[][] {
            {Period.of(0, 1, 0)},
            {Period.of(0, 0, 1)},
            {Period.of(0, 1, 1)},
            {Period.of(1, 1, 1)},
            {Duration.ofDays(1)},
            {Duration.ofHours(1)},
            {Duration.ofMinutes(1)},
            {Duration.ofSeconds(1)},
    };
}
 
Example 8
Source File: DelayedDailyPathComputerTest.java    From garmadon with Apache License 2.0 5 votes vote down vote up
@Test
public void dayBefore() {
    DelayedDailyPathComputer computer = new DelayedDailyPathComputer(Duration.ofHours(2));

    Assert.assertEquals("1987-08-12", computer.apply(LocalDateTime.parse("1987-08-13T00:00:00")));
    Assert.assertEquals("1987-08-12", computer.apply(LocalDateTime.parse("1987-08-13T01:59:59")));
}
 
Example 9
Source File: TCKDuration.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = {ArithmeticException.class})
public void plusHours_long_overflowTooSmall() {
    Duration t = Duration.ofHours(-1);
    t.plusHours(Long.MIN_VALUE/3600);
}
 
Example 10
Source File: TCKDuration.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=ArithmeticException.class)
public void factory_hours_tooBig() {
    Duration.ofHours(Long.MAX_VALUE / 3600 + 1);
}
 
Example 11
Source File: TCKDuration.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=ArithmeticException.class)
public void factory_hours_tooSmall() {
    Duration.ofHours(Long.MIN_VALUE / 3600 - 1);
}
 
Example 12
Source File: TCKDuration.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=ArithmeticException.class)
public void factory_hours_tooSmall() {
    Duration.ofHours(Long.MIN_VALUE / 3600 - 1);
}
 
Example 13
Source File: TCKDuration.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider="PlusHours")
public void plusHours_long(long hours, long amount, long expectedHours) {
    Duration t = Duration.ofHours(hours);
    t = t.plusHours(amount);
    assertEquals(t.toHours(), expectedHours);
}
 
Example 14
Source File: TCKDuration.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = {ArithmeticException.class})
public void minusHours_long_overflowTooSmall() {
    Duration t = Duration.ofHours(Long.MIN_VALUE/3600);
    t.minusHours(1);
}
 
Example 15
Source File: Cache.java    From robozonky with Apache License 2.0 4 votes vote down vote up
@Override
public Duration getEvictEvery() {
    return Duration.ofHours(1);
}
 
Example 16
Source File: EpochSecondsDelayParser.java    From riptide with MIT License 4 votes vote down vote up
EpochSecondsDelayParser(final Clock clock) {
    // maximum difference between timezones is 26 hours
    this(clock, Duration.ofHours(-26));
}
 
Example 17
Source File: TCKDuration.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = {ArithmeticException.class})
public void plusHours_long_overflowTooSmall() {
    Duration t = Duration.ofHours(-1);
    t.plusHours(Long.MIN_VALUE/3600);
}
 
Example 18
Source File: TCKDuration.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=ArithmeticException.class)
public void factory_hours_tooSmall() {
    Duration.ofHours(Long.MIN_VALUE / 3600 - 1);
}
 
Example 19
Source File: TCKDuration.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void factory_hours_min() {
    Duration test = Duration.ofHours(Long.MIN_VALUE / 3600);
    assertEquals(test.getSeconds(), (Long.MIN_VALUE / 3600) * 3600);
    assertEquals(test.getNano(), 0);
}
 
Example 20
Source File: TCKDuration.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=ArithmeticException.class)
public void factory_hours_tooSmall() {
    Duration.ofHours(Long.MIN_VALUE / 3600 - 1);
}