From 0e509fa03591cb8449c04e91c72f109049cc97ff Mon Sep 17 00:00:00 2001 From: Ely Date: Sun, 14 Jan 2024 01:29:15 -0700 Subject: [PATCH] these are the more complicated code improvements --- .../zmanim/hebrewcalendar/TefilaRules.java | 97 +++--- .../java/com/kosherjava/zmanim/util/Zman.java | 46 ++- .../zmanim/util/ZmanimFormatter.java | 295 +++++++++--------- 3 files changed, 210 insertions(+), 228 deletions(-) diff --git a/src/main/java/com/kosherjava/zmanim/hebrewcalendar/TefilaRules.java b/src/main/java/com/kosherjava/zmanim/hebrewcalendar/TefilaRules.java index b5d7eab2..fbe8473b 100644 --- a/src/main/java/com/kosherjava/zmanim/hebrewcalendar/TefilaRules.java +++ b/src/main/java/com/kosherjava/zmanim/hebrewcalendar/TefilaRules.java @@ -170,34 +170,31 @@ public boolean isTachanunRecitedShacharis(JewishCalendar jewishCalendar) { int day = jewishCalendar.getJewishDayOfMonth(); int month = jewishCalendar.getJewishMonth(); - if (jewishCalendar.getDayOfWeek() == Calendar.SATURDAY - || (!tachanunRecitedSundays && jewishCalendar.getDayOfWeek() == Calendar.SUNDAY) - || (!tachanunRecitedFridays && jewishCalendar.getDayOfWeek() == Calendar.FRIDAY) - || month == JewishDate.NISSAN - || (month == JewishDate.TISHREI && ((!tachanunRecitedEndOfTishrei && day > 8) - || (tachanunRecitedEndOfTishrei && (day > 8 && day < 22)))) - || (month == JewishDate.SIVAN && (tachanunRecitedWeekAfterShavuos && day < 7 - || !tachanunRecitedWeekAfterShavuos && day < (!jewishCalendar.getInIsrael() - && !tachanunRecited13SivanOutOfIsrael ? 14: 13))) - || (jewishCalendar.isYomTov() && (! jewishCalendar.isTaanis() - || (!tachanunRecitedPesachSheni && holidayIndex == JewishCalendar.PESACH_SHENI))) // Erev YT is included in isYomTov() - || (!jewishCalendar.getInIsrael() && !tachanunRecitedPesachSheni && !tachanunRecited15IyarOutOfIsrael - && jewishCalendar.getJewishMonth() == JewishDate.IYAR && day == 15) - || holidayIndex == JewishCalendar.TISHA_BEAV || jewishCalendar.isIsruChag() - || jewishCalendar.isRoshChodesh() - || (!tachanunRecitedShivasYemeiHamiluim && - ((!jewishCalendar.isJewishLeapYear() && month == JewishDate.ADAR) - || (jewishCalendar.isJewishLeapYear() && month == JewishDate.ADAR_II)) && day > 22) - || (!tachanunRecitedWeekOfPurim && - ((!jewishCalendar.isJewishLeapYear() && month == JewishDate.ADAR) - || (jewishCalendar.isJewishLeapYear() && month == JewishDate.ADAR_II)) && day > 10 && day < 18) - || (jewishCalendar.isUseModernHolidays() - && (holidayIndex == JewishCalendar.YOM_HAATZMAUT || holidayIndex == JewishCalendar.YOM_YERUSHALAYIM)) - || (!tachanunRecitedWeekOfHod && month == JewishDate.IYAR && day > 13 && day < 21)) { - return false; - } - return true; - } + return jewishCalendar.getDayOfWeek() != Calendar.SATURDAY + && (tachanunRecitedSundays || jewishCalendar.getDayOfWeek() != Calendar.SUNDAY) + && (tachanunRecitedFridays || jewishCalendar.getDayOfWeek() != Calendar.FRIDAY) + && month != JewishDate.NISSAN + && (month != JewishDate.TISHREI || ((tachanunRecitedEndOfTishrei || day <= 8) + && (!tachanunRecitedEndOfTishrei || (day <= 8 || day >= 22)))) + && (month != JewishDate.SIVAN || ((!tachanunRecitedWeekAfterShavuos || day >= 7) + && (tachanunRecitedWeekAfterShavuos || day >= (!jewishCalendar.getInIsrael() + && !tachanunRecited13SivanOutOfIsrael ? 14 : 13)))) + && (!jewishCalendar.isYomTov() || (jewishCalendar.isTaanis() + && (tachanunRecitedPesachSheni || holidayIndex != JewishCalendar.PESACH_SHENI))) // Erev YT is included in isYomTov() + && (jewishCalendar.getInIsrael() || tachanunRecitedPesachSheni || tachanunRecited15IyarOutOfIsrael + || jewishCalendar.getJewishMonth() != JewishDate.IYAR || day != 15) + && holidayIndex != JewishCalendar.TISHA_BEAV && !jewishCalendar.isIsruChag() + && !jewishCalendar.isRoshChodesh() + && (tachanunRecitedShivasYemeiHamiluim || + ((jewishCalendar.isJewishLeapYear() || month != JewishDate.ADAR) + && (!jewishCalendar.isJewishLeapYear() || month != JewishDate.ADAR_II)) || day <= 22) + && (tachanunRecitedWeekOfPurim || + ((jewishCalendar.isJewishLeapYear() || month != JewishDate.ADAR) + && (!jewishCalendar.isJewishLeapYear() || month != JewishDate.ADAR_II)) || day <= 10 || day >= 18) + && (!jewishCalendar.isUseModernHolidays() + || (holidayIndex != JewishCalendar.YOM_HAATZMAUT && holidayIndex != JewishCalendar.YOM_YERUSHALAYIM)) + && (tachanunRecitedWeekOfHod || month != JewishDate.IYAR || day <= 13 || day >= 21); + } /** * Returns if tachanun is recited during mincha on the day in question. @@ -210,19 +207,16 @@ public boolean isTachanunRecitedMincha(JewishCalendar jewishCalendar) { JewishCalendar tomorrow = new JewishCalendar(); tomorrow = (JewishCalendar) jewishCalendar.clone(); tomorrow.forward(Calendar.DATE, 1); - - if (!tachanunRecitedMinchaAllYear - || jewishCalendar.getDayOfWeek() == Calendar.FRIDAY - || ! isTachanunRecitedShacharis(jewishCalendar) - || (! isTachanunRecitedShacharis(tomorrow) && - !(tomorrow.getYomTovIndex() == JewishCalendar.EREV_ROSH_HASHANA) && - !(tomorrow.getYomTovIndex() == JewishCalendar.EREV_YOM_KIPPUR) && - !(tomorrow.getYomTovIndex() == JewishCalendar.PESACH_SHENI)) - || ! tachanunRecitedMinchaErevLagBaomer && tomorrow.getYomTovIndex() == JewishCalendar.LAG_BAOMER) { - return false; - } - return true; - } + + return tachanunRecitedMinchaAllYear + && jewishCalendar.getDayOfWeek() != Calendar.FRIDAY + && isTachanunRecitedShacharis(jewishCalendar) + && (isTachanunRecitedShacharis(tomorrow) || + tomorrow.getYomTovIndex() == JewishCalendar.EREV_ROSH_HASHANA || + tomorrow.getYomTovIndex() == JewishCalendar.EREV_YOM_KIPPUR || + tomorrow.getYomTovIndex() == JewishCalendar.PESACH_SHENI) + && (tachanunRecitedMinchaErevLagBaomer || tomorrow.getYomTovIndex() != JewishCalendar.LAG_BAOMER); + } /** * Returns if it is the Jewish day (starting the evening before) to start reciting Vesein Tal Umatar Livracha @@ -446,12 +440,8 @@ public boolean isHallelShalemRecited(JewishCalendar jewishCalendar) { int month = jewishCalendar.getJewishMonth(); boolean inIsrael = jewishCalendar.getInIsrael(); if(isHallelRecited(jewishCalendar)) { - if((jewishCalendar.isRoshChodesh() && ! jewishCalendar.isChanukah()) - || (month == JewishDate.NISSAN && ((inIsrael && day > 15) || (!inIsrael && day > 16)))) { - return false; - } else { - return true; - } + return (!jewishCalendar.isRoshChodesh() || jewishCalendar.isChanukah()) + && (month != JewishDate.NISSAN || ((!inIsrael || day <= 15) && (inIsrael || day <= 16))); } return false; } @@ -503,14 +493,11 @@ public boolean isMizmorLesodaRecited(JewishCalendar jewishCalendar) { } int holidayIndex = jewishCalendar.getYomTovIndex(); - if(! isMizmorLesodaRecitedErevYomKippurAndPesach() - && (holidayIndex == JewishCalendar.EREV_YOM_KIPPUR - || holidayIndex == JewishCalendar.EREV_PESACH - || jewishCalendar.isCholHamoedPesach())) { - return false; - } - return true; - } + return isMizmorLesodaRecitedErevYomKippurAndPesach() + || (holidayIndex != JewishCalendar.EREV_YOM_KIPPUR + && holidayIndex != JewishCalendar.EREV_PESACH + && !jewishCalendar.isCholHamoedPesach()); + } /** * Is tachanun set to be recited during the week of Purim, from the 11th through the 17th of {@link diff --git a/src/main/java/com/kosherjava/zmanim/util/Zman.java b/src/main/java/com/kosherjava/zmanim/util/Zman.java index 9c415025..9255984e 100644 --- a/src/main/java/com/kosherjava/zmanim/util/Zman.java +++ b/src/main/java/com/kosherjava/zmanim/util/Zman.java @@ -192,13 +192,11 @@ public void setDescription(String description) { * than the second. * Please note that this class will handle cases where either the {@code Zman} is a null or {@link #getZman()} returns a null. */ - public static final Comparator DATE_ORDER = new Comparator() { - public int compare(Zman zman1, Zman zman2) { - long firstTime = (zman1 == null || zman1.getZman() == null) ? Long.MAX_VALUE : zman1.getZman().getTime(); - long secondTime = (zman2 == null || zman2.getZman() == null) ? Long.MAX_VALUE : zman2.getZman().getTime(); - return Long.valueOf(firstTime).compareTo(Long.valueOf(secondTime)); - } - }; + public static final Comparator DATE_ORDER = (zman1, zman2) -> { + long firstTime = (zman1 == null || zman1.getZman() == null) ? Long.MAX_VALUE : zman1.getZman().getTime(); + long secondTime = (zman2 == null || zman2.getZman() == null) ? Long.MAX_VALUE : zman2.getZman().getTime(); + return Long.compare(firstTime, secondTime); + }; /** * A {@link Comparator} that will compare and sort zmanim by zmanim label order. Compares its two arguments by the zmanim label @@ -207,13 +205,11 @@ public int compare(Zman zman1, Zman zman2) { * Please note that this class will sort cases where either the {@code Zman} is a null or {@link #label} returns a null * as empty {@code String}s. */ - public static final Comparator NAME_ORDER = new Comparator() { - public int compare(Zman zman1, Zman zman2) { - String firstLabel = (zman1 == null || zman1.getLabel() == null) ? "" : zman1.getLabel(); - String secondLabel = (zman2 == null || zman2.getLabel() == null) ? "" : zman2.getLabel(); - return firstLabel.compareTo(secondLabel); - } - }; + public static final Comparator NAME_ORDER = (zman1, zman2) -> { + String firstLabel = (zman1 == null || zman1.getLabel() == null) ? "" : zman1.getLabel(); + String secondLabel = (zman2 == null || zman2.getLabel() == null) ? "" : zman2.getLabel(); + return firstLabel.compareTo(secondLabel); + }; /** * A {@link Comparator} that will compare and sort duration based zmanim such as @@ -223,23 +219,19 @@ public int compare(Zman zman1, Zman zman2) { * integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. * Please note that this class will sort cases where {@code Zman} is a null. */ - public static final Comparator DURATION_ORDER = new Comparator() { - public int compare(Zman zman1, Zman zman2) { - long firstDuration = zman1 == null ? Long.MAX_VALUE : zman1.getDuration(); - long secondDuration = zman2 == null ? Long.MAX_VALUE : zman2.getDuration(); - return firstDuration == secondDuration ? 0 : firstDuration > secondDuration ? 1 : -1; - } - }; + public static final Comparator DURATION_ORDER = (zman1, zman2) -> { + long firstDuration = zman1 == null ? Long.MAX_VALUE : zman1.getDuration(); + long secondDuration = zman2 == null ? Long.MAX_VALUE : zman2.getDuration(); + return Long.compare(firstDuration, secondDuration); + }; /** * @see java.lang.Object#toString() */ public String toString() { - StringBuffer sb = new StringBuffer(); - sb.append("\nLabel:\t\t\t").append(this.getLabel()); - sb.append("\nZman:\t\t\t").append(getZman()); - sb.append("\nDuration:\t\t\t").append(getDuration()); - sb.append("\nDescription:\t\t\t").append(getDescription()); - return sb.toString(); + return "\nLabel:\t\t\t" + this.getLabel() + + "\nZman:\t\t\t" + getZman() + + "\nDuration:\t\t\t" + getDuration() + + "\nDescription:\t\t\t" + getDescription(); } } diff --git a/src/main/java/com/kosherjava/zmanim/util/ZmanimFormatter.java b/src/main/java/com/kosherjava/zmanim/util/ZmanimFormatter.java index 449cee90..60dfcf9b 100644 --- a/src/main/java/com/kosherjava/zmanim/util/ZmanimFormatter.java +++ b/src/main/java/com/kosherjava/zmanim/util/ZmanimFormatter.java @@ -54,17 +54,17 @@ public class ZmanimFormatter { /** * the formatter for minutes as seconds. */ - private static DecimalFormat minuteSecondNF = new DecimalFormat("00"); + private static final DecimalFormat minuteSecondNF = new DecimalFormat("00"); /** * the formatter for hours. */ - private DecimalFormat hourNF; + private final DecimalFormat hourNF; /** * the formatter for minutes as milliseconds. */ - private static DecimalFormat milliNF = new DecimalFormat("000"); + private static final DecimalFormat milliNF = new DecimalFormat("000"); /** * @see #setDateFormat(SimpleDateFormat) @@ -320,7 +320,7 @@ public String getXSDateTime(Date dateTime, Calendar calendar) { // In a few cases, the time zone may be +/-hh:30. int min = offset % (60 * 60 * 1000); char posneg = hrs < 0 ? '-' : '+'; - sb.append(posneg + formatDigits(hrs) + ':' + formatDigits(min)); + sb.append(posneg).append(formatDigits(hrs)).append(':').append(formatDigits(min)); } return sb.toString(); } @@ -362,13 +362,13 @@ public String formatXSDDurationTime(Time time) { duration.append("T"); if (time.getHours() != 0) - duration.append(time.getHours() + "H"); + duration.append(time.getHours()).append("H"); if (time.getMinutes() != 0) - duration.append(time.getMinutes() + "M"); + duration.append(time.getMinutes()).append("M"); if (time.getSeconds() != 0 || time.getMilliseconds() != 0) { - duration.append(time.getSeconds() + "." + milliNF.format(time.getMilliseconds())); + duration.append(time.getSeconds()).append(".").append(milliNF.format(time.getMilliseconds())); duration.append("S"); } if (duration.length() == 1) // zero seconds @@ -422,22 +422,26 @@ public static String toXML(AstronomicalCalendar astronomicalCalendar) { boolean daylight = tz.useDaylightTime() && tz.inDaylightTime(date); StringBuilder sb = new StringBuilder("<"); - if (astronomicalCalendar.getClass().getName().equals("com.kosherjava.zmanim.AstronomicalCalendar")) { - sb.append("AstronomicalTimes"); - // TODO: use proper schema ref, and maybe build a real schema. - // output += "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "; - // output += xsi:schemaLocation="http://www.kosherjava.com/zmanim astronomical.xsd" - } else if (astronomicalCalendar.getClass().getName().equals("com.kosherjava.zmanim.ComplexZmanimCalendar")) { - sb.append("Zmanim"); - // TODO: use proper schema ref, and maybe build a real schema. - // output += "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "; - // output += xsi:schemaLocation="http://www.kosherjava.com/zmanim zmanim.xsd" - } else if (astronomicalCalendar.getClass().getName().equals("com.kosherjava.zmanim.ZmanimCalendar")) { - sb.append("BasicZmanim"); - // TODO: use proper schema ref, and maybe build a real schema. - // output += "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "; - // output += xsi:schemaLocation="http://www.kosherjava.com/zmanim basicZmanim.xsd" - } + switch (astronomicalCalendar.getClass().getName()) { + case "com.kosherjava.zmanim.AstronomicalCalendar": + sb.append("AstronomicalTimes"); + // TODO: use proper schema ref, and maybe build a real schema. + // output += "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "; + // output += xsi:schemaLocation="http://www.kosherjava.com/zmanim astronomical.xsd" + break; + case "com.kosherjava.zmanim.ComplexZmanimCalendar": + sb.append("Zmanim"); + // TODO: use proper schema ref, and maybe build a real schema. + // output += "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "; + // output += xsi:schemaLocation="http://www.kosherjava.com/zmanim zmanim.xsd" + break; + case "com.kosherjava.zmanim.ZmanimCalendar": + sb.append("BasicZmanim"); + // TODO: use proper schema ref, and maybe build a real schema. + // output += "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "; + // output += xsi:schemaLocation="http://www.kosherjava.com/zmanim basicZmanim.xsd" + break; + } sb.append(" date=\"").append(df.format(date)).append("\""); sb.append(" type=\"").append(astronomicalCalendar.getClass().getName()).append("\""); sb.append(" algorithm=\"").append(astronomicalCalendar.getAstronomicalCalculator().getCalculatorName()).append("\""); @@ -454,67 +458,68 @@ public static String toXML(AstronomicalCalendar astronomicalCalendar) { sb.append(">\n"); Method[] theMethods = astronomicalCalendar.getClass().getMethods(); - String tagName = ""; - Object value = null; - List dateList = new ArrayList(); - List durationList = new ArrayList(); - List otherList = new ArrayList(); - for (int i = 0; i < theMethods.length; i++) { - if (includeMethod(theMethods[i])) { - tagName = theMethods[i].getName().substring(3); - // String returnType = theMethods[i].getReturnType().getName(); - try { - value = theMethods[i].invoke(astronomicalCalendar, (Object[]) null); - if (value == null) {// TODO: Consider using reflection to determine the return type, not the value - otherList.add("<" + tagName + ">N/A"); - // TODO: instead of N/A, consider return proper xs:nil. - // otherList.add("<" + tagName + " xs:nil=\"true\" />"); - } else if (value instanceof Date) { - dateList.add(new Zman((Date) value, tagName)); - } else if (value instanceof Long || value instanceof Integer) {// shaah zmanis - if (((Long) value).longValue() == Long.MIN_VALUE) { - otherList.add("<" + tagName + ">N/A"); - // TODO: instead of N/A, consider return proper xs:nil. - // otherList.add("<" + tagName + " xs:nil=\"true\" />"); - } else { - durationList.add(new Zman((int) ((Long) value).longValue(), tagName)); - } - } else { // will probably never enter this block, but is present to be future-proof - otherList.add("<" + tagName + ">" + value + ""); - } - } catch (Exception e) { - e.printStackTrace(); - } - } - } - Zman zman; - Collections.sort(dateList, Zman.DATE_ORDER); - - for (int i = 0; i < dateList.size(); i++) { - zman = (Zman) dateList.get(i); - sb.append("\t<").append(zman.getLabel()).append(">"); - sb.append(formatter.formatDateTime(zman.getZman(), astronomicalCalendar.getCalendar())); - sb.append("\n"); - } - Collections.sort(durationList, Zman.DURATION_ORDER); - for (int i = 0; i < durationList.size(); i++) { - zman = (Zman) durationList.get(i); - sb.append("\t<" + zman.getLabel()).append(">"); - sb.append(formatter.format((int) zman.getDuration())).append("\n"); - } - - for (int i = 0; i < otherList.size(); i++) {// will probably never enter this block - sb.append("\t").append(otherList.get(i)).append("\n"); - } - - if (astronomicalCalendar.getClass().getName().equals("com.kosherjava.zmanim.AstronomicalCalendar")) { - sb.append(""); - } else if (astronomicalCalendar.getClass().getName().equals("com.kosherjava.zmanim.ComplexZmanimCalendar")) { - sb.append(""); - } else if (astronomicalCalendar.getClass().getName().equals("com.kosherjava.zmanim.ZmanimCalendar")) { - sb.append(""); - } + String tagName; + Object value; + List dateList = new ArrayList<>(); + List durationList = new ArrayList<>(); + List otherList = new ArrayList<>(); + for (Method theMethod : theMethods) { + if (includeMethod(theMethod)) { + tagName = theMethod.getName().substring(3); + // String returnType = theMethods[i].getReturnType().getName(); + try { + value = theMethod.invoke(astronomicalCalendar, (Object[]) null); + if (value == null) {// TODO: Consider using reflection to determine the return type, not the value + otherList.add("<" + tagName + ">N/A"); + // TODO: instead of N/A, consider return proper xs:nil. + // otherList.add("<" + tagName + " xs:nil=\"true\" />"); + } else if (value instanceof Date) { + dateList.add(new Zman((Date) value, tagName)); + } else if (value instanceof Long || value instanceof Integer) {// shaah zmanis + if (((Long) value).longValue() == Long.MIN_VALUE) { + otherList.add("<" + tagName + ">N/A"); + // TODO: instead of N/A, consider return proper xs:nil. + // otherList.add("<" + tagName + " xs:nil=\"true\" />"); + } else { + durationList.add(new Zman((int) ((Long) value).longValue(), tagName)); + } + } else { // will probably never enter this block, but is present to be future-proof + otherList.add("<" + tagName + ">" + value + ""); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + } + dateList.sort(Zman.DATE_ORDER); + + for (Zman zman : dateList) { + sb.append("\t<").append(zman.getLabel()).append(">"); + sb.append(formatter.formatDateTime(zman.getZman(), astronomicalCalendar.getCalendar())); + sb.append("\n"); + } + durationList.sort(Zman.DURATION_ORDER); + for (Zman zman : durationList) { + sb.append("\t<").append(zman.getLabel()).append(">"); + sb.append(formatter.format((int) zman.getDuration())).append("\n"); + } + + for (String s : otherList) {// will probably never enter this block + sb.append("\t").append(s).append("\n"); + } + + switch (astronomicalCalendar.getClass().getName()) { + case "com.kosherjava.zmanim.AstronomicalCalendar": + sb.append(""); + break; + case "com.kosherjava.zmanim.ComplexZmanimCalendar": + sb.append(""); + break; + case "com.kosherjava.zmanim.ZmanimCalendar": + sb.append(""); + break; + } return sb.toString(); } @@ -596,63 +601,64 @@ public static String toJSON(AstronomicalCalendar astronomicalCalendar) { .append("\""); sb.append("},\n\""); - - if (astronomicalCalendar.getClass().getName().equals("com.kosherjava.zmanim.AstronomicalCalendar")) { - sb.append("AstronomicalTimes"); - } else if (astronomicalCalendar.getClass().getName().equals("com.kosherjava.zmanim.ComplexZmanimCalendar")) { - sb.append("Zmanim"); - } else if (astronomicalCalendar.getClass().getName().equals("com.kosherjava.zmanim.ZmanimCalendar")) { - sb.append("BasicZmanim"); - } + + switch (astronomicalCalendar.getClass().getName()) { + case "com.kosherjava.zmanim.AstronomicalCalendar": + sb.append("AstronomicalTimes"); + break; + case "com.kosherjava.zmanim.ComplexZmanimCalendar": + sb.append("Zmanim"); + break; + case "com.kosherjava.zmanim.ZmanimCalendar": + sb.append("BasicZmanim"); + break; + } sb.append("\":{\n"); Method[] theMethods = astronomicalCalendar.getClass().getMethods(); - String tagName = ""; - Object value = null; - List dateList = new ArrayList(); - List durationList = new ArrayList(); - List otherList = new ArrayList(); - for (int i = 0; i < theMethods.length; i++) { - if (includeMethod(theMethods[i])) { - tagName = theMethods[i].getName().substring(3); - // String returnType = theMethods[i].getReturnType().getName(); - try { - value = theMethods[i].invoke(astronomicalCalendar, (Object[]) null); - if (value == null) {// TODO: Consider using reflection to determine the return type, not the value - otherList.add("\"" + tagName + "\":\"N/A\","); - } else if (value instanceof Date) { - dateList.add(new Zman((Date) value, tagName)); - } else if (value instanceof Long || value instanceof Integer) {// shaah zmanis - if (((Long) value).longValue() == Long.MIN_VALUE) { - otherList.add("\"" + tagName + "\":\"N/A\""); - } else { - durationList.add(new Zman((int) ((Long) value).longValue(), tagName)); - } - } else { // will probably never enter this block, but is present to be future-proof - otherList.add("\"" + tagName + "\":\"" + value + "\","); - } - } catch (Exception e) { - e.printStackTrace(); - } - } - } - Zman zman; - Collections.sort(dateList, Zman.DATE_ORDER); - for (int i = 0; i < dateList.size(); i++) { - zman = (Zman) dateList.get(i); - sb.append("\t\"").append(zman.getLabel()).append("\":\""); - sb.append(formatter.formatDateTime(zman.getZman(), astronomicalCalendar.getCalendar())); - sb.append("\",\n"); - } - Collections.sort(durationList, Zman.DURATION_ORDER); - for (int i = 0; i < durationList.size(); i++) { - zman = (Zman) durationList.get(i); - sb.append("\t\"" + zman.getLabel()).append("\":\""); - sb.append(formatter.format((int) zman.getDuration())).append("\",\n"); - } - - for (int i = 0; i < otherList.size(); i++) {// will probably never enter this block - sb.append("\t").append(otherList.get(i)).append("\n"); - } + String tagName; + Object value; + List dateList = new ArrayList<>(); + List durationList = new ArrayList<>(); + List otherList = new ArrayList<>(); + for (Method theMethod : theMethods) { + if (includeMethod(theMethod)) { + tagName = theMethod.getName().substring(3); + // String returnType = theMethods[i].getReturnType().getName(); + try { + value = theMethod.invoke(astronomicalCalendar, (Object[]) null); + if (value == null) {// TODO: Consider using reflection to determine the return type, not the value + otherList.add("\"" + tagName + "\":\"N/A\","); + } else if (value instanceof Date) { + dateList.add(new Zman((Date) value, tagName)); + } else if (value instanceof Long || value instanceof Integer) {// shaah zmanis + if (((Long) value).longValue() == Long.MIN_VALUE) { + otherList.add("\"" + tagName + "\":\"N/A\""); + } else { + durationList.add(new Zman((int) ((Long) value).longValue(), tagName)); + } + } else { // will probably never enter this block, but is present to be future-proof + otherList.add("\"" + tagName + "\":\"" + value + "\","); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + } + dateList.sort(Zman.DATE_ORDER); + for (Zman zman : dateList) { + sb.append("\t\"").append(zman.getLabel()).append("\":\""); + sb.append(formatter.formatDateTime(zman.getZman(), astronomicalCalendar.getCalendar())); + sb.append("\",\n"); + } + durationList.sort(Zman.DURATION_ORDER); + for (Zman zman : durationList) { + sb.append("\t\"").append(zman.getLabel()).append("\":\""); + sb.append(formatter.format((int) zman.getDuration())).append("\",\n"); + } + + for (String s : otherList) {// will probably never enter this block + sb.append("\t").append(s).append("\n"); + } sb.setLength(sb.length() - 2); sb.append("}\n}"); return sb.toString(); @@ -665,10 +671,10 @@ public static String toJSON(AstronomicalCalendar astronomicalCalendar) { * @return if the method should be included in serialization */ private static boolean includeMethod(Method method) { - List methodWhiteList = new ArrayList(); + List methodWhiteList = new ArrayList<>(); // methodWhiteList.add("getName"); - List methodBlackList = new ArrayList(); + List methodBlackList = new ArrayList<>(); // methodBlackList.add("getGregorianChange"); if (methodWhiteList.contains(method.getName())) @@ -681,9 +687,6 @@ private static boolean includeMethod(Method method) { if (!method.getName().startsWith("get")) return false; - if (method.getReturnType().getName().endsWith("Date") || method.getReturnType().getName().endsWith("long")) { - return true; - } - return false; - } + return method.getReturnType().getName().endsWith("Date") || method.getReturnType().getName().endsWith("long"); + } }