content: package com.tyme.rabbyung;
  
  import com.tyme.AbstractTyme;
  import com.tyme.culture.Zodiac;
  
  import java.util.*;
  
  /**
   * 藏历月，仅支持藏历1950年十二月至藏历2050年十二月
   *
   * @author 6tail
   */
  public class RabByungMonth extends AbstractTyme {
    public static final String[] NAMES = {"正月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"};
    public static final String[] ALIAS = {"神变月", "苦行月", "具香月", "萨嘎月", "作净月", "明净月", "具醉月", "具贤月", "天降月", "持众月", "庄严月", "满意月"};
  
    /**
     * 藏历年
     */
    protected RabByungYear year;
  
    /**
     * 月
     */
    protected int month;
  
    /**
     * 是否闰月
     */
    protected boolean leap;
  
    /**
     * 位于当年的索引，0-12
     */
    protected int indexInYear;
  
    protected static final Map<Integer, int[]> DAYS = new HashMap<>();
  
    static {
      int y = 1950;
      int m = 11;
      String[] years = "2c>,182[>1:2TA4ZI=n1E2Bk1J2Ff3Mk503Oc62g=,172^>1:2XA1>2UE2Bo1I2Fj3Lo62Fb3Mf5,03N^72b=1:2]A1>2ZF1B2VI2Em1K2Fe,2Lh1R3Na603P\\:172Y>1;2UB2=m2Dq1J2Eh,2Kl1Q3Me603Pa:172^>1;2YA2=p1C2UI,2Dk2Jp3QEc3Mi603Pf:3L[72b?1:2]A1<2UB2XH,2Cn1I2Ei1L2Ie1Q3Na703Q\\:2`@1;2XA,4\\H;m1B2TI2Em1L2Ij1Q3Nf603Q`903QW:,2[@1;2TB2XI1E4TMAh2Io3RFe3Mj603Pc803Q[;,2^?1;2WA2>q1E2Bm1I2Fi1M2Hc3Of70,3P^82a>1:2[A1>2WE1B2TI2Fm1L2Hf3Ni6,03Oa703PZ:3`A62V>4]F;q1B4YJ>l2Eq1L2Gi3Ml5,03Nd603Q_9172[>1;2XB2>p1E2VK2Fl,1K2Fc3Mh603Pc9172`>1;2\\B1>2UD2=j2En,1J2Fg3Mm62Ib3Pj;3M_703R[:2`B1=2YB2=n,1C2TI2Fk1L2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2@q1G2Cl2Ip1P2K_".split(",", -1);
      for (String ys : years) {
        while (!ys.isEmpty()) {
          int len = ys.charAt(0) - '0';
          int[] data = new int[len];
          for (int i = 0; i < len; i++) {
            data[i] = ys.charAt(i + 1) - '5' - 30;
          }
          DAYS.put(y * 13 + m, data);
          m++;
          ys = ys.substring(len + 1);
        }
        y++;
        m = 0;
      }
    }
  
    public RabByungMonth(RabByungYear year, int month) {
      if (month == 0 || month > 12 || month < -12) {
        throw new IllegalArgumentException(String.format("illegal rab-byung month: %d", month));
      }
      int y = year.getYear();
      if (y < 1950 || y > 2050) {
        throw new IllegalArgumentException(String.format("rab-byung year %d must between 1950 and 2050", y));
      }
      int m = Math.abs(month);
      if (y == 1950 && m < 12) {
        throw new IllegalArgumentException(String.format("month %d must be 12 in rab-byung year %d", month, y));
      }
      boolean leap = month < 0;
      int leapMonth = year.getLeapMonth();
      if (leap && m != leapMonth) {
        throw new IllegalArgumentException(String.format("illegal leap month %d in rab-byung year %d", m, y));
      }
      this.year = year;
      this.month = m;
      this.leap = leap;
      // 位于当年的索引
      int index = m - 1;
      if (leap || (0 < leapMonth && leapMonth < m)) {
        index += 1;
      }
      indexInYear = index;
    }
  
    /**
     * 从藏历年月初始化
     *
     * @param year  藏历年
     * @param month 藏历月，闰月为负
     */
    public RabByungMonth(int year, int month) {
      this(RabByungYear.fromYear(year), month);
    }
  
    public RabByungMonth(int rabByungIndex, RabByungElement element, Zodiac zodiac, int month) {
      this(RabByungYear.fromElementZodiac(rabByungIndex, element, zodiac), month);
    }
  
    /**
     * 从藏历年月初始化
     *
     * @param year  藏历年
     * @param month 藏历月，闰月为负
     * @return 藏历月
     */
    public static RabByungMonth fromYm(int year, int month) {
      return new RabByungMonth(year, month);
    }
  
    public static RabByungMonth fromElementZodiac(int rabByungIndex, RabByungElement element, Zodiac zodiac, int month) {
      return new RabByungMonth(rabByungIndex, element, zodiac, month);
    }
  
    /**
     * 藏历年
     *
     * @return 藏历年
     */
    public RabByungYear getRabByungYear() {
      return year;
    }
  
    /**
     * 年
     *
     * @return 年
     */
    public int getYear() {
      return year.getYear();
    }
  
    /**
     * 月
     *
     * @return 月
     */
    public int getMonth() {
      return month;
    }
  
    /**
     * 月
     *
     * @return 月，当月为闰月时，返回负数
     */
    public int getMonthWithLeap() {
      return leap ? -month : month;
    }
  
    /**
     * 位于当年的索引
     *
     * @return 索引(0-12)
     */
    public int getIndexInYear() {
      return indexInYear;
    }
  
    /**
     * 是否闰月
     *
     * @return true/false
     */
    public boolean isLeap() {
      return leap;
    }
  
    /**
     * 名称
     *
     * @return 名称
     */
    public String getName() {
      return (leap ? "闰" : "") + NAMES[month - 1];
    }
  
    /**
     * 别名
     *
     * @return 别名
     */
    public String getAlias() {
      return (leap ? "闰" : "") + ALIAS[month - 1];
    }
  
    @Override
    public String toString() {
      return year + getName();
    }
  
    public RabByungMonth next(int n) {
      if (n == 0) {
        return fromYm(getYear(), getMonthWithLeap());
      }
      int m = indexInYear + 1 + n;
      RabByungYear y = year;
      if (n > 0) {
        int monthCount = y.getMonthCount();
        while (m > monthCount) {
          m -= monthCount;
          y = y.next(1);
          monthCount = y.getMonthCount();
        }
      } else {
        while (m <= 0) {
          y = y.next(-1);
          m += y.getMonthCount();
        }
      }
      boolean leap = false;
      int leapMonth = y.getLeapMonth();
      if (leapMonth > 0) {
        if (m == leapMonth + 1) {
          leap = true;
        }
        if (m > leapMonth) {
          m--;
        }
      }
      return fromYm(y.getYear(), leap ? -m : m);
    }
  
    /**
     * 首日
     *
     * @return 藏历日
     */
    public RabByungDay getFirstDay() {
      return new RabByungDay(this, 1);
    }
  
    /**
     * 本月的藏历日列表
     *
     * @return 藏历日列表
     */
    public List<RabByungDay> getDays() {
      List<RabByungDay> l = new ArrayList<>();
      List<Integer> missDays = getMissDays();
      List<Integer> leapDays = getLeapDays();
      for (int i = 1; i < 31; i++) {
        if (missDays.contains(i)) {
          continue;
        }
        l.add(new RabByungDay(this, i));
        if (leapDays.contains(i)) {
          l.add(new RabByungDay(this, -i));
        }
      }
      return l;
    }
  
    /**
     * 当月天数
     *
     * @return 数量
     */
    public int getDayCount() {
      return 30 + getLeapDays().size() - getMissDays().size();
    }
  
    /**
     * 特殊日子列表
     *
     * @return 特殊日子列表，闰日为正，缺日为负
     */
    public List<Integer> getSpecialDays() {
      List<Integer> l = new ArrayList<>();
      int[] days = DAYS.get(getYear() * 13 + getIndexInYear());
      if (null == days) {
        return l;
      }
      for (int d : days) {
        l.add(d);
      }
      return l;
    }
  
    /**
     * 闰日列表
     *
     * @return 闰日列表
     */
    public List<Integer> getLeapDays() {
      List<Integer> l = new ArrayList<>();
      List<Integer> days = getSpecialDays();
      for (int d : days) {
        if (d > 0) {
          l.add(d);
        }
      }
      return l;
    }
  
    /**
     * 缺日列表
     *
     * @return 缺日列表
     */
    public List<Integer> getMissDays() {
      List<Integer> l = new ArrayList<>();
      List<Integer> days = getSpecialDays();
      for (int d : days) {
        if (d < 0) {
          l.add(-d);
        }
      }
      return l;
    }
  }
  