1 17 18 package org.apache.avalon.cornerstone.services.scheduler; 19 20 27 public class PeriodicTimeTrigger 28 implements TimeTrigger 29 { 30 protected final long m_offset; 31 protected final long m_period; 32 private long m_triggerTime; 33 34 44 public PeriodicTimeTrigger( final int offset, final int period ) 45 { 46 m_offset = offset; 47 m_period = period; 48 49 reset(); 50 } 51 52 59 public long getTimeAfter( final long moment ) 60 { 61 if( moment <= m_triggerTime ) 62 { 63 return m_triggerTime; 64 } 65 else 66 { 67 if( -1 == m_period ) 68 { 69 return -1; 70 } 71 72 final long over = moment - m_triggerTime; 73 final long remainder = over % m_period; 74 75 return moment + ( m_period - remainder ); 76 } 77 } 78 79 public long getOffset() 80 { 81 return m_offset; 82 } 83 84 public long getPeriod() 85 { 86 return m_period; 87 } 88 89 93 public void reset() 94 { 95 final long current = System.currentTimeMillis(); 96 97 if( -1 == m_offset ) 98 { 99 m_triggerTime = current; 100 } 101 else 102 { 103 m_triggerTime = current + m_offset; 104 } 105 } 106 107 public String toString() 108 { 109 final StringBuffer sb = new StringBuffer (); 110 sb.append( "PeriodicTimeTrigger[ " ); 111 112 sb.append( "trigger time=" ); 113 sb.append( m_triggerTime ); 114 sb.append( " " ); 115 116 sb.append( "offset=" ); 117 sb.append( m_offset ); 118 sb.append( " " ); 119 120 if( -1 != m_period ) 121 { 122 sb.append( "period=" ); 123 sb.append( m_period ); 124 sb.append( " " ); 125 } 126 127 sb.append( "]" ); 128 129 return sb.toString(); 130 } 131 } 132 133 134 135 | Popular Tags |