Example usage for com.google.common.base Optional fromNullable

List of usage examples for com.google.common.base Optional fromNullable

Introduction

In this page you can find the example usage for com.google.common.base Optional fromNullable.

Prototype

public static <T> Optional<T> fromNullable(@Nullable T nullableReference) 

Source Link

Document

If nullableReference is non-null, returns an Optional instance containing that reference; otherwise returns Optional#absent .

Usage

From source file:io.github.gammaliu.demo.app.LegacySearchApp.java

public static void main(String[] args) throws Exception {

    int port = Integer.valueOf(Optional.fromNullable(System.getenv("PORT")).or("8080"));
    WebServer server = new JettyWebServer(WEB_INF_LOCATION, WEB_APP_LOCATION, port, "0.0.0.0");

    System.setProperty("restx.mode", System.getProperty("restx.mode", "prod"));
    System.setProperty("restx.app.package", "fr.pilato.demo.legacysearch");

    server.startAndAwait();/*from www .  jav  a 2  s  . c o  m*/
}

From source file:org.bin01.db.verifier.DbVerifier.java

public static void main(String[] args) throws Exception {
    Optional<String> configPath = Optional.fromNullable((args.length > 0) ? args[0] : null);
    VerifierConfig config = loadConfig(VerifierConfig.class, configPath);
    DbVerifier verifier = new DbVerifier(config);
    verifier.run();/* w  w w. ja  v  a  2  s.co m*/

    // HttpClient's threads don't seem to shutdown properly, so force the VM to exit
    System.exit(0);
}

From source file:com.vino.AppServer.java

public static void main(String[] args) throws Exception {
    /*/*  ww  w  . jav  a  2 s.c o  m*/
     * load mode from system property if defined, or default to dev
     * be careful with that setting, if you use this class to launch your server in production, make sure to launch
     * it with -Drestx.mode=prod or change the default here
     */
    String mode = System.getProperty("restx.mode", "dev");
    System.setProperty("restx.mode", mode);
    System.setProperty("restx.app.package", "com.vino");

    int port = Integer.valueOf(Optional.fromNullable(System.getenv("PORT")).or("8080"));
    WebServer server = new JettyWebServer(WEB_INF_LOCATION, WEB_APP_LOCATION, port, "0.0.0.0");

    System.setProperty("restx.server.id", server.getServerId());
    System.setProperty("restx.server.baseURL", server.baseUrl());

    server.startAndAwait();
}

From source file:com.github.jeluard.attachant.Main.java

public static void main(final String[] args) throws Exception {
    Main.ensureArguments(args, 2);//from w  w  w  . ja  v  a2 s .c  o  m
    final String id = args[0];
    final String command = args[1];
    if ("load".equals(command)) {
        final int minimumArgumentCount = 3;
        Main.ensureArguments(args, minimumArgumentCount);

        final String agentJarPath = args[2];
        Agents.load(agentJarPath, id, Optional.fromNullable(Main.extractOptions(args, minimumArgumentCount)));
    } else if ("load-self".equals(command)) {
        final int minimumArgumentCount = 2;
        Main.ensureArguments(args, minimumArgumentCount);

        Agents.loadSelf(Agents.class, id,
                Optional.fromNullable(Main.extractOptions(args, minimumArgumentCount)));
    } else if ("load-remote-management".equals(command)) {
        final int minimumArgumentCount = 5;
        Main.ensureArguments(args, minimumArgumentCount);

        final int port = Integer.valueOf(args[2]);
        final boolean authenticate = Boolean.valueOf(args[3]);
        final boolean ssl = Boolean.valueOf(args[4]);
        Agents.loadRemoteManagement(id, port, authenticate, ssl,
                Optional.fromNullable(Main.extractOptions(args, minimumArgumentCount)));
    } else {
        throw new IllegalArgumentException("Unknown command <" + command + ">");
    }
}

From source file:org.opendaylight.protocol.pcep.pcc.mock.Main.java

public static void main(final String[] args)
        throws InterruptedException, ExecutionException, UnknownHostException {
    InetSocketAddress localAddress = new InetSocketAddress(LOCALHOST, DEFAULT_LOCAL_PORT);
    List<InetSocketAddress> remoteAddress = Lists
            .newArrayList(new InetSocketAddress(LOCALHOST, DEFAULT_REMOTE_PORT));
    int pccCount = 1;
    int lsps = 1;
    boolean pcError = false;
    final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    short ka = DEFAULT_KEEP_ALIVE;
    short dt = DEFAULT_DEAD_TIMER;
    String password = null;/*from   w  ww.  ja  va 2s  .c o m*/
    long reconnectTime = -1;
    int redelegationTimeout = 0;
    int stateTimeout = -1;

    getRootLogger(lc).setLevel(ch.qos.logback.classic.Level.INFO);
    int argIdx = 0;
    while (argIdx < args.length) {
        if (args[argIdx].equals("--local-address")) {
            localAddress = InetSocketAddressUtil.getInetSocketAddress(args[++argIdx], DEFAULT_LOCAL_PORT);
        } else if (args[argIdx].equals("--remote-address")) {
            remoteAddress = InetSocketAddressUtil.parseAddresses(args[++argIdx], DEFAULT_REMOTE_PORT);
        } else if (args[argIdx].equals("--pcc")) {
            pccCount = Integer.valueOf(args[++argIdx]);
        } else if (args[argIdx].equals("--lsp")) {
            lsps = Integer.valueOf(args[++argIdx]);
        } else if (args[argIdx].equals("--pcerr")) {
            pcError = true;
        } else if (args[argIdx].equals("--log-level")) {
            getRootLogger(lc).setLevel(Level.toLevel(args[++argIdx], ch.qos.logback.classic.Level.INFO));
        } else if (args[argIdx].equals("--keepalive") || args[argIdx].equals("-ka")) {
            ka = Short.valueOf(args[++argIdx]);
        } else if (args[argIdx].equals("--deadtimer") || args[argIdx].equals("-d")) {
            dt = Short.valueOf(args[++argIdx]);
        } else if (args[argIdx].equals("--password")) {
            password = args[++argIdx];
        } else if (args[argIdx].equals("--reconnect")) {
            reconnectTime = Integer.valueOf(args[++argIdx]).intValue();
        } else if (args[argIdx].equals("--redelegation-timeout")) {
            redelegationTimeout = Integer.valueOf(args[++argIdx]);
        } else if (args[argIdx].equals("--state-timeout")) {
            stateTimeout = Integer.valueOf(args[++argIdx]);
        } else if (args[argIdx].equals("--state-sync-avoidance")) {
            //"--state-sync-avoidance 10, 5, 10
            includeDbv = Boolean.TRUE;
            final Long dbVersionAfterReconnect = Long.valueOf(args[++argIdx]);
            disonnectAfterXSeconds = Integer.valueOf(args[++argIdx]);
            reconnectAfterXSeconds = Integer.valueOf(args[++argIdx]);
            syncOptDBVersion = BigInteger.valueOf(dbVersionAfterReconnect);
        } else if (args[argIdx].equals("--incremental-sync-procedure")) {
            //TODO Check that DBv > Lsp always ??
            includeDbv = Boolean.TRUE;
            incrementalSync = Boolean.TRUE;
            //Version of database to be used after restart
            final Long initialDbVersionAfterReconnect = Long.valueOf(args[++argIdx]);
            disonnectAfterXSeconds = Integer.valueOf(args[++argIdx]);
            reconnectAfterXSeconds = Integer.valueOf(args[++argIdx]);
            syncOptDBVersion = BigInteger.valueOf(initialDbVersionAfterReconnect);
        } else if (args[argIdx].equals("--triggered-initial-sync")) {
            triggeredInitSync = Boolean.TRUE;
        } else if (args[argIdx].equals("--triggered-re-sync")) {
            triggeredResync = Boolean.TRUE;
        } else {
            LOG.warn("WARNING: Unrecognized argument: {}", args[argIdx]);
        }
        argIdx++;
    }

    if (incrementalSync) {
        Preconditions.checkArgument(syncOptDBVersion.intValue() > lsps,
                "Synchronization Database Version which will be used after "
                        + "reconnectes requires to be higher than lsps");
    }

    final Optional<BigInteger> dBVersion = Optional.fromNullable(syncOptDBVersion);
    final PCCsBuilder pccs = new PCCsBuilder(lsps, pcError, pccCount, localAddress, remoteAddress, ka, dt,
            password, reconnectTime, redelegationTimeout, stateTimeout, getCapabilities());
    final TimerHandler timerHandler = new TimerHandler(pccs, dBVersion, disonnectAfterXSeconds,
            reconnectAfterXSeconds);
    pccs.createPCCs(BigInteger.valueOf(lsps), Optional.fromNullable(timerHandler));
    if (!triggeredInitSync) {
        timerHandler.createDisconnectTask();
    }
}

From source file:benchmarkio.controlcenter.LaunchRocket.java

public static void main(final String[] args) throws Exception {
    // create the parser
    final CommandLineParser parser = new BasicParser();

    // parse the command line arguments
    final CommandLine cmd = parser.parse(options, args);

    if (cmd.hasOption("u")) {
        displayHelp();//  www  .j  a  v a  2  s.com
    }

    final String host = cmd.getOptionValue("host");
    final int port = Integer.parseInt(cmd.getOptionValue("port"));
    final BrokerType brokerType = BrokerType.valueOf(cmd.getOptionValue("broker-type"));
    final int numConsumers = Integer.parseInt(cmd.getOptionValue("num-consumers"));
    final int numProducers = Integer.parseInt(cmd.getOptionValue("num-producers"));
    final int totalNumberOfMessages = Integer.parseInt(cmd.getOptionValue("total-number-of-messages"));
    final double msgSizeInKB = Double.parseDouble(cmd.getOptionValue("msg-size-in-kb"));

    // Optional options
    final Optional<String> optionalBenchmarkType = Optional.fromNullable(cmd.getOptionValue("benchmark-type"));
    final Optional<String> optionalDurable = Optional.fromNullable(cmd.getOptionValue("durable"));
    // Kafka Specific
    final Optional<String> optionalZookeeper = Optional.fromNullable(cmd.getOptionValue("zookeeper"));
    Optional<String> optionalKafkaProducerType = Optional
            .fromNullable(cmd.getOptionValue("kafka-producer-type"));

    BenchmarkType benchmarkType;
    if (optionalBenchmarkType.isPresent()) {
        benchmarkType = BenchmarkType.valueOf(optionalBenchmarkType.get());
    } else {
        log.info("Benchmark type was not specified, defaulting to: {}", BenchmarkType.PRODUCER_AND_CONSUMER);

        benchmarkType = BenchmarkType.PRODUCER_AND_CONSUMER;
    }

    boolean durable = false;
    if (optionalDurable.isPresent()) {
        durable = Boolean.valueOf(optionalDurable.get());
    } else {
        log.info("Durable parameter was not specified, defaulting to: FALSE");
    }

    if (brokerType == BrokerType.KAFKA) {
        if (!optionalZookeeper.isPresent()) {
            log.error("zookeeper is missing, it is a required property for KAFKA broker");

            System.exit(0);
        }

        if (!optionalKafkaProducerType.isPresent()) {
            log.info("kafka-producer-type is not specified, defaulting to sync");

            optionalKafkaProducerType = Optional.of("sync");
        } else if (!optionalKafkaProducerType.get().equals("sync")
                && !optionalKafkaProducerType.get().equals("async")) {
            log.warn("kafka-producer-type is not one of the accepted sync | async values, defaulting to sync");

            optionalKafkaProducerType = Optional.of("sync");
        }
    }

    log.info("destination (topic or queue): {}", Consts.DESTINATION_NAME);
    log.info("host: {}", host);
    log.info("port: {}", port);
    log.info("broker-type: {}", brokerType);
    log.info("benchmark-type: {}", benchmarkType);
    log.info("durable: {}", durable);
    log.info("num-consumers: {}", numConsumers);
    log.info("num-producers: {}", numProducers);
    log.info("total-number-of-messages: {}", totalNumberOfMessages);
    log.info("msg-size-in-kb: {}", msgSizeInKB);

    if (brokerType == BrokerType.KAFKA) {
        log.info("zookeeper: {}", optionalZookeeper.get());
        log.info("kafka-producer-type: {}", optionalKafkaProducerType.get());
    }

    LaunchRocket.start(brokerType, benchmarkType, durable, host, port, numConsumers, numProducers,
            totalNumberOfMessages, msgSizeInKB, optionalZookeeper, optionalKafkaProducerType);

    System.exit(0);
}

From source file:ofandroidclipse.util.DirectoryUtil.java

/**
 * Browse.//from w  ww . j  a  va2  s  .c  o m
 *
 * @param shell the shell
 * @return the optional
 */
public static Optional<String> browse(Shell shell) {
    DirectoryDialog dd = new DirectoryDialog(shell);
    String p = dd.open();
    return Optional.fromNullable(p);
}

From source file:com.github.nethad.clustermeister.api.impl.JobFactory.java

public static <T> Job<T> create(Map<String, Object> jobData) {
    return new JobImpl(DEFAULT_JOB_NAME, Optional.fromNullable(jobData));
}

From source file:org.onos.yangtools.yang.data.impl.codec.BinaryStringCodec.java

static TypeDefinitionAwareCodec<?, BinaryTypeDefinition> from(final BinaryTypeDefinition type) {
    return new BinaryStringCodec(Optional.fromNullable(type));
}

From source file:org.onos.yangtools.yang.data.impl.codec.DecimalStringCodec.java

static TypeDefinitionAwareCodec<?, DecimalTypeDefinition> from(final DecimalTypeDefinition normalizedType) {
    return new DecimalStringCodec(Optional.fromNullable(normalizedType));
}