Java tutorial
/* * Copyright 2014 agwlvssainokuni * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cherry.chart.app; import static java.lang.Integer.parseInt; import static java.text.MessageFormat.format; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.LinkedList; import java.util.List; import java.util.Random; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.data.category.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; import org.joda.time.LocalDate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import cherry.chart.ExitStatus; import cherry.chart.IBatch; @Component public class LineChartBatch implements IBatch { private final Logger log = LoggerFactory.getLogger(getClass()); @Value("${chartapp.lineChart.toDir}") private File toDir; @Value("${chartapp.lineChart.file}") private String file; @Value("${chartapp.lineChart.defaultNumThread}") private int defaultNumThread; @Value("${chartapp.lineChart.defaultCount}") private int defaultCount; @Value("${chartapp.lineChart.title}") private String title; @Value("#{'${chartapp.lineChart.series}'.split(',')}") private List<String> series; @Value("${chartapp.lineChart.xLabel}") private String xLabel; @Value("${chartapp.lineChart.yLabel}") private String yLabel; @Value("${chartapp.lineChart.width}") private int width; @Value("${chartapp.lineChart.height}") private int height; private Random random = new Random(); @Override public ExitStatus execute(String... args) { int nThread = (args.length < 1 ? defaultNumThread : parseInt(args[0])); int count = (args.length < 2 ? defaultCount : parseInt(args[1])); ExecutorService executorService = Executors.newFixedThreadPool(nThread); List<Future<Boolean>> tasks = new LinkedList<>(); for (int i = 0; i < count; i++) { final String numStr = String.valueOf(i); tasks.add(executorService.submit(new Callable<Boolean>() { @Override public Boolean call() { File f = new File(toDir, format(file, numStr)); String t = format(title, numStr); CategoryDataset dataset = createDataset(); JFreeChart chart = ChartFactory.createLineChart(t, xLabel, yLabel, dataset); try (OutputStream out = new FileOutputStream(f)) { ChartUtilities.writeChartAsPNG(out, chart, width, height); return true; } catch (IOException ex) { log.error("failed to create file", ex); return false; } } })); } boolean success = true; for (Future<Boolean> future : tasks) { try { success &= future.get(); } catch (ExecutionException | InterruptedException ex) { log.error("failed to get result", ex); success = false; } } return (success ? ExitStatus.NORMAL : ExitStatus.ERROR); } private CategoryDataset createDataset() { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); LocalDate now = LocalDate.now(); for (int i = 0; i <= 12; i++) { String date = now.plusMonths(i).toString(); for (String s : series) { dataset.addValue(random.nextInt(100), s, date); } } return dataset; } }