/** * Returns the value of the promise or <tt>null</tt> if not completed yet. * * @return the value of the promised computation. This method will not block, so <tt>null</tt> is returned if * the computation has not finished (or failed) yet. */ public V get() { return value != null ? value.get() : null; }
/** * Invokes the given supplier and returns the cached result for every other call. * * @return the cached computation result of the given supplier. */ @Override public T get() { if (value == null) { value = new ValueHolder<>(supplier.get()); } return value.get(); } }
/** * Determines if the given entity has validation warnings. * * @param entity the entity to check * @return <tt>true</tt> if there are validation warnings, <tt>false</tt> otherwise */ public boolean hasValidationWarnings(Object entity) { for (BiConsumer<Object, Consumer<String>> validator : validateHandlers) { ValueHolder<Boolean> hasWarnings = ValueHolder.of(false); validator.accept(entity, warning -> hasWarnings.accept(true)); if (hasWarnings.get()) { return true; } } return false; }
/** * Executes the given command and returns a transcript of stderr and stdout. * * @param command the command to execute * @param ignoreExitCodes if an exit code other than 0 should result in an exception being thrown * @return the transcript of stderr and stdout produced by the executed command * @throws ExecException in case the external program fails */ public static String exec(String command, boolean ignoreExitCodes) throws ExecException { StringBuffer logger = new StringBuffer(); try (Operation op = new Operation(() -> command, Duration.ofMinutes(5))) { Process p = Runtime.getRuntime().exec(command); Semaphore completionSynchronizer = new Semaphore(2); StreamEater errEater = StreamEater.eat(p.getErrorStream(), logger, completionSynchronizer); StreamEater outEater = StreamEater.eat(p.getInputStream(), logger, completionSynchronizer); doExec(ignoreExitCodes, logger, p); // Wait for the stream eaters to complete... completionSynchronizer.acquire(2); if (errEater.exHolder.get() != null) { throw new ExecException(errEater.exHolder.get(), logger.toString()); } if (outEater.exHolder.get() != null) { throw new ExecException(outEater.exHolder.get(), logger.toString()); } return logger.toString(); } catch (Exception e) { throw new ExecException(e, logger.toString()); } }
/** * Executes the given query returning the first matching row. * <p> * If the resulting row contains a {@link Blob} an {@link OutputStream} as to be passed in as parameter * with the name name as the column. The contents of the blob will then be written into the given * output stream (without closing it). * * @return the first matching row for the given query or <tt>null</tt> if no matching row was found * @throws SQLException in case of a database error */ @Nullable public Row queryFirst() throws SQLException { ValueHolder<Row> result = ValueHolder.of(null); iterateAll(result, Limit.singleItem()); return result.get(); }
protected void deleteByIteration(boolean force) throws Exception { ValueHolder<Exception> error = ValueHolder.of(null); iterate(e -> { try { if (force) { indexAccess.forceDelete(e); } else { indexAccess.delete(e); } } catch (Exception ex) { error.set(ex); } return true; }); if (error.get() != null) { throw error.get(); } } }
try { Sirius.LOG.INFO("Loading config: %s", value.group()); result.set(result.get().withFallback(ConfigFactory.load(getLoader(), value.group()))); } catch (Exception e) { Exceptions.ignore(e); Sirius.LOG.INFO("using application.conf from classpath..."); try { result.set(ConfigFactory.load(loader, "application.conf").withFallback(result.get())); } catch (Exception e) { Exceptions.ignore(e); return result.get();
/** * Returns the first item matched by the query. * * @return the first item matched by the query or <tt>null</tt> if there are no matches. */ public E queryFirst() { ValueHolder<E> result = ValueHolder.of(null); limit(1); iterate(r -> { result.set(r); return false; }); return result.get(); }