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);
}
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;
}
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();
}
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);
}
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);
}
Aggregations