View Javadoc
1   /*
2    * (c) Copyright 2006-2020 by rapiddweller GmbH & Volker Bergmann. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, is permitted under the terms of the
6    * GNU General Public License.
7    *
8    * For redistributing this software or a derivative work under a license other
9    * than the GPL-compatible Free Software License as defined by the Free
10   * Software Foundation or approved by OSI, you must first obtain a commercial
11   * license to this software product from rapiddweller GmbH & Volker Bergmann.
12   *
13   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14   * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
15   * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
17   * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24   * POSSIBILITY OF SUCH DAMAGE.
25   */
26  
27  package com.rapiddweller.benerator.primitive.datetime;
28  
29  import com.rapiddweller.common.Assert;
30  import com.rapiddweller.common.TimeUtil;
31  import com.rapiddweller.common.validator.bean.AbstractConstraintValidator;
32  
33  import javax.validation.ConstraintValidatorContext;
34  import java.util.Arrays;
35  import java.util.Calendar;
36  import java.util.Date;
37  
38  /**
39   * Filters {@link Date}s by their day of week.
40   * All days of the week are accepted by default.
41   * Attention: The weekday array begins with Monday (as defined in ISO_8601),
42   * not with Sunday (as used in {@link java.util.Calendar}).<br/>
43   * <br/>
44   * Created at 23.09.2009 17:51:52
45   *
46   * @author Volker Bergmann
47   * @see <a href="http://en.wikipedia.org/wiki/ISO_8601#Week_dates">ISO 8601</a>
48   * @since 0.6.0
49   */
50  public class DayOfWeekValidator extends AbstractConstraintValidator<DayOfWeek, Date> {
51  
52    /**
53     * holds a flag for each weekday that tells if it is accepted.
54     */
55    private final boolean[] daysOfWeekAccepted;
56  
57    /**
58     * Instantiates a new Day of week validator.
59     */
60    public DayOfWeekValidator() {
61      this.daysOfWeekAccepted = new boolean[7];
62      Arrays.fill(daysOfWeekAccepted, true);
63    }
64  
65    // properties ------------------------------------------------------------------------------------------------------
66  
67    /**
68     * Sets days of week accepted.
69     *
70     * @param daysOfWeekAccepted the days of week accepted
71     */
72    public void setDaysOfWeekAccepted(boolean... daysOfWeekAccepted) {
73      Assert.equals(7, daysOfWeekAccepted.length, getClass().getName() + ".day");
74      System.arraycopy(daysOfWeekAccepted, 0, this.daysOfWeekAccepted, 0, 7);
75    }
76  
77    /**
78     * Sets weekdays accepted.
79     *
80     * @param weekdayAccepted the weekday accepted
81     */
82    public void setWeekdaysAccepted(boolean weekdayAccepted) {
83      Arrays.fill(daysOfWeekAccepted, 0, 5, weekdayAccepted);
84    }
85  
86    /**
87     * Sets weekends accepted.
88     *
89     * @param weekendAccepted the weekend accepted
90     */
91    public void setWeekendsAccepted(boolean weekendAccepted) {
92      daysOfWeekAccepted[6] = weekendAccepted;
93      daysOfWeekAccepted[5] = weekendAccepted;
94    }
95  
96    @Override
97    public void initialize(DayOfWeek params) {
98      Arrays.fill(daysOfWeekAccepted, false);
99      for (int dayOfWeek : params.daysOfWeekAccepted()) {
100       daysOfWeekAccepted[isoDayOfWeek(dayOfWeek) - 1] = true;
101     }
102   }
103 
104   @Override
105   public boolean isValid(Date candidate, ConstraintValidatorContext ctx) {
106     int isoDayOfWeek = isoDayOfWeek(candidate);
107     return daysOfWeekAccepted[isoDayOfWeek - 1];
108   }
109 
110   /**
111    * Iso day of week int.
112    *
113    * @param candidate the candidate
114    * @return the int
115    */
116   static int isoDayOfWeek(Date candidate) {
117     Calendar calendar = TimeUtil.calendar(candidate);
118     int javaDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
119     return isoDayOfWeek(javaDayOfWeek);
120   }
121 
122   /**
123    * Calculates the day of the week (1=monday - 7=sunday) according to ISO 8601
124    * for the day of week returned by Calendar.get(DAY_OF_WEEK).
125    */
126   private static int isoDayOfWeek(int calendarDayOfWeek) {
127     return (calendarDayOfWeek == Calendar.SUNDAY ? 7 : calendarDayOfWeek - 1);
128   }
129 
130 }