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)))
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...");
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);
53 private static void write(AlbumCycleDefinitionProperty property) {
54 ObjectWriter writer = new ObjectMapper().writer(new DefaultPrettyPrinter());
56 writer.writeValue(Paths.get(CYCLEPATH).toFile(), property);
57 } catch (IOException e) {
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();
67 property = new ObjectMapper().readValue(Paths.get(CYCLEPATH).toFile(), AlbumCycleDefinitionProperty.class);
68 } catch (IOException e) {
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)) {
85 return property.getType();
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();
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.");
98 String res = cycling(property.getType(), (int)delta);
101 throw new IllegalStateException("Date delta has illegal value. the system clock might be incorrect?");
105 private static String cycling(String currentType, int times) {
106 int cyclingDelta = times % 3;
107 String[] typeArray = {
112 int currentIndex = Arrays.asList(typeArray).indexOf(currentType);
113 int nextIndex = currentIndex + cyclingDelta;
114 int nextIndexDelta = nextIndex % 3;
115 return typeArray[nextIndexDelta];