]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.maps/src/org/simantics/maps/pojo/TileJob.java
Share some projects for Simantics District
[simantics/district.git] / org.simantics.district.maps / src / org / simantics / maps / pojo / TileJob.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.maps.pojo;
13
14 import java.awt.Image;
15 import java.util.LinkedList;
16 import java.util.concurrent.Executor;
17 import java.util.concurrent.ScheduledThreadPoolExecutor;
18
19 import org.simantics.maps.ProvisionException;
20 import org.simantics.maps.query.Query;
21 import org.simantics.maps.tile.IFilter;
22 import org.simantics.maps.tile.ITileProvider;
23 import org.simantics.maps.tile.TileKey;
24
25 /**
26  * @author Tuukka Lehtonen
27  * @see TileJobQueue
28  */
29 public class TileJob implements Runnable {
30
31     LinkedList<Query<TileKey, Image>> queue = new LinkedList<Query<TileKey, Image>>();
32     Executor executor = new ScheduledThreadPoolExecutor(1);
33     ITileProvider provider;
34
35     public TileJob() {
36     }
37
38     public void setTileProvider(ITileProvider provider) {
39         this.provider = provider;
40     }
41
42     protected Image doQuery(TileKey key) throws ProvisionException {
43         return provider.get(key);
44     }
45
46     public void run() {
47         Query<TileKey, Image> job = pullNextJob();
48         do {
49             if (job != null) {
50                 try {
51                     Image result = doQuery(job.source);
52                     job.listener.queryComplete(job, result);
53                 } catch (Exception e) {
54                     job.listener.queryFailed(job, e);
55                 }
56             }
57             // 4. Pick next job
58             job = pullNextJob();
59         } while (job != null);
60     }
61
62     protected synchronized int jobsLeft() {
63         return queue.size();
64     }
65
66     protected synchronized Query<TileKey, Image> pullNextJob() {
67         if (queue.isEmpty())
68             return null;
69         return queue.removeFirst();
70     }
71
72     public synchronized void clear() {
73         @SuppressWarnings("unchecked")
74         Query<TileKey, Image> jobs[] = queue.toArray(new Query[queue.size()]);
75         for (Query<TileKey, Image> j : jobs)
76             removeJob(j);
77     }
78
79     public synchronized void addJob(Query<TileKey, Image> job) {
80         queue.addLast(job);
81         if (queue.size() == 1) {
82             executor.execute(this);
83         }
84     }
85
86     public synchronized void addAsFirstJob(Query<TileKey, Image> job) {
87         queue.addFirst(job);
88         if (queue.size() == 1) {
89             executor.execute(this);
90         }
91     }
92
93     public synchronized boolean removeJob(Query<TileKey, Image> job) {
94         if (queue.remove(job)) {
95             job.listener.queryCanceled(job);
96             return true;
97         }
98         return false;
99     }
100
101     public synchronized void filterQueries(IFilter<TileKey> filter) {
102         if (queue.isEmpty())
103             return;
104
105         LinkedList<Query<TileKey, Image>> result = new LinkedList<Query<TileKey, Image>>();
106         for (Query<TileKey, Image> query : queue) {
107             if (filter.select(query.source))
108                 result.add(query);
109             else
110                 query.listener.queryCanceled(query);
111         }
112         this.queue = result;
113     }
114
115 }