/** * 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; }
/** * 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(); } }
final ValueHolder<Config> result = new ValueHolder<>(ConfigFactory.empty()); 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();
/** * 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(); }
/** * 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; }
@Override public void run() { try (InputStreamReader isr = new InputStreamReader(stream); BufferedReader br = new BufferedReader(isr)) { Thread.currentThread() .setName(StreamEater.class.getSimpleName() + "-" + Thread.currentThread().getId()); String line = br.readLine(); while (line != null) { logger.append(line); logger.append("\n"); line = br.readLine(); } } catch (IOException e) { logger.append(NLS.toUserString(e)); exHolder.set(e); } finally { this.completionSynchronizer.release(); } }
/** * Creates a new <tt>ValueHolder</tt> with the given initial value. * <p> * This method can be used instead of a constructor, so that the type parameters don't need to be re-typed. * * @param initialValue sets the value of the value holder * @param <T> the type of the value being held by this <tt>ValueHolder</tt> * @return a new ValueHolder initialized with the given value. */ public static <T> ValueHolder<T> of(T initialValue) { return new ValueHolder<>(initialValue); }
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(); } } }
/** * 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()); } }
/** * Marks the promise as successful and completed with the given value. * * @param value the value to be used as promised result. * @return <tt>this</tt> for fluent method chaining */ public Promise<V> success(@Nullable final V value) { this.value = new ValueHolder<>(value); for (final CompletionHandler<V> handler : handlers) { completeHandler(value, handler); } return this; }