3 import java.io.IOException;
4 import java.nio.file.Files;
5 import java.nio.file.Paths;
6 import java.text.SimpleDateFormat;
7 import java.time.LocalDate;
8 import java.time.ZoneId;
9 import java.time.temporal.ChronoUnit;
10 import java.util.Arrays;
11 import java.util.Calendar;
12 import java.util.Date;
14 import javax.swing.JOptionPane;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
19 import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
20 import com.fasterxml.jackson.databind.ObjectMapper;
21 import com.fasterxml.jackson.databind.ObjectWriter;
23 public class EstimateAlbumTypeCycle {
25 private final static String CYCLEPATH = "generated/albumCycle.json";
26 private final static String DATEFORMAT = "YYYY/MM/dd";
27 private final static SimpleDateFormat FORMAT = new SimpleDateFormat(DATEFORMAT);
28 private static Logger logger = LoggerFactory.getLogger(EstimateAlbumTypeCycle.class);
30 public final static String ALBUM_A = "ALBUM A";
31 public final static String ALBUM_B = "ALBUM B";
32 public final static String ALBUM_C = "ALBUM C";
34 public static void Initialization() {
35 if(Files.exists(Paths.get(CYCLEPATH))) {
38 logger.info("Cycle definition file does not exist.Trying to ask you...");
39 AlbumCycleDefinitionProperty property = new AlbumCycleDefinitionProperty();
40 String inputType = JOptionPane.showInputDialog("現在のMASTER+のALBUMを入力してください。(A,B,C)");
41 if(!(inputType.equals("A") || inputType.equals("B") || inputType.equals("C"))) {
42 logger.error("inputType has invaild.Canceling initiate...");
45 String inputDaysLeft = JOptionPane.showInputDialog("MASTER+のALBUM切り替えまであと何日ですか?\n(残り時間が表示されている場合は0を入力してください)");
46 String dateDefinited = FORMAT.format(new Date());
47 property.setDateDefinited(dateDefinited);
48 property.setDaysLeft(Integer.parseInt(inputDaysLeft));
49 property.setType(inputType.equals("A") ? ALBUM_A : inputType.equals("B") ? ALBUM_B : ALBUM_C);
54 private static void write(AlbumCycleDefinitionProperty property) {
55 ObjectWriter writer = new ObjectMapper().writer(new DefaultPrettyPrinter());
57 writer.writeValue(Paths.get(CYCLEPATH).toFile(), property);
58 } catch (IOException e) {
59 logger.warn("Couldn't write album type information.", e);
63 public static String getCurrentCycle() {
64 if(Files.notExists(Paths.get(CYCLEPATH))) {
65 throw new IllegalStateException("Program seems to have avoided first initiating. how could it have done?");
67 AlbumCycleDefinitionProperty property = new AlbumCycleDefinitionProperty();
69 property = new ObjectMapper().readValue(Paths.get(CYCLEPATH).toFile(), AlbumCycleDefinitionProperty.class);
70 } catch (IOException e) {
71 logger.error("Error while reading local definition file.", e);
73 Date presentDate = new Date();
74 Calendar presentCalendar = Calendar.getInstance();
75 presentCalendar.setTime(presentDate);
76 presentCalendar.set(Calendar.HOUR_OF_DAY, 0);
77 presentCalendar.set(Calendar.MINUTE, 0);
78 presentCalendar.set(Calendar.SECOND, 0);
79 presentCalendar.set(Calendar.MILLISECOND, 0);
80 presentDate = presentCalendar.getTime();
81 String dateDefinited = property.getDateDefinited();
82 String dates[] = dateDefinited.split("/");
83 presentCalendar.set(Integer.parseInt(dates[0]), Integer.parseInt(dates[1]) - 1, Integer.parseInt(dates[2]));
84 Date definiteDate = presentCalendar.getTime();
85 switch(presentDate.compareTo(definiteDate)) {
87 return property.getType();
89 LocalDate presentLocalDate = presentDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
90 LocalDate definitedLocalDate = definiteDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
91 long delta = ChronoUnit.DAYS.between(definitedLocalDate, presentLocalDate);
92 if(delta < property.getDaysLeft()) {
93 return property.getType();
95 delta = delta - property.getDaysLeft();
96 if(delta > Integer.MAX_VALUE) {
97 JOptionPane.showMessageDialog(null, "ALBUM周期の推定に失敗しました。暫定的な措置として前回起動時のALBUM種類を表示します。\n(内部変数エラー:delta has the value that is more than Integer.MAX_VALUE.)");
98 logger.error("Valuable was overflowed.");
100 String res = cycling(property.getType(), (int)delta);
103 throw new IllegalStateException("Date delta has illegal value. the system clock might be incorrect?");
107 private static String cycling(String currentType, int times) {
108 int cyclingDelta = (times / 14) % 3;
109 String[] typeArray = {
114 int currentIndex = Arrays.asList(typeArray).indexOf(currentType);
115 int nextIndex = currentIndex + cyclingDelta;
116 int nextIndexDelta = nextIndex % 3;
117 return typeArray[nextIndexDelta];