Java tutorial
/* * Copyright (C) 2019 offishell Development Team * * Licensed under the MIT License (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://opensource.org/licenses/MIT */ package offishell.task; import java.nio.file.Path; import java.text.Normalizer; import java.text.Normalizer.Form; import java.time.LocalDate; import java.util.ArrayList; import java.util.List; import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFStyles; import kiss.Extensible; import kiss.I; import kiss.Signal; import officeman.model.Workspace; import offishell.Date; import offishell.excel.Excel; import offishell.word.Word; import psychopath.Locator; /** * @version 2016/06/30 16:16:12 */ public interface Task extends Extensible { /** * <p> * ? * </p> * * @return */ default String category() { String name = getClass().getPackage().getName(); return name.substring(name.lastIndexOf(".") + 1); } /** * <p> * ????? * </p> * * @return */ default String name() { return getClass().getSimpleName(); } /** * <p> * ??? * </p> * * @return */ default Date month() { List<XWPFParagraph> paras = mainWord().paragraphs.toList(); List<String> methods = I.signal(new Error().getStackTrace()) .take(e -> e.getClassName().equals(getClass().getName())).map(e -> e.getMethodName()).toList(); XWPFStyles styles = paras.get(0).getDocument().getStyles(); String heading = ""; for (XWPFParagraph para : paras) { String text = para.getText(); String id = para.getStyleID(); if (id != null && styles.getStyle(id).getName().toLowerCase().contains("heading")) { heading = text; } if (methods.stream().anyMatch(text::contains)) { // parse heading text heading = Normalizer.normalize(heading, Form.NFKC); int start = heading.indexOf("("); int end = heading.indexOf(")"); if (start != -1 && end != -1) { heading = heading.substring(start + 1, end); Matcher matcher = Pattern.compile("((\\d+))?(\\d+).*").matcher(heading); if (matcher.matches()) { int year = matcher.group(1) == null ? LocalDate.now().getYear() : Integer.parseInt(matcher.group(2)); return Date.of(year, Integer.parseInt(matcher.group(3)), 1); } } return Date.now(); } } return Date.now(); } /** * <p> * ? * </p> * * @return */ default Path directory() { return Workspace.by(category()).resolve(name()); } /** * <p> * ? * </p> * * @return */ default Path directoryFor(Date date) { return directory().resolve(String.valueOf(date.period()) + ""); } /** * <p> * Find file from the current task directory properly. * </p> * * @return */ default Path file(String name) { return directory().resolve(name); } /** * <p> * Find Word file from the current task directory properly. * </p> * * @return */ default Excel excel(String name) { return Excel.of(directory(), name); } /** * <p> * Find Word file from the current task directory properly. * </p> * * @return */ default Excel excel(Date year, String name) { return Excel.of(directoryFor(year), name); } /** * <p> * ???? * </p> * * @return */ default Excel mainExcel() { return excel("???" + name() + ""); } /** * <p> * Find Word file from the current task directory properly. * </p> * * @return */ default Word word(String name) { return Word.of(Locator.directory(directory()), name); } /** * <p> * Find Word file from the specified form. * </p> * * @return */ default <T extends FiniteForm> Word word(Class<T> type, Consumer<T> setting) { T form = I.make(type); setting.accept(form); return Word.of(form.locate()).calculate(form); } /** * <p> * Find Word file from the specified form. * </p> * * @return */ default <T extends FiniteForm, M> Word word(Class<T> type, Signal<M> models, BiConsumer<T, M> setting) { return word(type, models.toList(), setting); } /** * <p> * Find Word file from the specified form. * </p> * * @return */ default <T extends FiniteForm, M> Word word(Class<T> type, List<M> models, BiConsumer<T, M> setting) { List<T> forms = new ArrayList(); for (M model : models) { T form = I.make(type); setting.accept(form, model); forms.add(form); } return Word.of(forms.get(0).locate()).calculateAndMerge(forms); } /** * <p> * ?????? * </p> * * @return */ default Word mainWord() { return word("???" + name() + ""); } }