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 org.opengion.hayabusa.common.HybsSystem;
019
020import java.util.Calendar;
021
022/**
023 * 事業所(CDJGS) 毎の休日カレンダデータオブジェクトです。
024 *
025 * カレンダデータは、指定の事業所に関して、すべての休日情報を持っています。
026 * 元のカレンダテーブル(GE13)の 1日(DY1)~31日(DY31)までの日付け欄に対して、
027 * 休日日付けの 年月日 に対する、休日かどうかを判断できるだけの情報を保持します。
028 * 具体的には、年月日に対する Set を持つことになります。
029 *
030 * @og.rev 3.6.0.0 (2004/09/17) 新規作成
031 * @og.group リソース管理
032 *
033 * @version  4.0
034 * @author   Hiroki Nakamura
035 * @since    JDK5.0,
036 */
037public abstract class AbstractCalendarPGData implements CalendarData {
038
039        /**
040         * このコンストラクタは、他のパッケージから呼び出せないように、
041         * パッケージプライベートにしておきます。
042         *
043         */
044        AbstractCalendarPGData() {
045                // ここでは処理を行いません。
046        }
047
048        /**
049         * 指定の日付けから、範囲の間に、本日を含むかどうかを返します。
050         * 指定の日付けが、キャッシュしているデータの最大と最小の間に
051         * 存在しない場合は、常に false になります。
052         * 判定は、年月日の項目のみで比較し、時分秒は無視します。
053         *
054         * @og.rev 3.7.1.1 (2005/05/31) 新規追加
055         * @og.rev 3.8.8.6 (2007/04/20) today を毎回求めます。(キャッシュ対策)
056         *
057         * @param       day     指定の開始日付け
058         * @param       scope   範囲の日数
059         *
060         * @return      本日:true それ以外:false
061         */
062        public boolean isContainedToday( final Calendar day,final int scope ) {
063                final boolean rtnFlag;
064
065                Calendar today = Calendar.getInstance();
066                today.set( Calendar.HOUR_OF_DAY ,12 );  // 昼にセット
067                today.set( Calendar.MINUTE ,0 );
068                today.set( Calendar.SECOND ,0 );
069
070                if( scope == 1 ) {
071                        // false の確率の高い方から、比較します。
072                        rtnFlag = day.get( Calendar.DATE )  == today.get( Calendar.DATE  ) &&
073                                                day.get( Calendar.MONTH ) == today.get( Calendar.MONTH ) &&
074                                                day.get( Calendar.YEAR )  == today.get( Calendar.YEAR  ) ;
075                }
076                else {
077                        Calendar next = (Calendar)day.clone();
078                        next.add( Calendar.DATE,scope );
079                        rtnFlag = day.before( today ) && next.after( today ) ;
080                }
081                return rtnFlag ;
082        }
083
084        /**
085         * 指定の開始、終了日の期間に、平日(稼働日)が何日あるか求めます。
086         * start と end が、リスト範囲外の場合は、エラーとします。
087         * 開始と終了が同じ日の場合は、1を返します。
088         *
089         * @param       start   開始日付け(稼働日に含めます)
090         * @param       end             終了日付け(稼働日に含めます)
091         *
092         * @return      稼働日数
093         *
094         */
095        public int getKadoubisu( final Calendar start,final Calendar end ) {
096                long diff = start.getTimeInMillis() - end.getTimeInMillis() ;
097                int dayCount = (int)(diff/(1000*60*60*24)) + 1; // end も含むので+1必要
098
099                Calendar tempDay = (Calendar)(start.clone());
100                int su = 0 ;
101                while( ! isHoliday( tempDay ) ) {
102                        su++ ;
103                        tempDay.add(Calendar.DATE, 1);          // 日にちを進める。
104                }
105
106                int count = ( dayCount - su ) / 7 + 1;
107
108                return dayCount - count ;
109        }
110
111        /**
112         * 指定の開始日に平日のみ期間を加算して求められる日付けを返します。
113         * これは、実稼働日計算に使用します。
114         * 例えば、start=20040810 , span=5 で、休日がなければ、10,11,12,13,14 となり、
115         * 20040815 を返します。
116         * 指定の日付けや、期間加算後の日付けが、キャッシュしているデータの
117         * 最大と最小の間に存在しない場合は、エラーとします。
118         *
119         * @param       start   開始日付け(YYYYMMDD 形式)
120         * @param       span    稼動期間
121         *
122         * @return      開始日から稼動期間を加算した日付け(当日を含む)
123         *
124         */
125        public Calendar getAfterDay( final Calendar start,final int span ) {
126                Calendar tempDay = (Calendar)(start.clone());
127                int suSpan = span ;
128                while( suSpan > 0 ) {
129                        if( ! isHoliday( tempDay ) ) { suSpan--; }
130                        tempDay.add(Calendar.DATE, 1);          // 日にちを進める。
131                }
132                return tempDay ;
133        }
134
135        /**
136         * オブジェクトの識別子として,詳細なカレンダ情報を返します。
137         *
138         * @return  詳細なカレンダ情報
139         */
140        @Override
141        public String toString() {
142                StringBuilder rtn = new StringBuilder( HybsSystem.BUFFER_MIDDLE );
143                rtn.append( "CLASS   : ").append( getClass().getName() ).append( HybsSystem.CR );       // クラス名
144
145                return rtn.toString();
146        }
147}