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.fukurou.util; 017 018import java.util.List; 019import java.util.ArrayList; 020 021/** 022 * HybsTimerTaskManager.java は、HybsTimerTask オブジェクトを管理しているクラスです。 023 * 024 * HybsTimerTask は、タスクとして一定間隔で実行されます。オブジェクトが生成されてから、 025 * 実行中のタスクを、このクラスで管理することにより、個別に削除、生成、全件終了等の制御が 026 * 可能になります。 027 * 028 * @version 4.0 029 * @author Kazuhiko Hasegawa 030 * @since JDK5.0, 031 */ 032public class HybsTimerTaskManager implements Cleanable { 033 private final List<HybsTimerTask> list = new ArrayList<HybsTimerTask>(); 034 035 /** 036 * HybsTimerTask クラスの 実オブジェクトを管理のため、登録します。 037 * 038 * @param task HybsTimerTaskオブジェクト 039 */ 040 public synchronized void addTask( final HybsTimerTask task ) { 041 list.add( task ); 042 } 043 044 /** 045 * このタイマータスクオブジェクトを削除します。 046 * このとき、すでにタイマータスク自信でキャンセルされているオブジェクトの 047 * 削除も行います。 048 * 049 * @param key HybsTimerTask のユニークキー 050 * 051 * @return 結果(削除成功 true/ 削除できなかった false ) 052 */ 053 public synchronized boolean cancelTask( final int key ) { 054 boolean flag = false; 055 056 int size = list.size(); 057 for( int i=size-1; i>=0; i-- ) { 058 HybsTimerTask task = list.get( i ); 059 if( task != null ) { 060 if( !task.isAlive() || ( task.getUniqKey() == key ) ) { 061 list.remove( i ); 062 task.cancel(); 063 flag = true; 064 } 065 } 066 else { 067 list.remove( i ); 068 } 069 } 070 071 return flag; 072 } 073 074 /** 075 * このタイマータスクオブジェクトを削除します。 076 * 077 * @param name HybsTimerTask の名前 078 * 079 * @return 結果(削除成功 true/ 削除できなかった false ) 080 */ 081 public synchronized boolean cancelTask( final String name ) { 082 boolean flag = false; 083 084 if( name == null ) { return flag; } 085 086 int size = list.size(); 087 for( int i=size-1; i>=0; i-- ) { 088 HybsTimerTask task = list.get( i ); 089 if( task != null ) { 090 if( !task.isAlive() || ( name.equals( task.getName() ) ) ) { 091 list.remove( i ); 092 task.cancel(); 093 flag = true; 094 } 095 } 096 else { 097 list.remove( i ); 098 } 099 } 100 101 return flag; 102 } 103 104 /** 105 * 現在実行中の全てのタイマータスクオブジェクトを削除します。 106 * (org.opengion.fukurou.util.Cleanable の実装)。 107 * 108 * @og.rev 4.0.0.0 (2005/01/31) 新規追加 109 */ 110 public synchronized void clear() { 111 HybsTimerTask[] tasks = list.toArray( new HybsTimerTask[list.size()] ); 112 113 for( int i=0; i<tasks.length; i++ ) { 114 if( tasks[i] != null ) { 115 tasks[i].cancel(); 116 tasks[i] = null; 117 } 118 } 119 list.clear(); 120 } 121 122 /** 123 * 現在実行中の、タイマータスクを、すべて返します。 124 * 125 * @return タイマータスクの配列 126 */ 127 public synchronized HybsTimerTask[] toArray() { 128 int size = list.size(); 129 for( int i=size-1; i>=0; i-- ) { 130 HybsTimerTask task = list.get( i ); 131 if( task != null ) { 132 if( !task.isAlive() ) { 133 list.remove( i ); 134 } 135 } 136 else { 137 list.remove( i ); 138 } 139 } 140 141 HybsTimerTask[] tasks = list.toArray( new HybsTimerTask[list.size()] ); 142 143 return tasks; 144 } 145 146 /** 147 * 現在実行中の、タイマータスクの件数を返します。 148 * 149 * タスクがすでに 終了していたり、null 化されているかをチェックし、 150 * 不要なタスクは、削除します。 151 * 152 * @return タイマータスクの件数 153 */ 154 public synchronized int size() { 155 int size = list.size(); 156 for( int i=size-1; i>=0; i-- ) { 157 HybsTimerTask task = list.get( i ); 158 if( task != null ) { 159 if( !task.isAlive() ) { 160 list.remove( i ); 161 } 162 } 163 else { 164 list.remove( i ); 165 } 166 } 167 168 return list.size(); 169 } 170 171 /** 172 * 現在実行中の、タイマータスクに、同一の Name 属性を持ったタスクが存在するかどうか。 173 * 174 * @param nm タイマータスクの、Name 属性 175 * 176 * @return 存在する:true / 存在しない:false 177 */ 178 public synchronized boolean contains( final String nm ) { 179 if( nm == null ) { return false; } 180 181 HybsTimerTask[] tasks = toArray() ; 182 183 for( int i=0; i<tasks.length; i++ ) { 184 if( tasks[i] != null && nm.equals( tasks[i].getName() ) ) { return true; } 185 } 186 return false; 187 } 188}