OSDN Git Service

16338848ef626d1e113841c6c9bf257f03e1b91c
[delesterandomselector/DelesteRandomSelector.git] / src / com / ranfa / lib / EstimateAlbumTypeCycle.java
1 package com.ranfa.lib;
2
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;
13
14 import javax.swing.JOptionPane;
15
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
20 import com.fasterxml.jackson.databind.ObjectMapper;
21 import com.fasterxml.jackson.databind.ObjectWriter;
22
23 public class EstimateAlbumTypeCycle {
24
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);
29
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";
33
34         public static void Initialization() {
35                 if(Files.exists(Paths.get(CYCLEPATH)))
36                         return;
37                 logger.info("Cycle definition file does not exist.Trying to ask you...");
38                 AlbumCycleDefinitionProperty property = new AlbumCycleDefinitionProperty();
39                 String inputType = JOptionPane.showInputDialog("現在のMASTER+のALBUMを入力してください。(A,B,C)");
40                 if(!(inputType.equals("A") || inputType.equals("B") || inputType.equals("C"))) {
41                         logger.error("inputType has invaild.Canceling initiate...");
42                         return;
43                 }
44                 String inputDaysLeft = JOptionPane.showInputDialog("MASTER+のALBUM切り替えまであと何日ですか?\n(残り時間が表示されている場合は0を入力してください)");
45                 String dateDefinited = FORMAT.format(new Date());
46                 property.setDateDefinited(dateDefinited);
47                 property.setDaysLeft(Integer.parseInt(inputDaysLeft));
48                 property.setType(inputType.equals("A") ? ALBUM_A : inputType.equals("B") ? ALBUM_B : ALBUM_C);
49                 write(property);
50                 return;
51         }
52
53         private static void write(AlbumCycleDefinitionProperty property) {
54                 ObjectWriter writer = new ObjectMapper().writer(new DefaultPrettyPrinter());
55                 try {
56                         writer.writeValue(Paths.get(CYCLEPATH).toFile(), property);
57                 } catch (IOException e) {
58                         e.printStackTrace();
59                 }
60         }
61
62         public static String getCurrentCycle() {
63                 if(Files.notExists(Paths.get(CYCLEPATH)))
64                         throw new IllegalStateException("Program seems to have avoided first initiating. how could it have done?");
65                 AlbumCycleDefinitionProperty property = new AlbumCycleDefinitionProperty();
66                 try {
67                         property = new ObjectMapper().readValue(Paths.get(CYCLEPATH).toFile(), AlbumCycleDefinitionProperty.class);
68                 } catch (IOException e) {
69                         e.printStackTrace();
70                 }
71                 Date presentDate = new Date();
72                 Calendar presentCalendar = Calendar.getInstance();
73                 presentCalendar.setTime(presentDate);
74                 presentCalendar.set(Calendar.HOUR_OF_DAY, 0);
75                 presentCalendar.set(Calendar.MINUTE, 0);
76                 presentCalendar.set(Calendar.SECOND, 0);
77                 presentCalendar.set(Calendar.MILLISECOND, 0);
78                 presentDate = presentCalendar.getTime();
79                 String dateDefinited = property.getDateDefinited();
80                 String dates[] = dateDefinited.split("/");
81                 presentCalendar.set(Integer.parseInt(dates[0]), Integer.parseInt(dates[1]) - 1, Integer.parseInt(dates[2]));
82                 Date definiteDate = presentCalendar.getTime();
83                 switch(presentDate.compareTo(definiteDate)) {
84                 case 0:
85                         return property.getType();
86                 case 1:
87                         LocalDate presentLocalDate = presentDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
88                         LocalDate definitedLocalDate = definiteDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
89                         long delta = ChronoUnit.DAYS.between(definitedLocalDate, presentLocalDate);
90                         if(delta < property.getDaysLeft()) {
91                                 return property.getType();
92                         }
93                         delta = delta - property.getDaysLeft();
94                         if(delta > Integer.MAX_VALUE) {
95                                 JOptionPane.showMessageDialog(null, "ALBUM周期の推定に失敗しました。暫定的な措置として前回起動時のALBUM種類を表示します。\n(内部変数エラー:delta has the value that is more than Integer.MAX_VALUE.)");
96                                 logger.error("Valuable was overflowed.");
97                         }
98                         String res = cycling(property.getType(), (int)delta);
99                         return res;
100                 default:
101                         throw new IllegalStateException("Date delta has illegal value. the system clock might be incorrect?");
102                 }
103         }
104
105         private static String cycling(String currentType, int times) {
106                 int cyclingDelta = times % 3;
107                 String[] typeArray = {
108                                 ALBUM_A,
109                                 ALBUM_B,
110                                 ALBUM_C
111                 };
112                 int currentIndex = Arrays.asList(typeArray).indexOf(currentType);
113                 int nextIndex = currentIndex + cyclingDelta;
114                 int nextIndexDelta = nextIndex % 3;
115                 return typeArray[nextIndexDelta];
116         }
117
118 }