Search in sources :

Example 31 with TemporalUnit

use of java.time.temporal.TemporalUnit in project titan by thinkaurelius.

the class HadoopConfiguration method get.

@Override
public <O> O get(String key, Class<O> datatype) {
    final String internalKey = getInternalKey(key);
    if (null == config.get(internalKey))
        return null;
    if (datatype.isArray()) {
        Preconditions.checkArgument(datatype.getComponentType() == String.class, "Only string arrays are supported: %s", datatype);
        return (O) config.getStrings(internalKey);
    } else if (Number.class.isAssignableFrom(datatype)) {
        String s = config.get(internalKey);
        return constructFromStringArgument(datatype, s);
    } else if (datatype == String.class) {
        return (O) config.get(internalKey);
    } else if (datatype == Boolean.class) {
        return (O) Boolean.valueOf(config.get(internalKey));
    } else if (datatype.isEnum()) {
        O[] constants = datatype.getEnumConstants();
        Preconditions.checkState(null != constants && 0 < constants.length, "Zero-length or undefined enum");
        String estr = config.get(internalKey);
        for (O c : constants) if (c.toString().equals(estr))
            return c;
        throw new IllegalArgumentException("No match for string \"" + estr + "\" in enum " + datatype);
    } else if (datatype == Object.class) {
        // Object.class must be supported for the sake of AbstractConfiguration's getSubset impl
        return (O) config.get(internalKey);
    } else if (Duration.class.isAssignableFrom(datatype)) {
        // This is a conceptual leak; the config layer should ideally only handle standard library types
        String s = config.get(internalKey);
        String[] comps = s.split("\\s");
        TemporalUnit unit = null;
        if (comps.length == 1) {
            //By default, times are in milli seconds
            unit = ChronoUnit.MILLIS;
        } else if (comps.length == 2) {
            unit = Durations.parse(comps[1]);
        } else {
            throw new IllegalArgumentException("Cannot parse time duration from: " + s);
        }
        return (O) Duration.of(Long.valueOf(comps[0]), unit);
    } else
        throw new IllegalArgumentException("Unsupported data type: " + datatype);
}
Also used : TemporalUnit(java.time.temporal.TemporalUnit)

Example 32 with TemporalUnit

use of java.time.temporal.TemporalUnit in project titan by thinkaurelius.

the class Durations method parse.

public static TemporalUnit parse(String unitName) {
    TemporalUnit unit = unitNames.get(unitName.toLowerCase());
    Preconditions.checkArgument(unit != null, "Unknown unit time: %s", unitName);
    return unit;
}
Also used : TemporalUnit(java.time.temporal.TemporalUnit)

Example 33 with TemporalUnit

use of java.time.temporal.TemporalUnit in project ratpack by ratpack.

the class BlockingHttpClient method request.

public ReceivedResponse request(HttpClient httpClient, URI uri, ExecController execController, Duration timeout, Action<? super RequestSpec> action) throws Throwable {
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<ExecResult<ReceivedResponse>> result = new AtomicReference<>();
    execController.fork().start(e -> httpClient.request(uri, action.prepend(s -> s.readTimeout(Duration.ofHours(1)))).map(response -> {
        TypedData responseBody = response.getBody();
        ByteBuf responseBuffer = responseBody.getBuffer();
        ByteBuf heapResponseBodyBuffer = unreleasableBuffer(responseBuffer.isDirect() ? TestByteBufAllocators.LEAKING_UNPOOLED_HEAP.heapBuffer(responseBuffer.readableBytes()).writeBytes(responseBuffer) : responseBuffer.retain());
        return new DefaultReceivedResponse(response.getStatus(), response.getHeaders(), new ByteBufBackedTypedData(heapResponseBodyBuffer, responseBody.getContentType()));
    }).connect(new Downstream<ReceivedResponse>() {

        @Override
        public void success(ReceivedResponse value) {
            result.set(ExecResult.of(Result.success(value)));
            latch.countDown();
        }

        @Override
        public void error(Throwable throwable) {
            result.set(ExecResult.of(Result.error(throwable)));
            latch.countDown();
        }

        @Override
        public void complete() {
            result.set(ExecResult.complete());
            latch.countDown();
        }
    }));
    try {
        if (!latch.await(timeout.toNanos(), TimeUnit.NANOSECONDS)) {
            TemporalUnit unit = timeout.getUnits().get(0);
            throw new IllegalStateException("Request to " + uri + " took more than " + timeout.get(unit) + " " + unit.toString() + " to complete");
        }
    } catch (InterruptedException e) {
        throw Exceptions.uncheck(e);
    }
    return result.get().getValueOrThrow();
}
Also used : TypedData(ratpack.http.TypedData) ByteBufBackedTypedData(ratpack.http.internal.ByteBufBackedTypedData) TemporalUnit(java.time.temporal.TemporalUnit) DefaultReceivedResponse(ratpack.http.client.internal.DefaultReceivedResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf) ReceivedResponse(ratpack.http.client.ReceivedResponse) DefaultReceivedResponse(ratpack.http.client.internal.DefaultReceivedResponse) ByteBufBackedTypedData(ratpack.http.internal.ByteBufBackedTypedData) Downstream(ratpack.exec.Downstream) ExecResult(ratpack.exec.ExecResult)

Example 34 with TemporalUnit

use of java.time.temporal.TemporalUnit in project jdk8u_jdk by JetBrains.

the class TCKDuration method factory_from_TemporalAmount_DaysNanos.

@Test
public void factory_from_TemporalAmount_DaysNanos() {
    TemporalAmount amount = new TemporalAmount() {

        @Override
        public long get(TemporalUnit unit) {
            if (unit == DAYS) {
                return 23;
            } else {
                return 45;
            }
        }

        @Override
        public List<TemporalUnit> getUnits() {
            List<TemporalUnit> list = new ArrayList<>();
            list.add(DAYS);
            list.add(NANOS);
            return list;
        }

        @Override
        public Temporal addTo(Temporal temporal) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Temporal subtractFrom(Temporal temporal) {
            throw new UnsupportedOperationException();
        }
    };
    Duration t = Duration.from(amount);
    assertEquals(t.getSeconds(), 23 * 86400);
    assertEquals(t.getNano(), 45);
}
Also used : Temporal(java.time.temporal.Temporal) TemporalUnit(java.time.temporal.TemporalUnit) TemporalAmount(java.time.temporal.TemporalAmount) ArrayList(java.util.ArrayList) Duration(java.time.Duration) Test(org.testng.annotations.Test)

Example 35 with TemporalUnit

use of java.time.temporal.TemporalUnit in project jdk8u_jdk by JetBrains.

the class TCKDuration method factory_from_TemporalAmount_Minutes_tooBig.

@Test(expectedExceptions = ArithmeticException.class)
public void factory_from_TemporalAmount_Minutes_tooBig() {
    TemporalAmount amount = new TemporalAmount() {

        @Override
        public long get(TemporalUnit unit) {
            return (Long.MAX_VALUE / 60) + 2;
        }

        @Override
        public List<TemporalUnit> getUnits() {
            return Collections.<TemporalUnit>singletonList(MINUTES);
        }

        @Override
        public Temporal addTo(Temporal temporal) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Temporal subtractFrom(Temporal temporal) {
            throw new UnsupportedOperationException();
        }
    };
    Duration.from(amount);
}
Also used : Temporal(java.time.temporal.Temporal) TemporalUnit(java.time.temporal.TemporalUnit) TemporalAmount(java.time.temporal.TemporalAmount) Test(org.testng.annotations.Test)

Aggregations

TemporalUnit (java.time.temporal.TemporalUnit)60 LocalDate (java.time.LocalDate)18 Test (org.junit.Test)18 Test (org.testng.annotations.Test)17 Temporal (java.time.temporal.Temporal)16 TemporalAmount (java.time.temporal.TemporalAmount)15 Chronology (java.time.chrono.Chronology)12 IsoChronology (java.time.chrono.IsoChronology)12 ArrayList (java.util.ArrayList)12 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)6 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)6 DateTimeException (java.time.DateTimeException)6 HijrahChronology (java.time.chrono.HijrahChronology)6 JapaneseChronology (java.time.chrono.JapaneseChronology)6 MinguoChronology (java.time.chrono.MinguoChronology)6 ThaiBuddhistChronology (java.time.chrono.ThaiBuddhistChronology)6 Matcher (java.util.regex.Matcher)6 Pattern (java.util.regex.Pattern)6 Duration (java.time.Duration)5 ChronoLocalDate (java.time.chrono.ChronoLocalDate)4