001    package com.github.sarxos.webcam;
002    
003    public abstract class WebcamTask {
004    
005            private boolean sync = true;
006            private WebcamProcessor processor = null;
007            private WebcamDevice device = null;
008            private Throwable throwable = null;
009    
010            public WebcamTask(WebcamDriver driver, WebcamDevice device) {
011    
012                    if (driver == null) {
013                            throw new IllegalArgumentException("Webcam driver argument cannot be null");
014                    }
015    
016                    this.sync = !driver.isThreadSafe();
017                    this.device = device;
018                    this.processor = WebcamProcessor.getInstance();
019            }
020    
021            public WebcamDevice getDevice() {
022                    return device;
023            }
024    
025            /**
026             * Process task by processor thread.
027             * 
028             * @param processor the processor to be used to process this task
029             * @throws InterruptedException when thread has been interrupted
030             */
031            public void process() throws InterruptedException {
032                    if (sync) {
033                            if (processor == null) {
034                                    throw new RuntimeException("Driver should be synchronized, but processor is null");
035                            }
036                            processor.process(this);
037                    } else {
038                            handle();
039                    }
040            }
041    
042            public Throwable getThrowable() {
043                    return throwable;
044            }
045    
046            public void setThrowable(Throwable t) {
047                    this.throwable = t;
048            }
049    
050            protected abstract void handle();
051    }