List of usage examples for com.google.common.base Function toString
public String toString()
From source file:org.caleydo.core.util.function.DoubleFunctions.java
public static IDoubleFunction wrap(final Function<Double, Double> f) { return new IDoubleFunction() { @Override//from www . j a v a 2 s .c o m public final Double apply(Double in) { return f.apply(in); } @Override public double apply(double in) { return f.apply(in); } @Override public String toString() { return f.toString(); } }; }
From source file:jhc.redsniff.webdriver.LoggingWebDriverWait.java
private <V, T> Function<T, V> loggingFunction(final Function<T, V> wrapped) { return new Function<T, V>() { int calledNum = 0; public V apply(T input) { long startTimeMillis = System.currentTimeMillis(); V apply = wrapped.apply(input); if (calledNum > 0) log("Called " + wrapped.toString() + " again for " + calledNum + "th time - took" + ((System.currentTimeMillis() - startTimeMillis) / 1000d) + "::" + (apply == null ? "NULL" : apply.toString())); calledNum++;// w w w. jav a2 s .c om return apply; } @Override public String toString() { return wrapped.toString(); } }; }
From source file:org.jclouds.collect.PaginatedSets.java
/** * /* w ww . j av a 2s . co m*/ * @param initial * the initial set of data * @param markerToNext * produces the next set based on the marker * * @return iterable of users fitting the criteria */ public static <T> Iterable<T> lazyContinue(final PaginatedSet<T> initial, final Function<String, PaginatedSet<T>> markerToNext) { if (initial.getNextMarker() == null) return initial; return new Iterable<T>() { @Override public Iterator<T> iterator() { return new AbstractIterator<T>() { private PaginatedSet<T> response = initial; private Iterator<T> iterator = response.iterator(); /** * {@inheritDoc} */ @Override protected T computeNext() { while (true) { if (iterator == null) { response = markerToNext.apply(response.getNextMarker()); iterator = response.iterator(); } if (iterator.hasNext()) { return iterator.next(); } if (response.getNextMarker() == null) { return endOfData(); } iterator = null; } } }; } @Override public String toString() { return "lazyContinue(" + markerToNext.toString() + ")"; } }; }
From source file:com.turbospaces.core.JVMUtil.java
/** * repeats the task action totalIterationsCount times concurrently(you provide how many threads and callback * function) - this is general purpose utility.</p> * //from w ww . j av a 2s . c o m * <strong>NOTE :</strong> this method returns all caught exceptions and you should at least use * <code>JUnit.Asser.assertTrue(repeateConcurrenlty.size(), 0)</code> or something similar to check that there are * no execution errors. * * @param threads * number of concurrent threads * @param totalIterationsCount * how many times to repeat task execution concurrently * @param task * the action which needs to be performed * @return all errors from thread's execution */ public static <T> List<Throwable> repeatConcurrently(final int threads, final int totalIterationsCount, final Function<Integer, Object> task) { final AtomicInteger atomicLong = new AtomicInteger(totalIterationsCount); final CountDownLatch countDownLatch = new CountDownLatch(threads); final LinkedList<Throwable> errors = Lists.newLinkedList(); for (int j = 0; j < threads; j++) { Thread thread = new Thread(new Runnable() { @Override public void run() { try { int l; while ((l = atomicLong.decrementAndGet()) >= 0) try { task.apply(l); } catch (Throwable e) { Log.error(e.getMessage(), e); errors.add(e); Throwables.propagate(e); } } finally { countDownLatch.countDown(); } } }); thread.setName(String.format("RepeateConcurrentlyThread-%s:{%s}", j, task.toString())); thread.start(); } Uninterruptibles.awaitUninterruptibly(countDownLatch); return errors; }
From source file:org.jclouds.virtualbox.util.MachineUtils.java
/** * Locks the machine and executes the given function using the machine matching the given id. * Since the machine is locked it is possible to perform some modifications to the IMachine. * <p/>//from w w w .ja va2 s .c o m * Unlocks the machine before returning. * * @param machineId * the id of the machine * @param function * the function to execute * @return the result from applying the function to the machine. */ public <T> T writeLockMachineAndApply(final String machineId, final Function<IMachine, T> function) { return lockSessionOnMachineAndApply(machineId, LockType.Write, new Function<ISession, T>() { @Override public T apply(ISession session) { return function.apply(session.getMachine()); } @Override public String toString() { return function.toString(); } }); }
From source file:org.jclouds.virtualbox.util.MachineUtils.java
/** * Locks the machine and executes the given function using the machine matching the given id. The * machine is read locked, which means that settings can be read safely (but not changed) by * function.//from ww w . j a v a 2 s . c o m * <p/> * Unlocks the machine before returning. * * @param machineId * the id of the machine * @param function * the function to execute * @return the result from applying the function to the machine. */ public <T> T sharedLockMachineAndApply(final String machineId, final Function<IMachine, T> function) { return lockSessionOnMachineAndApply(machineId, LockType.Shared, new Function<ISession, T>() { @Override public T apply(ISession session) { return function.apply(session.getMachine()); } @Override public String toString() { return function.toString(); } }); }
From source file:org.jclouds.virtualbox.util.MachineUtils.java
/** * @param machineId/*from w ww . j a v a 2 s . com*/ * @param function * @return */ public <T> T applyForMachine(final String machineId, final Function<IMachine, T> function) { final IMachine immutableMachine = manager.get().getVBox().findMachine(machineId); return new Function<IMachine, T>() { @Override public T apply(IMachine machine) { return function.apply(machine); } @Override public String toString() { return function.toString(); } }.apply(immutableMachine); }
From source file:com.isotrol.impe3.pms.core.support.ObjectsLoader.java
/** * Constructor.//from ww w .j a v a 2 s. c o m * @param name Object name for debugging. * @param computer Computer function. */ private ObjectsLoader(String name, final Function<UUID, T> computer) { final CacheLoader<Key, T> loader = new CacheLoader<Key, T>() { @Override public T load(Key key) throws Exception { final Stopwatch w = Stopwatch.createStarted(); try { return computer.apply(key.id); } finally { long t = w.elapsed(TimeUnit.MILLISECONDS); if (t > 500) { System.out .println(String.format("State Loader [%s] took %d ms", ObjectsLoader.this.name, t)); } } } }; if (StringUtils.hasText(name)) { this.name = name; } else { this.name = computer.toString(); } this.cache = CacheBuilder.newBuilder().maximumSize(64L).softValues() .expireAfterAccess(2 * 3600L, TimeUnit.SECONDS).build(loader); }
From source file:com.eucalyptus.component.Topology.java
private static Callable<ServiceConfiguration> callable(final ServiceConfiguration config, final Function<ServiceConfiguration, ServiceConfiguration> function) { final Long queueStart = System.currentTimeMillis(); final Callable<ServiceConfiguration> call = new Callable<ServiceConfiguration>() { @Override/*from w w w .java 2 s .c om*/ public ServiceConfiguration call() throws Exception { if (Bootstrap.isShuttingDown()) { throw Exceptions.toUndeclared("System is shutting down."); } else { final Long serviceStart = System.currentTimeMillis(); Logs.extreme().debug(EventRecord.here(Topology.class, EventType.DEQUEUE, function.toString(), config.getFullName().toString(), Long.toString(serviceStart - queueStart), "ms")); try { final ServiceConfiguration result = function.apply(config); Logs.extreme() .debug(EventRecord.here(Topology.class, EventType.QUEUE, function.toString(), config.getFullName().toString(), Long.toString(System.currentTimeMillis() - serviceStart), "ms")); return result; } catch (final Exception ex) { final Throwable t = Exceptions.unwrapCause(ex); Logs.extreme().error( config.getFullName() + " failed to transition because of:\n" + t.getMessage()); Logs.extreme().error(t, t); throw ex; } } } @Override public String toString() { return Topology.class.getSimpleName() + ":" + config.getFullName() + " " + function.toString(); } }; return call; }
From source file:org.openqa.selenium.support.ui.FluentWait.java
/** * Repeatedly applies this instance's input value to the given function until one of the following * occurs://from w w w. j a v a 2s. com * <ol> * <li>the function returns neither null nor false,</li> * <li>the function throws an unignored exception,</li> * <li>the timeout expires, * <li> * <li>the current thread is interrupted</li> * </ol> * * @param isTrue the parameter to pass to the {@link ExpectedCondition} * @param <V> The function's expected return type. * @return The functions' return value if the function returned something different * from null or false before the timeout expired. * @throws TimeoutException If the timeout expires. */ public <V> V until(Function<? super T, V> isTrue) { long end = clock.laterBy(timeout.in(MILLISECONDS)); Throwable lastException = null; while (true) { try { V value = isTrue.apply(input); if (value != null && Boolean.class.equals(value.getClass())) { if (Boolean.TRUE.equals(value)) { return value; } } else if (value != null) { return value; } } catch (Throwable e) { lastException = propagateIfNotIngored(e); } // Check the timeout after evaluating the function to ensure conditions // with a zero timeout can succeed. if (!clock.isNowBefore(end)) { String message = messageSupplier != null ? messageSupplier.get() : null; String toAppend = message == null ? " waiting for " + isTrue.toString() : ": " + message; String timeoutMessage = String.format("Timed out after %d seconds%s", timeout.in(SECONDS), toAppend); throw timeoutException(timeoutMessage, lastException); } try { sleeper.sleep(interval); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new WebDriverException(e); } } }