001/*
002 * Copyright (c) 2009 The openGion Project.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied. See the License for the specific language
014 * governing permissions and limitations under the License.
015 */
016package org.opengion.hayabusa.resource;
017
018import static org.opengion.fukurou.system.HybsConst.CR ;                                // 6.1.0.0 (2014/12/26)
019import static org.opengion.fukurou.system.HybsConst.BUFFER_MIDDLE;      // 6.1.0.0 (2014/12/26) refactoring
020
021import java.util.Calendar;
022
023/**
024 * 事業所(CDJGS) 毎の休日カレンダデータオブジェクトです。
025 *
026 * カレンダデータは、指定の事業所に関して、すべての休日情報を持っています。
027 * 元のカレンダテーブル(GE13)の 1日(DY1)~31日(DY31)までの日付け欄に対して、
028 * 休日日付けの 年月日 に対する、休日かどうかを判断できるだけの情報を保持します。
029 * 具体的には、年月日に対する Set を持つことになります。
030 *
031 * @og.rev 3.6.0.0 (2004/09/17) 新規作成
032 * @og.group リソース管理
033 *
034 * @version  4.0
035 * @author   Hiroki Nakamura
036 * @since    JDK5.0,
037 */
038public abstract class AbstractCalendarPGData implements CalendarData {
039
040        /**
041         * このコンストラクタは、他のパッケージから呼び出せないように、
042         * パッケージプライベートにしておきます。
043         *
044         */
045        AbstractCalendarPGData() {
046                // ここでは処理を行いません。
047        }
048
049        /**
050         * 指定の日付けから、範囲の間に、本日を含むかどうかを返します。
051         * 指定の日付けが、キャッシュしているデータの最大と最小の間に
052         * 存在しない場合は、常に false になります。
053         * 判定は、年月日の項目のみで比較し、時分秒は無視します。
054         *
055         * @og.rev 3.7.1.1 (2005/05/31) 新規追加
056         * @og.rev 3.8.8.6 (2007/04/20) today を毎回求めます。(キャッシュ対策)
057         *
058         * @param       day     指定の開始日付け
059         * @param       scope   範囲の日数
060         *
061         * @return      本日:true それ以外:false
062         */
063        @Override       // CalendarData
064        public boolean isContainedToday( final Calendar day,final int scope ) {
065                final boolean rtnFlag;
066
067                final Calendar today = Calendar.getInstance();
068                today.set( Calendar.HOUR_OF_DAY ,12 );  // 昼にセット
069                today.set( Calendar.MINUTE ,0 );
070                today.set( Calendar.SECOND ,0 );
071
072                if( scope == 1 ) {
073                        // false の確率の高い方から、比較します。
074                        rtnFlag = day.get( Calendar.DATE )  == today.get( Calendar.DATE  ) &&
075                                                day.get( Calendar.MONTH ) == today.get( Calendar.MONTH ) &&
076                                                day.get( Calendar.YEAR )  == today.get( Calendar.YEAR  ) ;
077                }
078                else {
079                        final Calendar next = (Calendar)day.clone();
080                        next.add( Calendar.DATE,scope );
081                        rtnFlag = day.before( today ) && next.after( today ) ;
082                }
083                return rtnFlag ;
084        }
085
086        /**
087         * 指定の開始、終了日の期間に、平日(稼働日)が何日あるか求めます。
088         * start と end が、リスト範囲外の場合は、エラーとします。
089         * 開始と終了が同じ日の場合は、1を返します。
090         *
091         * @param       start   開始日付け(稼働日に含めます)
092         * @param       end             終了日付け(稼働日に含めます)
093         *
094         * @return      稼働日数
095         *
096         */
097        @Override       // CalendarData
098        public int getKadoubisu( final Calendar start,final Calendar end ) {
099                final long diff = start.getTimeInMillis() - end.getTimeInMillis() ;
100                final int dayCount = (int)(diff/(1000*60*60*24)) + 1;   // end も含むので+1必要
101
102                final Calendar tempDay = (Calendar)(start.clone());
103                int su = 0 ;
104                while( ! isHoliday( tempDay ) ) {
105                        su++ ;
106                        tempDay.add(Calendar.DATE, 1);          // 日にちを進める。
107                }
108
109                final int count = ( dayCount - su ) / 7 + 1;
110
111                return dayCount - count ;
112        }
113
114        /**
115         * 指定の開始日に平日のみ期間を加算して求められる日付けを返します。
116         * これは、実稼働日計算に使用します。
117         * 例えば、start=20040810 , span=5 で、休日がなければ、10,11,12,13,14 となり、
118         * 20040815 を返します。
119         * 指定の日付けや、期間加算後の日付けが、キャッシュしているデータの
120         * 最大と最小の間に存在しない場合は、エラーとします。
121         *
122         * @param       start   開始日付け(YYYYMMDD 形式)
123         * @param       span    稼動期間
124         *
125         * @return      開始日から稼動期間を加算した日付け(当日を含む)
126         *
127         */
128        @Override       // CalendarData
129        public Calendar getAfterDay( final Calendar start,final int span ) {
130                final Calendar tempDay = (Calendar)(start.clone());
131                int suSpan = span ;
132                while( suSpan > 0 ) {
133                        if( ! isHoliday( tempDay ) ) { suSpan--; }
134                        tempDay.add(Calendar.DATE, 1);          // 日にちを進める。
135                }
136                return tempDay ;
137        }
138
139        /**
140         * オブジェクトの識別子として、詳細なカレンダ情報を返します。
141         *
142         * @return  詳細なカレンダ情報
143         * @og.rtnNotNull
144         */
145        @Override       // Object
146        public String toString() {
147                final StringBuilder rtn = new StringBuilder( BUFFER_MIDDLE );
148                rtn.append( "CLASS   : ").append( getClass().getName() ).append( CR );  // クラス名
149
150                return rtn.toString();
151        }
152}