AuctionDateGenerator.java 4.0 KB

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