Example usage for java.util.function Supplier Supplier

List of usage examples for java.util.function Supplier Supplier

Introduction

In this page you can find the example usage for java.util.function Supplier Supplier.

Prototype

Supplier

Source Link

Usage

From source file:edu.cudenver.bios.matrix.MatrixUtilities.java

/**
 * Given a label and a matrix, return a supplier of a
 * log message for that label and matrix.
 *
 * @param label The label./*from  ww  w.j a  va2s  . c om*/
 * @param rm    The matrix.
 *
 * @return The supplier.
 */
public static Supplier<Object> logMessageSupplier(final String label, final RealMatrix rm) {
    return new Supplier<Object>() {
        @Override
        public Object get() {
            return logMessage(label, rm);
        }
    };
}

From source file:com.heliosapm.streams.collector.ds.pool.PooledObjectFactoryBuilder.java

/**
 * Provides the supplier that will create pooled objects
 * @return the supplier// ww w .ja va2s .  c  o m
 */
default public Supplier<T> supplier() {
    return new Supplier<T>() {
        @Override
        public T get() {
            return create();
        }
    };
}

From source file:io.termd.core.telnet.TelnetHandlerTest.java

@Test
public void testRejectNAWS() throws Exception {
    final AtomicReference<Boolean> serverValue = new AtomicReference<>();
    WindowSizeOptionHandler optionHandler = new WindowSizeOptionHandler(20, 10, false, false, false, false);
    testOptionValue(new Supplier<TelnetHandler>() {
        @Override//  w ww  . j  a v  a2s  .co m
        public TelnetHandler get() {
            return new TelnetHandler() {
                @Override
                protected void onOpen(TelnetConnection conn) {
                    conn.writeDoOption(Option.NAWS);
                }

                @Override
                protected void onNAWS(boolean naws) {
                    serverValue.set(naws);
                    testComplete();
                }

                @Override
                protected void onSize(int width, int height) {
                    super.onSize(width, height);
                }
            };
        }
    }, optionHandler);
    assertEquals(false, serverValue.get());
    assertEquals(false, optionHandler.getAcceptLocal());
}

From source file:com.qwazr.webapps.transaction.ControllerManager.java

private void handleJavaClass(WebappTransaction transaction, String className) throws IOException,
        InterruptedException, ScriptException, ReflectiveOperationException, ServletException {
    final Class<? extends HttpServlet> servletClass = ClassLoaderUtils.findClass(ClassLoaderManager.classLoader,
            className);//  ww w  .j  a  v a  2s . co m
    Objects.requireNonNull(servletClass, "Class not found: " + className);
    final ServletInfo servletInfo = new ServletInfo(className, servletClass);
    servletInfo.getInstanceFactory().createInstance();
    HttpServlet servlet = servletMap.getOrCreate(servletClass, new Supplier() {
        @Override
        public HttpServlet get() {
            try {
                HttpServlet servlet = servletClass.newInstance();
                WebServlet webServlet = AnnotationsUtils.getFirstAnnotation(servletClass, WebServlet.class);
                servlet.init(new ServletConfigImpl(servletInfo, transaction.getRequest().getServletContext()));
                LibraryManager.inject(servlet);
                return servlet;
            } catch (InstantiationException | IllegalAccessException | ServletException e) {
                throw new RuntimeException(e);
            }
        }
    });
    servlet.service(transaction.getRequest(), transaction.getResponse());
}

From source file:com.epam.ta.reportportal.core.launch.impl.FinishLaunchHandler.java

private void validate(final String launchId, Launch launch, FinishExecutionRQ finishExecutionRQ) {
    expect(launch, notNull()).verify(LAUNCH_NOT_FOUND, launchId);

    expect(launch, not(Preconditions.LAUNCH_FINISHED)).verify(FINISH_LAUNCH_NOT_ALLOWED, formattedSupplier(
            "Launch '{}' already finished with status '{}'", launch.getId(), launch.getStatus()));

    expect(finishExecutionRQ, Preconditions.finishSameTimeOrLater(launch.getStartTime())).verify(
            FINISH_TIME_EARLIER_THAN_START_TIME, finishExecutionRQ.getEndTime(), launch.getStartTime(),
            launchId);// www. j  av  a 2  s .c  om

    final List<TestItem> items = testItemRepository.findByLaunch(launch);
    expect(items, not(Preconditions.HAS_IN_PROGRESS_ITEMS)).verify(FINISH_LAUNCH_NOT_ALLOWED,
            new Supplier<String>() {
                @Override
                public String get() {
                    String[] values = { launchId,
                            DbUtils.toIds(getInProgressItems(items)).stream().collect(Collectors.joining(",")),
                            IN_PROGRESS.name() };
                    return MessageFormatter.arrayFormat("Launch '{}' has items '[{}]' with '{}' status", values)
                            .getMessage();
                }

                @Override
                public String toString() {
                    return get();
                }
            });
}

From source file:io.termd.core.telnet.TelnetHandlerTest.java

@Test
public void testSendBinary() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    server.start(new Supplier<TelnetHandler>() {
        @Override/* w  w w  .ja v  a  2s.  c o m*/
        public TelnetHandler get() {
            return new TelnetHandler() {
                private TelnetConnection conn;

                @Override
                protected void onOpen(TelnetConnection conn) {
                    this.conn = conn;
                    conn.writeWillOption(Option.BINARY);
                }

                @Override
                protected void onSendBinary(boolean binary) {
                    if (binary) {
                        conn.write(new byte[] { 'h', 'e', 'l', 'l', 'o', -1 });
                        latch.countDown();
                    } else {
                        fail("Was not expecting a don't for binary option");
                    }
                }

                @Override
                protected void onReceiveBinary(boolean binary) {
                    if (binary) {
                        fail("Was not expecting a will for binary option");
                    }
                }
            };
        }
    });
    client.setOptionHandler(new SimpleOptionHandler(0, false, false, false, true));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    client.client.registerSpyStream(baos);
    client.connect("localhost", 4000);
    latch.await();
    Reader reader = new InputStreamReader(client.client.getInputStream());
    int expectedLen = 5;
    char[] hello = new char[expectedLen];
    int num = 0;
    while (num < expectedLen) {
        int read = reader.read(hello, num, expectedLen - num);
        if (read == -1) {
            fail("Unexpected");
        }
        num += read;
    }
    assertEquals(5, num);
    assertEquals("hello", new String(hello));
    long now = System.currentTimeMillis();
    byte[] data;
    while ((data = baos.toByteArray()).length < 10) {
        assertTrue(System.currentTimeMillis() - now < 10000);
        Thread.sleep(5);
    }
    assertEquals((byte) 'h', data[3]);
    assertEquals((byte) 'e', data[4]);
    assertEquals((byte) 'l', data[5]);
    assertEquals((byte) 'l', data[6]);
    assertEquals((byte) 'o', data[7]);
    assertEquals((byte) -1, data[8]);
    assertEquals((byte) -1, data[9]);
}

From source file:com.vmware.photon.controller.common.dcp.helpers.dcp.MultiHostEnvironment.java

private void waitForHostReady(final H host) throws Throwable {
    ServiceHostUtils.waitForState(new Supplier<H>() {
        @Override//from ww w .ja v  a2 s .co  m
        public H get() {
            return host;
        }
    }, new Predicate<H>() {
        @Override
        public boolean test(H esxCloudDcpServiceHost) {
            boolean isReady = esxCloudDcpServiceHost.isReady();
            return isReady;
        }
    }, getEnvironmentCleanup());
}

From source file:com.thoughtworks.go.server.dao.PipelineSqlMapDao.java

public Pipeline pipelineWithMaterialsAndModsByBuildId(long buildId) {
    String cacheKey = this.cacheKeyGenerator.generate("getPipelineByBuildId", buildId);
    return pipelineByBuildIdCache.get(cacheKey, new Supplier<Pipeline>() {
        @Override//from w  w w. j a v a 2 s .  c  o m
        public Pipeline get() {
            Pipeline pipeline = (Pipeline) getSqlMapClientTemplate().queryForObject("getPipelineByBuildId",
                    buildId);
            if (pipeline == null) {
                throw new DataRetrievalFailureException(
                        "Could not load pipeline from build with id " + buildId);
            }
            return loadMaterialRevisions(pipeline);
        }
    });
}

From source file:com.vmware.photon.controller.common.dcp.ServiceHostUtils.java

/**
 * Wait until service state satisfies the given predicate.
 */// w  ww  .ja v  a 2 s .  c  o  m
public static <T> T waitForServiceState(final Class<T> type, final String serviceUri, Predicate<T> predicate,
        final ServiceHost host, long waitIterationSleep, long waitIterationCount, Runnable cleanup)
        throws Throwable {
    return waitForState(new Supplier<T>() {
        @Override
        public T get() {
            try {
                return getServiceState(host, type, serviceUri, "test-host");
            } catch (Throwable t) {
                throw new RuntimeException("Failed to get service state", t);
            }
        }
    }, predicate, waitIterationSleep, waitIterationCount, cleanup);
}

From source file:com.vmware.photon.controller.common.xenon.ServiceHostUtils.java

/**
 * Wait until service state satisfies the given predicate.
 *///from   w  w  w  .  j  a v a2s.c  om
public static <T> T waitForServiceState(final Class<T> type, final String serviceUri, Predicate<T> predicate,
        final ServiceHost host, long waitIterationSleepMillis, long waitIterationCount, Runnable cleanup)
        throws Throwable {

    String timeoutMessage = String.format("Timeout waiting for state transition, serviceUri=[%s]", serviceUri);
    return waitForState(new Supplier<T>() {
        @Override
        public T get() {
            try {
                return getServiceState(host, type, serviceUri, "test-host");
            } catch (Throwable t) {
                throw new RuntimeException("Failed to get service state", t);
            }
        }
    }, predicate, waitIterationSleepMillis, waitIterationCount, cleanup, timeoutMessage);
}