AuctionDateGenerator.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package com.l2jserver.gameserver.model.itemauction;
  2. /*
  3. * This program is free software: you can redistribute it and/or modify it under
  4. * the terms of the GNU General Public License as published by the Free Software
  5. * Foundation, either version 3 of the License, or (at your option) any later
  6. * version.
  7. *
  8. * This program is distributed in the hope that it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  11. * details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. import java.util.Calendar;
  17. import java.util.concurrent.TimeUnit;
  18. import com.l2jserver.gameserver.templates.StatsSet;
  19. /**
  20. *
  21. * @author Forsaiken
  22. *
  23. */
  24. public final class AuctionDateGenerator
  25. {
  26. public static final String FIELD_INTERVAL = "interval";
  27. public static final String FIELD_DAY_OF_WEEK = "day_of_week";
  28. public static final String FIELD_HOUR_OF_DAY = "hour_of_day";
  29. public static final String FIELD_MINUTE_OF_HOUR = "minute_of_hour";
  30. private static final long MILLIS_IN_WEEK = TimeUnit.MILLISECONDS.convert(7, TimeUnit.DAYS);
  31. private final Calendar _calendar;
  32. private int _interval;
  33. private int _day_of_week;
  34. private int _hour_of_day;
  35. private int _minute_of_hour;
  36. public AuctionDateGenerator(final StatsSet config) throws IllegalArgumentException
  37. {
  38. _calendar = Calendar.getInstance();
  39. _interval = config.getInteger(FIELD_INTERVAL, -1);
  40. _day_of_week = config.getInteger(FIELD_DAY_OF_WEEK, -1);
  41. _hour_of_day = config.getInteger(FIELD_HOUR_OF_DAY, -1);
  42. _minute_of_hour = config.getInteger(FIELD_MINUTE_OF_HOUR, -1);
  43. checkDayOfWeek(-1);
  44. checkHourOfDay(-1);
  45. checkMinuteOfHour(0);
  46. }
  47. public synchronized final long nextDate(final long date)
  48. {
  49. _calendar.setTimeInMillis(date);
  50. _calendar.set(Calendar.MILLISECOND, 0);
  51. _calendar.set(Calendar.SECOND, 0);
  52. _calendar.set(Calendar.MINUTE, _minute_of_hour);
  53. _calendar.set(Calendar.HOUR_OF_DAY, _hour_of_day);
  54. if (_day_of_week > 0)
  55. {
  56. _calendar.set(Calendar.DAY_OF_WEEK, _day_of_week);
  57. return calcDestTime(_calendar.getTimeInMillis(), date, MILLIS_IN_WEEK);
  58. }
  59. return calcDestTime(_calendar.getTimeInMillis(), date, TimeUnit.MILLISECONDS.convert(_interval, TimeUnit.DAYS));
  60. }
  61. private final long calcDestTime(long time, final long date, final long add)
  62. {
  63. if (time < date)
  64. {
  65. time += ((date - time) / add) * add;
  66. if (time < date)
  67. time += add;
  68. }
  69. return time;
  70. }
  71. private final void checkDayOfWeek(final int defaultValue)
  72. {
  73. if (_day_of_week < 1 || _day_of_week > 7)
  74. {
  75. if (defaultValue == -1 && _interval < 1)
  76. throw new IllegalArgumentException("Illegal params for '" + FIELD_DAY_OF_WEEK + "': " + (_day_of_week == -1 ? "not found" : _day_of_week));
  77. _day_of_week = defaultValue;
  78. }
  79. else if (_interval > 1)
  80. throw new IllegalArgumentException("Illegal params for '" + FIELD_INTERVAL +"' and '" + FIELD_DAY_OF_WEEK + "': you can use only one, not both");
  81. }
  82. private final void checkHourOfDay(final int defaultValue)
  83. {
  84. if (_hour_of_day < 0 || _hour_of_day > 23)
  85. {
  86. if (defaultValue == -1)
  87. throw new IllegalArgumentException("Illegal params for '" + FIELD_HOUR_OF_DAY + "': " + (_hour_of_day == -1 ? "not found" : _hour_of_day));
  88. _hour_of_day = defaultValue;
  89. }
  90. }
  91. private final void checkMinuteOfHour(final int defaultValue)
  92. {
  93. if (_minute_of_hour < 0 || _minute_of_hour > 59)
  94. {
  95. if (defaultValue == -1)
  96. throw new IllegalArgumentException("Illegal params for '" + FIELD_MINUTE_OF_HOUR + "': " + (_minute_of_hour == -1 ? "not found" : _minute_of_hour));
  97. _minute_of_hour = defaultValue;
  98. }
  99. }
  100. }