public static ScanListener noMoreFrequentlyThan(
      final ScanListener listener, final long time, final TimeUnit timeUnit) {
    return new ScanListener() {
      // Most users will only scan from one device at a time.
      private Map<SaneDevice, Long> lastSentTime = Maps.newHashMapWithExpectedSize(1);

      @Override
      public void scanningStarted(SaneDevice device) {
        listener.scanningStarted(device);
      }

      @Override
      public void frameAcquisitionStarted(
          SaneDevice device, SaneParameters parameters, int currentFrame, int likelyTotalFrames) {
        listener.frameAcquisitionStarted(device, parameters, currentFrame, likelyTotalFrames);
      }

      @Override
      public void recordRead(SaneDevice device, int totalBytesRead, int imageSizeBytes) {
        long currentTime = System.currentTimeMillis();
        if (!lastSentTime.containsKey(device)) {
          lastSentTime.put(device, 0L);
        }

        if (currentTime - lastSentTime.get(device) > timeUnit.toMillis(time)) {
          lastSentTime.put(device, currentTime);
          listener.recordRead(device, totalBytesRead, imageSizeBytes);
        }
      }

      @Override
      public void scanningFinished(SaneDevice device) {
        listener.scanningFinished(device);
      }
    };
  }
public static ScanListener noMoreFrequentlyThan ( final ScanListener listener , final long time , final TimeUnit timeUnit ) { return new ScanListener ( ) { private Map < SaneDevice , Long > lastSentTime = Maps . newHashMapWithExpectedSize ( 1 ) ; @ Override public void scanningStarted ( SaneDevice device ) { listener . scanningStarted ( device ) ; } @ Override public void frameAcquisitionStarted ( SaneDevice device , SaneParameters parameters , int currentFrame , int likelyTotalFrames ) { listener . frameAcquisitionStarted ( device , parameters , currentFrame , likelyTotalFrames ) ; } @ Override public void recordRead ( SaneDevice device , int totalBytesRead , int imageSizeBytes ) { long currentTime = System . currentTimeMillis ( ) ; if ( ! lastSentTime . containsKey ( device ) ) { lastSentTime . put ( device , 0L ) ; } if ( currentTime - lastSentTime . get ( device ) > timeUnit . toMillis ( time ) ) { lastSentTime . put ( device , currentTime ) ; listener . recordRead ( device , totalBytesRead , imageSizeBytes ) ; } } @ Override public void scanningFinished ( SaneDevice device ) { listener . scanningFinished ( device ) ; } } ; }
Returns {@link ScanListener} that calls the given listener {@link ScanListener#recordRead}
method no more frequently than the given time any one device. Record read events from one
device that occur more frequently are simply discarded.