SHARE
TWEET

com.l2jserver.gameserver.model.itemauction.AuctionDateGenera

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