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

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

Introduction

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

Prototype

public static <T> Optional<T> of(T reference) 

Source Link

Document

Returns an Optional instance containing the given non-null reference.

Usage

From source file:org.apache.hama.examples.InlinkCount.java

public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {

    if (args.length < 2)
        printUsage();//from  ww  w.j  av a  2 s . c  om

    Optional<Integer> absent = Optional.absent();
    GraphJob inlinkJob = getJob(args[0], args[1],
            args.length >= 3 ? Optional.of(Integer.parseInt(args[3])) : absent);

    long startTime = System.currentTimeMillis();
    if (inlinkJob.waitForCompletion(true)) {
        System.out.println("Job Finished in " + (System.currentTimeMillis() - startTime) / 1000.0 + " seconds");
    }
}

From source file:edu.cmu.cs.lti.ark.fn.identification.training.TrainBatch.java

public static void main(String[] args) throws Exception {
    final FNModelOptions options = new FNModelOptions(args);
    LogManager.getLogManager().reset();
    final FileHandler fh = new FileHandler(options.logOutputFile.get(), true);
    fh.setFormatter(new SimpleFormatter());
    logger.addHandler(fh);//w  ww.  j  av  a  2  s.  co m

    final String restartFile = options.restartFile.get();
    final int numThreads = options.numThreads.present() ? options.numThreads.get()
            : Runtime.getRuntime().availableProcessors();
    final TrainBatch tbm = new TrainBatch(options.alphabetFile.get(), options.eventsFile.get(),
            options.modelFile.get(), options.reg.get(), options.lambda.get(),
            restartFile.equals("null") ? Optional.<String>absent() : Optional.of(restartFile), numThreads,
            options.usePartialCredit.get(),
            options.costMultiple.present() ? (float) options.costMultiple.get() : DEFAULT_COST_MULTIPLE);
    tbm.trainModel();
}

From source file:edu.cmu.cs.lti.ark.fn.identification.latentmodel.TrainBatchModelDerThreaded.java

public static void main(String[] args) throws Exception {
    final FNModelOptions options = new FNModelOptions(args);
    LogManager.getLogManager().reset();
    final FileHandler fh = new FileHandler(options.logOutputFile.get(), true);
    fh.setFormatter(new SimpleFormatter());
    logger.addHandler(fh);// ww w. j a  va2  s. c om

    final String restartFile = options.restartFile.get();
    final int numThreads = options.numThreads.present() ? options.numThreads.get()
            : Runtime.getRuntime().availableProcessors();
    final TrainBatchModelDerThreaded tbm = new TrainBatchModelDerThreaded(options.alphabetFile.get(),
            options.eventsFile.get(), options.modelFile.get(), options.reg.get(), options.lambda.get(),
            restartFile.equals("null") ? Optional.<String>absent() : Optional.of(restartFile), numThreads);
    tbm.trainModel();
}

From source file:org.apache.hama.examples.MindistSearch.java

public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
    if (args.length < 2)
        printUsage();/*w w  w . j a v a 2s. c  om*/

    Optional<Integer> absent = Optional.absent();
    GraphJob job = getJob(args[0], args[1], args.length >= 3 ? Optional.of(Integer.parseInt(args[3])) : absent,
            args.length >= 4 ? Optional.of(Integer.parseInt(args[4])) : absent);

    long startTime = System.currentTimeMillis();
    if (job.waitForCompletion(true)) {
        System.out.println("Job Finished in " + (System.currentTimeMillis() - startTime) / 1000.0 + " seconds");
    }
}

From source file:org.metastatic.treediff.Main.java

public static void main(String... argv) throws Exception {
    Optional<Command> command = Optional.absent();
    LongOpt[] longOpts = new LongOpt[] { new LongOpt("checksum", LongOpt.NO_ARGUMENT, null, CHECKSUM),
            new LongOpt("diff", LongOpt.NO_ARGUMENT, null, DIFF),
            new LongOpt("patch", LongOpt.NO_ARGUMENT, null, PATCH),
            new LongOpt("hash", LongOpt.REQUIRED_ARGUMENT, null, 'h'),
            new LongOpt("hash-length", LongOpt.REQUIRED_ARGUMENT, null, 'l'),
            new LongOpt("sums-file", LongOpt.REQUIRED_ARGUMENT, null, 's'),
            new LongOpt("diff-file", LongOpt.REQUIRED_ARGUMENT, null, 'd'),
            new LongOpt("output", LongOpt.REQUIRED_ARGUMENT, null, 'o'),
            new LongOpt("verbose", LongOpt.NO_ARGUMENT, null, 'v'),
            new LongOpt("strict-hash", LongOpt.NO_ARGUMENT, null, 'H'),
            new LongOpt("size-only", LongOpt.NO_ARGUMENT, null, 'S'),
            new LongOpt("help", LongOpt.NO_ARGUMENT, null, HELP),
            new LongOpt("version", LongOpt.NO_ARGUMENT, null, VERSION) };
    Optional<MessageDigest> hash = Optional.absent();
    Optional<Integer> hashLength = Optional.absent();
    Optional<String> inputFile = Optional.absent();
    Optional<String> outputFile = Optional.absent();
    Optional<DiffCheck> diffCheck = Optional.of(DiffCheck.SizeAndTime);
    Getopt getopt = new Getopt(Main.class.getName(), argv, "h:l:s:d:o:vH", longOpts);
    int ch;// w ww . ja  v a  2s.  c o  m
    while ((ch = getopt.getopt()) != -1) {
        switch (ch) {
        case CHECKSUM:
            if (command.isPresent()) {
                System.err.printf("%s: only specify one command.%n", Main.class.getName());
                System.exit(1);
                return;
            }
            command = Optional.of(Command.Checksum);
            break;

        case DIFF:
            if (command.isPresent()) {
                System.err.printf("%s: only specify one command.%n", Main.class.getName());
                System.exit(1);
                return;
            }
            command = Optional.of(Command.Diff);
            break;

        case PATCH:
            if (command.isPresent()) {
                System.err.printf("%s: only specify one command.%n", Main.class.getName());
                System.exit(1);
                return;
            }
            command = Optional.of(Command.Patch);

        case HELP:
            help();
            System.exit(0);
            break;

        case VERSION:
            version();
            System.exit(0);
            break;

        case 'h':
            checkCommand("--hash", command, EnumSet.of(Command.Checksum));
            try {
                hash = Optional.of(MessageDigest.getInstance(getopt.getOptarg()));
            } catch (NoSuchAlgorithmException nsae) {
                try {
                    hash = Optional.of(MessageDigest.getInstance(getopt.getOptarg(), new JarsyncProvider()));
                } catch (NoSuchAlgorithmException nsae2) {
                    System.err.printf("%s: no such hash: %s%n", Main.class.getName(), getopt.getOptarg());
                    System.exit(1);
                    return;
                }
            }
            break;

        case 'l':
            checkCommand("--hash-length", command, EnumSet.of(Command.Checksum));
            try {
                hashLength = Optional.of(Integer.parseInt(getopt.getOptarg()));
            } catch (NumberFormatException nfe) {
                System.err.printf("%s: --hash-length: invalid number.%n", Main.class.getName());
                System.exit(1);
                return;
            }
            break;

        case 's':
            checkCommand("--sums-file", command, EnumSet.of(Command.Diff));
            inputFile = Optional.of(getopt.getOptarg());
            break;

        case 'd':
            checkCommand("--diff-file", command, EnumSet.of(Command.Patch));
            inputFile = Optional.of(getopt.getOptarg());
            break;

        case 'o':
            checkCommand("--output", command, EnumSet.of(Command.Checksum, Command.Diff));
            outputFile = Optional.of(getopt.getOptarg());
            break;

        case 'v':
            verbosity++;
            break;

        case 'H':
            checkCommand("--strict-hash", command, EnumSet.of(Command.Diff));
            diffCheck = Optional.of(DiffCheck.StrictHash);
            break;

        case 'S':
            checkCommand("--size-only", command, EnumSet.of(Command.Diff));
            diffCheck = Optional.of(DiffCheck.SizeOnly);
            break;

        case '?':
            System.err.printf("Try `%s --help' for more info.%n", Main.class.getName());
            System.exit(1);
            return;
        }
    }

    if (!command.isPresent()) {
        System.err.printf("%s: must supply a command.%n", Main.class.getName());
        System.exit(1);
        return;
    }

    switch (command.get()) {
    case Checksum: {
        if (!hash.isPresent())
            hash = Optional.of(MessageDigest.getInstance("Murmur3", new JarsyncProvider()));
        if (!hashLength.isPresent())
            hashLength = Optional.of(hash.get().getDigestLength());
        else if (hashLength.get() <= 0 || hashLength.get() > hash.get().getDigestLength()) {
            System.err.printf("%s: invalid hash length: %d.%n", Main.class.getName(), hashLength.get());
            System.exit(1);
            return;
        }
        if (!outputFile.isPresent()) {
            System.err.printf("%s: --output argument required.%n", Main.class.getName());
            System.exit(1);
            return;
        }
        if (getopt.getOptind() >= argv.length) {
            System.err.printf("%s: must specify at least one file or directory.%n", Main.class.getName());
            System.exit(1);
            return;
        }
        DataOutputStream output = new DataOutputStream(
                new BufferedOutputStream(new FileOutputStream(outputFile.get())));
        output.write(SUMS_MAGIC);
        String alg = hash.get().getAlgorithm();
        output.writeUTF(alg);
        output.writeInt(hashLength.get());
        checksum(hash.get(), hashLength.get(), output,
                Arrays.asList(argv).subList(getopt.getOptind(), argv.length));
        output.close();
        break;
    }

    case Diff: {
        if (!inputFile.isPresent()) {
            System.err.printf("%s: --diff: option --sums-file required.%n", Main.class.getName());
            System.exit(1);
            return;
        }
        if (!outputFile.isPresent()) {
            System.err.printf("%s: --output argument required.%n", Main.class.getName());
            System.exit(1);
            return;
        }
        DataInputStream input = new DataInputStream(new FileInputStream(inputFile.get()));
        byte[] magic = new byte[8];
        input.readFully(magic);
        if (!Arrays.equals(magic, SUMS_MAGIC)) {
            System.err.printf("%s: %s: invalid file header.%n", Main.class.getName(), inputFile.get());
            System.exit(1);
            return;
        }
        String alg = input.readUTF();
        try {
            hash = Optional.of(MessageDigest.getInstance(alg, new JarsyncProvider()));
        } catch (NoSuchAlgorithmException nsae) {
            hash = Optional.of(MessageDigest.getInstance(alg));
        }
        hashLength = Optional.of(input.readInt());
        if (hashLength.get() <= 0 || hashLength.get() > hash.get().getDigestLength()) {
            System.err.printf("%s: invalid hash length: %d.%n", Main.class.getName(), hashLength.get());
            System.exit(1);
            return;
        }
        DataOutputStream output = new DataOutputStream(
                new BufferedOutputStream(new FileOutputStream(outputFile.get())));
        output.write(DIFF_MAGIC);
        output.writeUTF(alg);
        output.writeInt(hashLength.get());
        diff(hash.get(), hashLength.get(), input, output, diffCheck.or(DiffCheck.SizeAndTime));
        output.close();
        break;
    }

    case Patch:
        throw new Error("not yet implemented");
    }
}

From source file:gobblin.aws.GobblinAWSClusterManager.java

public static void main(String[] args) throws Exception {
    final Options options = buildOptions();
    try {/* ww w  . ja v  a  2s .  c  om*/
        final CommandLine cmd = new DefaultParser().parse(options, args);
        if (!cmd.hasOption(GobblinClusterConfigurationKeys.APPLICATION_NAME_OPTION_NAME)
                || !cmd.hasOption(GobblinAWSConfigurationKeys.APP_WORK_DIR)) {
            printUsage(options);
            System.exit(1);
        }

        Log4jConfigHelper.updateLog4jConfiguration(GobblinAWSClusterManager.class,
                GobblinAWSConfigurationKeys.GOBBLIN_AWS_LOG4J_CONFIGURATION_FILE);

        LOGGER.info(JvmUtils.getJvmInputArguments());

        // Note: Application id is required param for {@link GobblinClusterManager} super class
        // .. but has not meaning in AWS cluster context, so defaulting to a fixed value
        final String applicationId = "1";
        final String appWorkDir = cmd.getOptionValue(GobblinAWSConfigurationKeys.APP_WORK_DIR);

        try (GobblinAWSClusterManager clusterMaster = new GobblinAWSClusterManager(
                cmd.getOptionValue(GobblinClusterConfigurationKeys.APPLICATION_NAME_OPTION_NAME), applicationId,
                ConfigFactory.load(), Optional.of(new Path(appWorkDir)))) {

            clusterMaster.start();
        }
    } catch (ParseException pe) {
        printUsage(options);
        System.exit(1);
    }
}

From source file:org.apache.gobblin.aws.GobblinAWSClusterManager.java

public static void main(String[] args) throws Exception {
    final Options options = buildOptions();
    try {/*from ww  w.jav a2  s . c  om*/
        final CommandLine cmd = new DefaultParser().parse(options, args);
        if (!cmd.hasOption(GobblinClusterConfigurationKeys.APPLICATION_NAME_OPTION_NAME)
                || !cmd.hasOption(GobblinAWSConfigurationKeys.APP_WORK_DIR)) {
            printUsage(options);
            System.exit(1);
        }

        if (System.getProperty("log4j.configuration") == null) {
            Log4jConfigHelper.updateLog4jConfiguration(GobblinAWSClusterManager.class,
                    GobblinAWSConfigurationKeys.GOBBLIN_AWS_LOG4J_CONFIGURATION_FILE);
        }

        LOGGER.info(JvmUtils.getJvmInputArguments());

        // Note: Application id is required param for {@link GobblinClusterManager} super class
        // .. but has not meaning in AWS cluster context, so defaulting to a fixed value
        final String applicationId = "1";
        final String appWorkDir = cmd.getOptionValue(GobblinAWSConfigurationKeys.APP_WORK_DIR);

        try (GobblinAWSClusterManager clusterMaster = new GobblinAWSClusterManager(
                cmd.getOptionValue(GobblinClusterConfigurationKeys.APPLICATION_NAME_OPTION_NAME), applicationId,
                ConfigFactory.load(), Optional.of(new Path(appWorkDir)))) {

            clusterMaster.start();
        }
    } catch (ParseException pe) {
        printUsage(options);
        System.exit(1);
    }
}

From source file:gobblin.aws.GobblinAWSTaskRunner.java

public static void main(String[] args) throws Exception {
    final Options options = buildOptions();

    try {/* w  w w . ja  va 2 s.  co  m*/
        final CommandLine cmd = new DefaultParser().parse(options, args);
        if (!cmd.hasOption(GobblinClusterConfigurationKeys.APPLICATION_NAME_OPTION_NAME)
                || !cmd.hasOption(GobblinClusterConfigurationKeys.HELIX_INSTANCE_NAME_OPTION_NAME)
                || !cmd.hasOption(GobblinAWSConfigurationKeys.APP_WORK_DIR)) {
            printUsage(options);
            System.exit(1);
        }

        Log4jConfigHelper.updateLog4jConfiguration(GobblinTaskRunner.class,
                GobblinAWSConfigurationKeys.GOBBLIN_AWS_LOG4J_CONFIGURATION_FILE);

        LOGGER.info(JvmUtils.getJvmInputArguments());

        final String applicationName = cmd
                .getOptionValue(GobblinClusterConfigurationKeys.APPLICATION_NAME_OPTION_NAME);
        final String helixInstanceName = cmd
                .getOptionValue(GobblinClusterConfigurationKeys.HELIX_INSTANCE_NAME_OPTION_NAME);
        final String appWorkDir = cmd.getOptionValue(GobblinAWSConfigurationKeys.APP_WORK_DIR);

        final GobblinTaskRunner gobblinTaskRunner = new GobblinAWSTaskRunner(applicationName, helixInstanceName,
                ConfigFactory.load(), Optional.of(new Path(appWorkDir)));
        gobblinTaskRunner.start();
    } catch (ParseException pe) {
        printUsage(options);
        System.exit(1);
    }
}

From source file:org.apache.gobblin.aws.GobblinAWSTaskRunner.java

public static void main(String[] args) throws Exception {
    final Options options = buildOptions();

    try {/*w  w w .  j a va  2  s. co  m*/
        final CommandLine cmd = new DefaultParser().parse(options, args);
        if (!cmd.hasOption(GobblinClusterConfigurationKeys.APPLICATION_NAME_OPTION_NAME)
                || !cmd.hasOption(GobblinClusterConfigurationKeys.HELIX_INSTANCE_NAME_OPTION_NAME)
                || !cmd.hasOption(GobblinAWSConfigurationKeys.APP_WORK_DIR)) {
            printUsage(options);
            System.exit(1);
        }

        if (System.getProperty("log4j.configuration") == null) {
            Log4jConfigHelper.updateLog4jConfiguration(GobblinTaskRunner.class,
                    GobblinAWSConfigurationKeys.GOBBLIN_AWS_LOG4J_CONFIGURATION_FILE);
        }

        LOGGER.info(JvmUtils.getJvmInputArguments());

        final String applicationName = cmd
                .getOptionValue(GobblinClusterConfigurationKeys.APPLICATION_NAME_OPTION_NAME);
        final String helixInstanceName = cmd
                .getOptionValue(GobblinClusterConfigurationKeys.HELIX_INSTANCE_NAME_OPTION_NAME);
        final String appWorkDir = cmd.getOptionValue(GobblinAWSConfigurationKeys.APP_WORK_DIR);

        final GobblinTaskRunner gobblinTaskRunner = new GobblinAWSTaskRunner(applicationName, helixInstanceName,
                ConfigFactory.load(), Optional.of(new Path(appWorkDir)));
        gobblinTaskRunner.start();
    } catch (ParseException pe) {
        printUsage(options);
        System.exit(1);
    }
}

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.  ja  v  a  2 s.c o  m
    }

    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);
}