1   /**
2    * Distribution License:
3    * JSword is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU Lesser General Public License, version 2.1 as published by
5    * the Free Software Foundation. This program is distributed in the hope
6    * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8    * See the GNU Lesser General Public License for more details.
9    *
10   * The License is available on the internet at:
11   *       http://www.gnu.org/copyleft/lgpl.html
12   * or by writing to:
13   *      Free Software Foundation, Inc.
14   *      59 Temple Place - Suite 330
15   *      Boston, MA 02111-1307, USA
16   *
17   * Copyright: 2007
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id: org.eclipse.jdt.ui.prefs 1178 2006-11-06 12:48:02Z dmsmith $
21   */
22  
23  package org.crosswire.common.util;
24  
25  /**
26   * A TimeGate when entered will cause the gate to be closed for a specified
27   * period of time.
28   * 
29   * @see gnu.lgpl.License for license details.<br>
30   *      The copyright to this program is held by it's authors.
31   * @author DM Smith [dmsmith555 at yahoo dot com]
32   */
33  public class TimeGate {
34      /**
35       * Build a TimeGate that will allow entry no more often than count
36       * milliseconds
37       * 
38       * @param count
39       *            the length of time to keep the gate shut after opening it.
40       */
41      public TimeGate(int count) {
42          closeTime = count;
43      }
44  
45      /**
46       * Determine whether entry through the gate is allowed. Opening the gate
47       * will close it until the TimeGate's interval has passed.
48       * 
49       * @return true if one may enter.
50       */
51      public synchronized boolean open() {
52          // check to see if the gate has been closed long enough.
53          // If so, then open it and note the time that it was opened.
54          long now = System.currentTimeMillis();
55          if (now - then > closeTime) {
56              then = now;
57              return true;
58          }
59  
60          // Otherwise the gate was opened not that long ago and
61          // is still closed.
62          return false;
63      }
64  
65      /**
66       * The interval during which the gate is closed.
67       */
68      private int closeTime;
69  
70      /**
71       * The time in milliseconds that the gate last closed.
72       */
73      private long then;
74  }
75