001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030/**
031 * 
032 */
033package org.openimaj.time;
034
035/**
036 *      A time keeper knows what time it is. You can ask the time keeper what
037 *      time it is using the {@link #getTime()} method.
038 *      <p>
039 *      Subclasses must override the method to return appropriate time-based
040 *      objects.  Note that the time keeper is passive - it does not let anyone
041 *      know when certain times occur - it merely provides times when asked.
042 *      <p>
043 *      Initially the timekeeper should not be started but should only start when
044 *      {@link #run()} is called. TimeKeepers may run continuously, so it is important
045 *      that timekeepers are run in threads. 
046 *      <p><code>
047 *              new Thread( timeKeeper ).start()
048 *      </code></p> 
049 *      For time keepers that do not run continuously
050 *      this will incur an overhead, so if you know that your time keeper does not
051 *      run continuously, you should control it closely; otherwise timekeepers of
052 *      unknown type should be run in threads. If your timekeeper does run in a
053 *      thread, then the stop() method should cause the timekeeper to exit the 
054 *      run method.
055 *      <p>
056 *      The semantic difference between pause and stop is that pause is intended
057 *      for short term stoppages in the running of the timekeeper that will result
058 *      in the timekeeper being restarted either from the same position or from a
059 *      newly seeked position. The stop method is expected to be called when the
060 *      timekeeper is being shut down or will be required to start from the beginning
061 *      again when restarted. Some timekeepers may not support mid-stream stopping
062 *      and they should return false for the {@link #supportsPause()} method.
063 *      Similarly, timekeepers that do not support seeking should return false
064 *      for the {@link #supportsSeek()} method.
065 *
066 *      @author David Dupplaw (dpd@ecs.soton.ac.uk)
067 *      
068 *      @param <T> The type of {@link Timecode}  
069 *      @created 28 Nov 2011
070 */
071public interface TimeKeeper<T extends Timecode> extends Runnable
072{
073        /**
074         *      Use this method to start the time keeper running.
075         * 
076         *      {@inheritDoc}
077         *      @see java.lang.Runnable#run()
078         */
079        @Override
080        public void run();
081        
082        /**
083         *      Pause the running of the timekeeper.
084         */
085        void pause();
086        
087        /**
088         *      Use this method to stop the time keeper from running.
089         */
090        void stop();
091        
092        /**
093         *      Returns the current time.
094         *      @return the current time object.
095         */
096        public T getTime();
097        
098        /**
099         *      Seek to a given timestamp.
100         *      @param timestamp The timestamp to seek to
101         */
102        public void seek( long timestamp ); 
103        
104        /**
105         *      Reset the timekeeper.
106         */
107        public void reset();
108        
109        /**
110         *      Returns whether the timekeeper supports pausing. If the timekeeper
111         *      supports pausing then a stop() followed by a run() will continue
112         *      from where the timekeeper was paused. Use reset() inbetween to force
113         *      the timekeeper to start from the beginning again.
114         *  
115         *      @return TRUE if the timekeeper supports pausing. 
116         */
117        public boolean supportsPause();
118        
119        /**
120         *      Returns whether the timekeeper supports seeking.
121         * 
122         *      @return TRUE if the timekeeper supports seeking.
123         */
124        public boolean supportsSeek();
125}