1 /*
2 * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit.
3 *
4 * Based in London, we are world leaders in the design and development
5 * of bespoke applications for the securities financing markets.
6 *
7 * <a href="http://www.objectlab.co.uk/open">Click here to learn more</a>
8 * ___ _ _ _ _ _
9 * / _ \| |__ (_) ___ ___| |_| | __ _| |__
10 * | | | | '_ \| |/ _ \/ __| __| | / _` | '_ \
11 * | |_| | |_) | | __/ (__| |_| |__| (_| | |_) |
12 * \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
13 * |__/
14 *
15 * www.ObjectLab.co.uk
16 *
17 * $Id$
18 *
19 * Copyright 2006 the original author or authors.
20 *
21 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
22 * use this file except in compliance with the License. You may obtain a copy of
23 * the License at
24 *
25 * http://www.apache.org/licenses/LICENSE-2.0
26 *
27 * Unless required by applicable law or agreed to in writing, software
28 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
29 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
30 * License for the specific language governing permissions and limitations under
31 * the License.
32 */
33 package net.objectlab.kit.datecalc.common;
34
35 import java.util.Calendar;
36 import java.util.Date;
37
38 /**
39 * Immutable Working Week, default is Mon->Friday.
40 *
41 * @author Benoit Xhenseval
42 *
43 */
44 public class WorkingWeek {
45 private static final byte MONDAY = 1;
46
47 private static final byte TUESDAY = 2;
48
49 private static final byte WEDNESDAY = 4;
50
51 private static final byte THURSDAY = 8;
52
53 private static final byte FRIDAY = 16;
54
55 private static final byte SATURDAY = 32;
56
57 private static final byte SUNDAY = 64;
58
59 private static final byte DEFAULT_WORKING_DAYS = (byte) (MONDAY + TUESDAY + WEDNESDAY + THURSDAY + FRIDAY);
60
61 private static final byte ARABIC_WORKING_DAYS = (byte) (MONDAY + TUESDAY + WEDNESDAY + THURSDAY + SUNDAY);
62
63 private static final byte[] WORKING_WEEK_DAYS_OFFSET = new byte[] { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY };
64
65 public static final WorkingWeek DEFAULT = new WorkingWeek();
66
67 public static final WorkingWeek ARABIC_WEEK = new WorkingWeek(ARABIC_WORKING_DAYS);
68
69 // -----------------------------------------------------------------------
70 //
71 // ObjectLab, world leaders in the design and development of bespoke
72 // applications for the securities financing markets.
73 // www.ObjectLab.co.uk
74 //
75 // -----------------------------------------------------------------------
76
77 /**
78 * working days: 1 Monday, 2 Tuesday, 4 Wednesday, 8 Thursday, 16 Friday, 32
79 * Saturday, 64 Sunday So Monday-Friday= 1+2+4+8+16 = 31
80 */
81 private byte workingDays = DEFAULT_WORKING_DAYS;
82
83 /**
84 * Default Working Week Monday -> Friday.
85 */
86 public WorkingWeek() {
87 this(DEFAULT_WORKING_DAYS);
88 }
89
90 protected WorkingWeek(final byte workingDays) {
91 this.workingDays = workingDays;
92 }
93
94 /**
95 * Create a new calendar with the intersection of WORKING days.
96 * e.g. if normal and arabic calendars are intersected, the week is 3 days: Fri-Sun.
97 * @param ww
98 * @return a new Working week
99 * @since 1.4.0
100 */
101 public WorkingWeek intersection(final WorkingWeek ww) {
102 final byte combined = (byte) (this.workingDays & ww.workingDays);
103 return new WorkingWeek(combined);
104 }
105
106 public boolean isWorkingDayFromCalendar(final int dayOfWeek) {
107 final int day = adjustDay(dayOfWeek);
108 return (WORKING_WEEK_DAYS_OFFSET[day] & workingDays) != 0;
109 }
110
111 public boolean isWorkingDay(final Date date) {
112 return isWorkingDay(Utils.getCal(date));
113 }
114
115 public boolean isWorkingDay(final Calendar cal) {
116 return cal != null ? isWorkingDayFromCalendar(cal.get(Calendar.DAY_OF_WEEK)) : false;
117 }
118
119 protected int adjustDay(final int dayOfWeek) {
120 return dayOfWeek - 1;
121 }
122
123 /**
124 * If the value for the given day has changed, return a NEW WorkingWeek.
125 *
126 * @param working
127 * true if working day
128 * @param dayOfWeek
129 * e.g. Calendar.MONDAY, Calendar.TUESDAY, etc
130 * @return a new instance of a <code>WorkingWeek</code> with the working
131 * day set
132 */
133 public WorkingWeek withWorkingDayFromCalendar(final boolean working, final int dayOfWeek) {
134 final int day = adjustDay(dayOfWeek);
135 WorkingWeek ret = this;
136 if (working && !isWorkingDayFromCalendar(dayOfWeek)) {
137 ret = new WorkingWeek((byte) (workingDays + WORKING_WEEK_DAYS_OFFSET[day]));
138 } else if (!working && isWorkingDayFromCalendar(dayOfWeek)) {
139 ret = new WorkingWeek((byte) (workingDays - WORKING_WEEK_DAYS_OFFSET[day]));
140 }
141 return ret;
142 }
143
144 public byte getWorkingDays() {
145 return workingDays;
146 }
147 }
148
149 /*
150 * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit.
151 *
152 * Based in London, we are world leaders in the design and development
153 * of bespoke applications for the securities financing markets.
154 *
155 * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
156 * ___ _ _ _ _ _
157 * / _ \| |__ (_) ___ ___| |_| | __ _| |__
158 * | | | | '_ \| |/ _ \/ __| __| | / _` | '_ \
159 * | |_| | |_) | | __/ (__| |_| |__| (_| | |_) |
160 * \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
161 * |__/
162 *
163 * www.ObjectLab.co.uk
164 */