Example usage for java.lang String valueOf

List of usage examples for java.lang String valueOf

Introduction

In this page you can find the example usage for java.lang String valueOf.

Prototype

public static String valueOf(double d) 

Source Link

Document

Returns the string representation of the double argument.

Usage

From source file:Main.java

public static void main(String[] args) {
    char c = 'x';
    int length = 10;

    // creates char array with 10 elements
    char[] chars = new char[length];

    // fill each element of chars array with 'x'
    Arrays.fill(chars, c);/*from w  ww  . j av  a  2  s . co m*/

    // print out the repeated 'x'
    System.out.println(String.valueOf(chars));
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    final JLabel valueLabel = new JLabel(String.valueOf(value));
    JButton decButton = new JButton("-");
    decButton.addActionListener(e -> valueLabel.setText(String.valueOf(--value)));

    JButton incButton = new JButton("+");
    incButton.addActionListener(e -> valueLabel.setText(String.valueOf(++value)));

    JPanel panel = new JPanel();
    panel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.weightx = 1;/*from   w  ww  . ja  va 2 s .c o  m*/
    c.gridx = 0;
    c.gridy = 0;
    panel.add(decButton, c);
    c.gridx = 1;
    panel.add(valueLabel, c);
    c.gridx = 2;
    panel.add(incButton, c);

    c.gridy = 1;
    int w = 32;
    for (c.gridx = 0; c.gridx < 3; c.gridx++) {
        panel.add(Box.createHorizontalStrut(w), c);
    }

    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
}

From source file:com.metamx.druid.utils.ExposeS3DataSource.java

public static void main(String[] args) throws ServiceException, IOException, NoSuchAlgorithmException {
    CLI cli = new CLI();
    cli.addOption(new RequiredOption(null, "s3Bucket", true, "s3 bucket to pull data from"));
    cli.addOption(new RequiredOption(null, "s3Path", true,
            "base input path in s3 bucket.  Everything until the date strings."));
    cli.addOption(new RequiredOption(null, "timeInterval", true, "ISO8601 interval of dates to index"));
    cli.addOption(new RequiredOption(null, "granularity", true, String.format(
            "granularity of index, supported granularities: [%s]", Arrays.asList(Granularity.values()))));
    cli.addOption(new RequiredOption(null, "zkCluster", true, "Cluster string to connect to ZK with."));
    cli.addOption(new RequiredOption(null, "zkBasePath", true, "The base path to register index changes to."));

    CommandLine commandLine = cli.parse(args);

    if (commandLine == null) {
        return;//w  ww .  java2s . c  o  m
    }

    String s3Bucket = commandLine.getOptionValue("s3Bucket");
    String s3Path = commandLine.getOptionValue("s3Path");
    String timeIntervalString = commandLine.getOptionValue("timeInterval");
    String granularity = commandLine.getOptionValue("granularity");
    String zkCluster = commandLine.getOptionValue("zkCluster");
    String zkBasePath = commandLine.getOptionValue("zkBasePath");

    Interval timeInterval = new Interval(timeIntervalString);
    Granularity gran = Granularity.valueOf(granularity.toUpperCase());
    final RestS3Service s3Client = new RestS3Service(new AWSCredentials(
            System.getProperty("com.metamx.aws.accessKey"), System.getProperty("com.metamx.aws.secretKey")));
    ZkClient zkClient = new ZkClient(new ZkConnection(zkCluster), Integer.MAX_VALUE, new StringZkSerializer());

    zkClient.waitUntilConnected();

    for (Interval interval : gran.getIterable(timeInterval)) {
        log.info("Processing interval[%s]", interval);
        String s3DatePath = JOINER.join(s3Path, gran.toPath(interval.getStart()));
        if (!s3DatePath.endsWith("/")) {
            s3DatePath += "/";
        }

        StorageObjectsChunk chunk = s3Client.listObjectsChunked(s3Bucket, s3DatePath, "/", 2000, null, true);
        TreeSet<String> commonPrefixes = Sets.newTreeSet();
        commonPrefixes.addAll(Arrays.asList(chunk.getCommonPrefixes()));

        if (commonPrefixes.isEmpty()) {
            log.info("Nothing at s3://%s/%s", s3Bucket, s3DatePath);
            continue;
        }

        String latestPrefix = commonPrefixes.last();

        log.info("Latest segments at [s3://%s/%s]", s3Bucket, latestPrefix);

        chunk = s3Client.listObjectsChunked(s3Bucket, latestPrefix, "/", 2000, null, true);
        Integer partitionNumber;
        if (chunk.getCommonPrefixes().length == 0) {
            partitionNumber = null;
        } else {
            partitionNumber = -1;
            for (String partitionPrefix : chunk.getCommonPrefixes()) {
                String[] splits = partitionPrefix.split("/");
                partitionNumber = Math.max(partitionNumber, Integer.parseInt(splits[splits.length - 1]));
            }
        }

        log.info("Highest segment partition[%,d]", partitionNumber);

        if (partitionNumber == null) {
            final S3Object s3Obj = new S3Object(new S3Bucket(s3Bucket),
                    String.format("%sdescriptor.json", latestPrefix));
            updateWithS3Object(zkBasePath, s3Client, zkClient, s3Obj);
        } else {
            for (int i = partitionNumber; i >= 0; --i) {
                final S3Object partitionObject = new S3Object(new S3Bucket(s3Bucket),
                        String.format("%s%s/descriptor.json", latestPrefix, i));

                updateWithS3Object(zkBasePath, s3Client, zkClient, partitionObject);
            }
        }
    }
}

From source file:org.excalibur.benchmark.test.EC2InstancesBenchmark.java

public static void main(String[] args) throws IOException {
    final String benchmark = "sp";
    final String outputDir = "/home/alessandro/excalibur/source/services/benchmarks/ec2/";

    final String[] scripts = {
            readLines(getDefaultClassLoader()
                    .getResourceAsStream("org/excalibur/service/deployment/resource/script/iperf3.sh")),
            readLines(getDefaultClassLoader()
                    .getResourceAsStream("org/excalibur/service/deployment/resource/script/linkpack.sh")),
            readLines(getDefaultClassLoader().getResourceAsStream(
                    "org/excalibur/service/deployment/resource/script/benchmarks/run_linpack_xeon64.sh")), };

    final String[] instanceTypes = { "c3.8xlarge", "r3.large", "r3.xlarge", "r3.2xlarge", "i2.xlarge" };

    final String privateKeyMaterial = IOUtils2
            .readLines(new File(SystemUtils2.getUserDirectory(), "/.ec2/leite.pem"));
    final File sshKey = new File(SystemUtils2.getUserDirectory(), "/.ec2/leite.pem");
    Properties properties = Properties2
            .load(getDefaultClassLoader().getResourceAsStream("aws-config.properties"));

    final LoginCredentials loginCredentials = new LoginCredentials.Builder()
            .identity(properties.getProperty("aws.access.key"))
            .credential(properties.getProperty("aws.secret.key")).credentialName("leite").build();

    final UserProviderCredentials userProviderCredentials = new UserProviderCredentials()
            .setLoginCredentials(loginCredentials)
            .setRegion(new Region("us-east-1").setEndpoint("https://ec2.us-east-1.amazonaws.com"));

    final EC2 ec2 = new EC2(userProviderCredentials);

    List<Callable<Void>> tasks = Lists.newArrayList();

    for (final String instanceType : instanceTypes) {
        tasks.add(new Callable<Void>() {
            @Override//w  w w . j  av  a2  s. c  o  m
            public Void call() throws Exception {
                InstanceTemplate template = new InstanceTemplate().setImageId("ami-864d84ee")
                        .setInstanceType(InstanceType.valueOf(instanceType)).setKeyName("leite")
                        .setLoginCredentials(
                                loginCredentials.toBuilder().privateKey(privateKeyMaterial).build())
                        .setGroup(new org.excalibur.core.cloud.api.Placement().setZone("us-east-1a")) //.setGroupName("iperf-bench")
                        .setMinCount(1).setMaxCount(1)
                        .setInstanceName(String.format("%s-%s", instanceType, benchmark))
                        .setRegion(userProviderCredentials.getRegion()).setTags(Tags
                                .newTags(new org.excalibur.core.cloud.api.domain.Tag("benchmark", benchmark)));

                final Instances instances = ec2.createInstances(template);

                for (VirtualMachine instance : instances) {
                    Preconditions.checkState(
                            !Strings.isNullOrEmpty(instance.getConfiguration().getPlatformUserName()));
                    Preconditions.checkState(
                            !Strings.isNullOrEmpty(instance.getConfiguration().getPublicIpAddress()));

                    HostAndPort hostAndPort = fromParts(instance.getConfiguration().getPublicIpAddress(), 22);

                    LoginCredentials sshCredentials = new LoginCredentials.Builder().authenticateAsSudo(true)
                            .privateKey(sshKey).user(instance.getConfiguration().getPlatformUserName()).build();

                    SshClient client = SshClientFactory.defaultSshClientFactory().create(hostAndPort,
                            sshCredentials);
                    client.connect();

                    try {
                        for (int i = 0; i < scripts.length; i++) {
                            ExecutableResponse response = client.execute(scripts[i]);
                            Files.write(response.getOutput().getBytes(), new File(outputDir,
                                    String.format("%s-%s.output.txt", template.getInstanceName(), i)));
                            LOG.info(
                                    "Executed the script [{}] with exit code [{}], error [{}], and output [{}] ",
                                    new Object[] { scripts[i], String.valueOf(response.getExitStatus()),
                                            response.getError(), response.getOutput() });
                        }
                    } finally {
                        client.disconnect();
                    }

                    ec2.terminateInstance(instance);
                }
                return null;
            }
        });
    }

    ListeningExecutorService executor = DynamicExecutors
            .newListeningDynamicScalingThreadPool("benchmark-instances-thread-%d");

    Futures2.invokeAllAndShutdownWhenFinish(tasks, executor);

    ec2.close();
}

From source file:MainClass.java

public static void main(String argv[]) {

    ColorPane pane = new ColorPane();
    for (int n = 1; n <= 400; n += 1) {
        if (isPrime(n)) {
            pane.append(Color.red, String.valueOf(n) + ' ');
        } else if (isPerfectSquare(n)) {
            pane.append(Color.blue, String.valueOf(n) + ' ');
        } else {//w w  w.j  a v  a 2 s  .c  o m
            pane.append(Color.black, String.valueOf(n) + ' ');
        }
    }

    JFrame f = new JFrame("ColorPane example");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setContentPane(new JScrollPane(pane));
    f.setSize(600, 400);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    char charArray[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
    boolean booleanValue = true;
    char characterValue = 'Z';
    int integerValue = 7;
    long longValue = 10000000000L; // L suffix indicates long
    float floatValue = 2.5f; // f indicates that 2.5 is a float
    double doubleValue = 3.3; // no suffix, double is default
    Object objectRef = "hello"; // assign string to an Object reference

    System.out.printf("char array = %s\n", String.valueOf(charArray));
    System.out.printf("part of char array = %s\n", String.valueOf(charArray, 3, 3));
    System.out.printf("boolean = %s\n", String.valueOf(booleanValue));
    System.out.printf("char = %s\n", String.valueOf(characterValue));
    System.out.printf("int = %s\n", String.valueOf(integerValue));
    System.out.printf("long = %s\n", String.valueOf(longValue));
    System.out.printf("float = %s\n", String.valueOf(floatValue));
    System.out.printf("double = %s\n", String.valueOf(doubleValue));
    System.out.printf("Object = %s\n", String.valueOf(objectRef));
}

From source file:MainClass.java

public static void main(String args[]) {
    char charArray[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
    boolean booleanValue = true;
    char characterValue = 'Z';
    int integerValue = 7;
    long longValue = 10000000000L; // L suffix indicates long
    float floatValue = 2.5f; // f indicates that 2.5 is a float
    double doubleValue = 33.333; // no suffix, double is default
    Object objectRef = "hello"; // assign string to an Object reference

    System.out.printf("char array = %s\n", String.valueOf(charArray));
    System.out.printf("part of char array = %s\n", String.valueOf(charArray, 3, 3));
    System.out.printf("boolean = %s\n", String.valueOf(booleanValue));
    System.out.printf("char = %s\n", String.valueOf(characterValue));
    System.out.printf("int = %s\n", String.valueOf(integerValue));
    System.out.printf("long = %s\n", String.valueOf(longValue));
    System.out.printf("float = %s\n", String.valueOf(floatValue));
    System.out.printf("double = %s\n", String.valueOf(doubleValue));
    System.out.printf("Object = %s\n", String.valueOf(objectRef));
}

From source file:Main.java

public static void main(String[] args) {

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    int x = 5;//from   w  w w  .ja  v a  2 s  . c om
    int y = 5;
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(x, y));
    for (int i = 0; i < x * y; i++) {
        JButton button = new JButton(String.valueOf(i));
        button.setPreferredSize(new Dimension(100, 100));
        panel.add(button);
    }
    JPanel container = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
    container.add(panel);
    JScrollPane scrollPane = new JScrollPane(container);
    f.getContentPane().add(scrollPane);

    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    Box box = Box.createVerticalBox();
    for (int i = 1; i < 4; i++) {
        JPanel panel = new JPanel() {
            @Override/*w w  w.j  ava  2  s .  c  om*/
            public Dimension getMaximumSize() {
                return getPreferredSize();
            }
        };
        JLabel label1 = new JLabel("Label");
        JLabel label2 = new JLabel(String.valueOf(i));
        panel.add(label1);
        panel.add(label2);
        box.add(panel);

    }

    JFrame frame = new JFrame();
    frame.add(box);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();//from  w  w w . j a v a 2 s.  c  o  m
    Chunk c = new Chunk("this is a test");
    float w = c.getWidthPoint();
    Paragraph p = new Paragraph("The width of the chunk: '");
    p.add(c);
    p.add("' is ");

    p.add(String.valueOf(w));
    p.add(" points or ");
    p.add(String.valueOf(w / 72f));
    p.add(" inches or ");
    p.add(String.valueOf(w / 72f * 2.54f));
    p.add(" cm.");
    document.add(p);
    document.add(Chunk.NEWLINE);
    document.add(c);

    document.close();
}