ãã¹ã¯ãããã«ã«ã¬ã³ãã¼ã表示ããããã°ã©ã ãJavaFXã§ä½æãã¦ãã¾ãã
https://github.com/torutk/calendar
Ver. 0.1.8ã§ã¯ãæ¥ä»ã®è²ããå¹³æ¥ã¯é»ãåææ¥ã¯éãæ¥ææ¥ã¯èµ¤ã¨ãã¦ãã¾ããããããã¯ãç¥æ¥ãèå¥ããããªã¨æãã¾ããã
æ¥æ¬ã®ç¥æ¥ã¯ãæ¥ä»åºå®ã®ç¥æ¥ï¼å æ¦ã®1æ1æ¥ã建å½è¨å¿µæ¥ã®2æ11æ¥ãªã©ï¼ãæã®ä½çªç®ãã®é±ã®æææ¥ã¨ãªãç¥æ¥ï¼æ人ã®æ¥ã®1æ第2æææ¥ãæµ·ã®æ¥ã®7æ第3æææ¥ï¼ãå½ç«å¤©æå°ã®æ¦è¨ç®ã«åºã¥ãé£è°æ±ºå®ã§å®ã¾ãç¥æ¥ï¼æ¥åã®æ¥ãç§åã®æ¥ï¼ãæ¥ææ¥ã¨ç¥æ¥ãéãªã£ãã¨ãã®æ¯ãæ¿ãä¼æ¥ï¼åºæ¬ã¯ç¿æææ¥ã ãç¥æ¥ãé£ã¡ããããã¨ãã¯ããã«ãããï¼ãç¥æ¥ã¨ç¥æ¥ã«1æ¥ã ãå¹³æ¥ãæã¾ããæã®å½æ°ã®ä¼æ¥ãªã©ããããäºåã«ãã¸ãã¯ã§è¨ç®ããããã¨ãå°é£ã«æãã¾ããã
ããã§ãç¥æ¥ãäºãè¨å®ãã¡ã¤ã«ã«è¨è¼ãããã®è¨å®ãã¡ã¤ã«ããç¥æ¥æ å ±ãèªã¿è¾¼ãæ©è½ã追å ãããã¨ã«ãã¾ããã
è¨å®ãã¡ã¤ã«ã«ã¯ãç¥æ¥ã¨ããå¹´ææ¥ã1è¡1件ã§è¨è¼ãããã¨ã¨ããå¹´ææ¥ã¯java.time.LocalDateã¯ã©ã¹ããã¼ã¹ã§ããå½¢å¼ï¼ISO-8601ï¼ã§è¨è¿°ãã¾ãã
è¨å®ãã¡ã¤ã«ã¯ãã³ãã³ãã©ã¤ã³ãªãã·ã§ã³ã§ãã®ãã¹ãæå®ããè¨å®ãã¡ã¤ã«ãåå¨ããªãå ´åã¯ããã°ã©ã ã®ä¸é¨ï¼JARãã¡ã¤ã«ã®ä¸ã«çµã¿è¾¼ããã¡ã¤ã«ï¼ã¨ãã¦æä¾ããããã©ã«ãã®è¨å®ãã¡ã¤ã«ã使ç¨ãããã¨ã¨ãã¾ãã
ã³ãã³ãã©ã¤ã³ãªãã·ã§ã³ã§ãã¹ãæå®
JavaFXã§ã¯ãããã°ã©ã èµ·åæã«ã³ãã³ãã©ã¤ã³ãªãã·ã§ã³çã§æ¸¡ãããæ
å ±ããã©ã¡ã¼ã¿APIã¨ãã¦å®ç¾©ãã¦ãã¾ãã詳ããã¯æ¬¡ã®Wikiãã¼ã¸ã«è¨è¼ãã¦ãã¾ãã
JavaFXとパラメータの取得 - ソフトウェアエンジニアリング - Torutk
ãã©ã¡ã¼ã¿APIã¯ãã³ãã³ãã©ã¤ã³ãªãã·ã§ã³ãJava Web Startã®ãã©ã¡ã¼ã¿ï¼HTMLï¼ããã¤ãã£ããã³ãã«ã®ã³ã³ãã£ã°ã¬ã¼ã·ã§ã³ãã¡ã¤ã«ãªã©ã«è¨å®ãããªãã·ã§ã³ãå
±éã®APIã§èªã¿åãã¾ãã
ãªãã·ã§ã³ã«ã¯ãååä»ããååãªãã®2種é¡ããããåè
ã¯--aaa=bbbã®å½¢å¼ãå¾è
ã¯--cccã®å½¢å¼ã§ãã
--holiday=conf/holidays.txt
ã®ããã«ã³ãã³ãã©ã¤ã³ã§æå®ããã¨ãã¾ãã
javafx.application.Applicationæ´¾çã¯ã©ã¹ã®startã¡ã½ããå
ãªã©ã§æ¬¡ã®APIã§æå®ããå
容ï¼ä¾ã§ã¯ "conf/holidays.txt"ï¼ãåå¾ãã¾ãã
Map<String, String> params = getParameters().getNamed(); String confPathText = params.getOrDefault("holiday", "holidays.conf");
Applicationã¯ã©ã¹ã®getParametersã¡ã½ããã§ãèµ·åæãªãã·ã§ã³æ
å ±ãåå¾ãã¾ããä»åã¯ååä»ããªãã·ã§ã³ã§æå®ããã®ã§ãããã«getNamedã¡ã½ããã§ååä»ããªãã·ã§ã³æ
å ±ãMap
Java SE 8ã§ã¯Mapã¤ã³ã¿ãã§ã¼ã¹ã«getOrDefaultã¡ã½ããã追å ããããã¼ã«æå®ããå¤ãæ ¼ç´ããã¦ããªãã¨ãã«ãnullã§ã¯ãªã第2å¼æ°ã«æå®ããå¤ãè¿ããã¨ãã§ãã¾ãã
ãã¹ã§æå®ãããã¡ã¤ã«ã®ããã¹ãæ å ±ãèªã¿åºã
Java SE 7ããæè¼ãããNIO2 APIã使ã£ã¦ãã¡ã¤ã«ãèªã¿åºãã¾ãã
java.nio.fileããã±ã¼ã¸ã®Pathsã¯ã©ã¹ãPathã¯ã©ã¹ãFilesã¯ã©ã¹ã使ãã¾ãã
Path filePath = Paths.get(confPathText);
ä»åã¯ããã¡ã¤ã«ã·ã¹ãã ã®åºææ©è½ã¯ä½¿ããªãã®ã§ãç°¡æã«Pathã¤ã³ã¹ã¿ã³ã¹ãçæããPathsã¯ã©ã¹ã®getã¡ã½ããã使ãã¾ãããã¡ã¤ã«ã¸ã®ãã¹ã表ç¾ããæååãå¼æ°ã¨ãã¦Paths.getã¡ã½ãããå¼ã¶ã¨ããã¡ã¤ã«ã·ã¹ãã ä¸ã®ãã¹ã表ç¾ããPathã¤ã³ã¹ã¿ã³ã¹ãçæããã¾ããå¼æ°ã«æå®ããæååã¯ãç¸å¯¾ãã¹ã§ã絶対ãã¹ã§ãæå®å¯è½ã§ãã
if (Files.exists(filePath)) {
ãã¡ã¤ã«ãå®å¨ãããã©ãããjava.nio.file.Filesã¯ã©ã¹ã®existsã¡ã½ããã§å¤å®ãã¾ãã
List<LocalDate> holidays = null; try (Stream<String> lines = Files.lines(filePath)) { // (1) holidays = lines.map(s -> LocalDate.parse(s)) // (2) .collect(toList()); // (3) } catch (IOException ex) { logger.config(() -> String.format( "In holidays configuration, file %s cannot be read because of %s", filePath, ex.getLocalizedMessage() )); }
ãã¡ã¤ã«ã®å
容ã1è¡1è¡åãåºãã¦å¦çããã¦ããå ´åãJava SE 8ããæè¼ãããStream APIã使ãã¨ç°¡æ½ã«è¨è¿°ã§ãã¾ãããã¡ã¤ã«ããèªã¿è¾¼ããã¼ã¿ãStringåã®Streamã¨ãã¦çæã(1)ãStream APIã®mapã§StringåããLocalDateåã¸å¤æã(2)ããã®çµæãList
Files.lineã¯ãã¡ã¤ã«ããªã¼ãã³ããã®ã§ã使ç¨å¾ã«ã¯ãã¼ãºãå¿ è¦ã§ããtry-with-resourceæ§æã¨ä¸ç·ã«ä½¿ãããã«ãã¾ãã
空è¡ãããã¨ã¨ã©ã¼ã«ãªã£ã¦ãã¾ã
è¨å®ãã¡ã¤ã«ã«ç©ºè¡ãããã¨ã(1)ã®ãã¡ã¤ã«ããèªã¿è¾¼ãã ãã¼ã¿ï¼Streamï¼ã«ã空æååãå
¥ãã¾ãããããã¯(2)ã®mapã§LocalDate.parse(s)ã®sã«ç©ºæåãå
¥ãã¨ã©ã¼ï¼ä¾å¤çºçï¼ã¨ãªãã¾ãã
ããã§ãããããã空æååãé¤å¤ãããã£ã«ã¿ã¼ãå
¥ãã¾ã(4)ã
try (Stream<String> lines = Files.lines(filePath)) { // (1) holidays = lines.filter(s -> !s.isEmpty()) // (4) .map(s -> LocalDate.parse(s)) // (2) .collect(toList()); // (3)
ã¯ã©ã¹ãã¹ä¸ãããã¡ã¤ã«ãèªã¿è¾¼ã
JARãã¡ã¤ã«ã«å梱ããè¨å®ãã¡ã¤ã«ãèªã¿è¾¼ãå ´åã¯ãé常ã®Pathã§ã¯æå®ã§ããªãã®ã§ãClassã¯ã©ã¹ã®getResourceAsStreamã¡ã½ãããå©ç¨ãã¾ã*1ã
List<LocalDate> holidays = null; try (BufferedReader reader = new BufferedReader(new InputStreamReader( this.getClass().getResourceAsStream("/holiday.conf"))) ) { holidays = reader.lines.filter(s -> !s.isEmpty()) .map(s -> LocalDate.parse(s)) .collect(toList()); } catch (IOException ex) { logger.warning(() -> String.format( "In holidays configuration, resource %s cannot be read because of %s", "/holiday.conf", ex.getLocalizedMessage())); }
ã¯ã©ã¹ãã¹ä¸ã«åå¨ãããã¡ã¤ã«ãèªã¿è¾¼ãå ´åã¯ãClassã¤ã³ã¹ã¿ã³ã¹ã®getResourceAsStreamã¡ã½ããã§ãã¡ã¤ã«ãã¹ãæå®ããInputStreamã¤ã³ã¹ã¿ã³ã¹ãåå¾ãã¾ãã
Stream
ãã¡ã¤ã«ããªã¼ãã³ããå¦çãªã®ã§ã使ç¨å¾ã«ã¯ãã¼ãºãå¿ è¦ã§ããtry-with-resourceæ§æã¨ä¸ç·ã«ä½¿ãããã«ãã¾ãã
*1:JARãã¡ã¤ã«ãç¹å®ããJARãã¡ã¤ã«å ã®ãã¹ãç¹å®ã§ããã°ãZIPãã¡ã¤ã«ã·ã¹ãã ã®Pathã¨ãã¦æ±ããã¨ãã§ãããããããªãã¨æãã¾ãããæªç¢ºèªã§ãã