Crossfire JXClient, Trunk
FileCacheFaceQueue.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff
19  * Copyright (C) 2006-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.faces;
24 
25 import java.util.Collection;
26 import java.util.HashSet;
27 import java.util.concurrent.ExecutorService;
28 import java.util.concurrent.Executors;
29 import javax.swing.ImageIcon;
30 import org.jetbrains.annotations.NotNull;
31 
36 public class FileCacheFaceQueue extends AbstractFaceQueue {
37 
41  @NotNull
42  private final Object sync = new Object();
43 
47  @NotNull
49 
53  @NotNull
55 
59  @NotNull
61 
66  @NotNull
67  private final ExecutorService executorService = Executors.newFixedThreadPool(1);
68 
73  @NotNull
74  private final Collection<Face> pendingLoadFaces = new HashSet<>();
75 
82  private int id;
83 
94  this.imageCacheOriginal = imageCacheOriginal;
95  this.imageCacheScaled = imageCacheScaled;
96  this.imageCacheMagicMap = imageCacheMagicMap;
97  }
98 
99  @Override
100  public void reset() {
101  synchronized (sync) {
102  id++;
103  pendingLoadFaces.clear();
104  }
105  }
106 
107  @Override
108  public void loadFace(@NotNull final Face face) {
109  final boolean doAdd;
110  synchronized (sync) {
111  doAdd = pendingLoadFaces.add(face);
112  }
113  if (doAdd) {
114  executorService.submit(new LoadTask(face));
115  }
116  }
117 
124  public void saveFace(@NotNull final Face face, @NotNull final FaceImages faceImages) {
125  executorService.submit(new SaveTask(face, faceImages));
126  }
127 
132  private class LoadTask implements Runnable {
133 
138  private final int taskId = id;
139 
143  @NotNull
144  private final Face face;
145 
150  private LoadTask(@NotNull final Face face) {
151  this.face = face;
152  }
153 
154  @Override
155  public void run() {
156  final Thread thread = Thread.currentThread();
157  final String name = thread.getName();
158  try {
159  thread.setName("JXClient:LoadTask:face="+face.getFaceName());
160  try {
161  if (taskId != id) {
162  return;
163  }
164 
165  final ImageIcon originalImageIcon = imageCacheOriginal.load(face);
166  if (originalImageIcon == null) {
168  return;
169  }
170 
171  final ImageIcon scaledImageIcon = imageCacheScaled.load(face);
172  if (scaledImageIcon == null) {
174  return;
175  }
176 
177  final ImageIcon magicMapImageIcon = imageCacheMagicMap.load(face);
178  if (magicMapImageIcon == null) {
180  return;
181  }
182 
183  fireFaceLoaded(face, new FaceImages(originalImageIcon, scaledImageIcon, magicMapImageIcon));
184  } finally {
185  synchronized (sync) {
186  pendingLoadFaces.remove(face);
187  }
188  }
189  } finally {
190  thread.setName(name);
191  }
192  }
193 
194  }
195 
200  private class SaveTask implements Runnable {
201 
205  @NotNull
206  private final Face face;
207 
211  @NotNull
212  private final FaceImages faceImages;
213 
219  private SaveTask(@NotNull final Face face, @NotNull final FaceImages faceImages) {
220  this.face = face;
221  this.faceImages = faceImages;
222  }
223 
224  @Override
225  public void run() {
226  final Thread thread = Thread.currentThread();
227  final String name = thread.getName();
228  try {
229  thread.setName("JXClient:SaveTask:face="+face.getFaceName());
233  } finally {
234  thread.setName(name);
235  }
236  }
237 
238  }
239 
240 }
com.realtime.crossfire.jxclient.faces.FileCacheFaceQueue.SaveTask
Definition: FileCacheFaceQueue.java:200
com.realtime.crossfire.jxclient.faces.Face.getFaceName
String getFaceName()
Definition: Face.java:124
com.realtime.crossfire.jxclient.faces.FileCacheFaceQueue.id
int id
Definition: FileCacheFaceQueue.java:82
com.realtime.crossfire.jxclient.faces.FileCacheFaceQueue.sync
final Object sync
Definition: FileCacheFaceQueue.java:42
com.realtime.crossfire.jxclient.faces.ImageCache.save
void save(@NotNull Face face, @NotNull ImageIcon imageIcon)
com.realtime.crossfire.jxclient.faces.FaceImages.getMagicMapImageIcon
ImageIcon getMagicMapImageIcon()
Definition: FaceImages.java:87
com.realtime.crossfire.jxclient.faces.FileCacheFaceQueue.LoadTask
Definition: FileCacheFaceQueue.java:132
com.realtime.crossfire.jxclient.faces.FileCacheFaceQueue.LoadTask.face
final Face face
Definition: FileCacheFaceQueue.java:144
com.realtime.crossfire.jxclient.faces.FileCacheFaceQueue.executorService
final ExecutorService executorService
Definition: FileCacheFaceQueue.java:67
com.realtime.crossfire.jxclient.faces.FileCacheFaceQueue.SaveTask.face
final Face face
Definition: FileCacheFaceQueue.java:206
com.realtime.crossfire.jxclient.faces.FileCacheFaceQueue.reset
void reset()
Definition: FileCacheFaceQueue.java:100
com.realtime.crossfire.jxclient.faces.ImageCache
Definition: ImageCache.java:33
com.realtime.crossfire.jxclient.faces.AbstractFaceQueue.fireFaceLoaded
void fireFaceLoaded(@NotNull final Face face, @NotNull final FaceImages faceImages)
Definition: AbstractFaceQueue.java:58
com.realtime.crossfire.jxclient.faces.FileCacheFaceQueue.LoadTask.run
void run()
Definition: FileCacheFaceQueue.java:155
com.realtime.crossfire.jxclient.faces.FaceImages.getOriginalImageIcon
ImageIcon getOriginalImageIcon()
Definition: FaceImages.java:69
com.realtime.crossfire.jxclient.faces.AbstractFaceQueue.fireFaceFailed
void fireFaceFailed(@NotNull final Face face)
Definition: AbstractFaceQueue.java:68
com.realtime.crossfire.jxclient.faces.FileCacheFaceQueue.LoadTask.taskId
final int taskId
Definition: FileCacheFaceQueue.java:138
com.realtime.crossfire.jxclient.faces.FaceImages.getScaledImageIcon
ImageIcon getScaledImageIcon()
Definition: FaceImages.java:78
com.realtime.crossfire.jxclient.faces.FileCacheFaceQueue.pendingLoadFaces
final Collection< Face > pendingLoadFaces
Definition: FileCacheFaceQueue.java:74
com.realtime.crossfire.jxclient.faces.FileCacheFaceQueue.SaveTask.faceImages
final FaceImages faceImages
Definition: FileCacheFaceQueue.java:212
com.realtime.crossfire.jxclient.faces.FileCacheFaceQueue.LoadTask.LoadTask
LoadTask(@NotNull final Face face)
Definition: FileCacheFaceQueue.java:150
com.realtime.crossfire.jxclient.faces.FileCacheFaceQueue.imageCacheOriginal
final ImageCache imageCacheOriginal
Definition: FileCacheFaceQueue.java:48
com.realtime.crossfire.jxclient.faces.FileCacheFaceQueue.SaveTask.SaveTask
SaveTask(@NotNull final Face face, @NotNull final FaceImages faceImages)
Definition: FileCacheFaceQueue.java:219
com.realtime.crossfire.jxclient.faces.FileCacheFaceQueue.imageCacheMagicMap
final ImageCache imageCacheMagicMap
Definition: FileCacheFaceQueue.java:60
com.realtime.crossfire.jxclient.faces.FileCacheFaceQueue
Definition: FileCacheFaceQueue.java:36
com.realtime.crossfire.jxclient.faces.FileCacheFaceQueue.FileCacheFaceQueue
FileCacheFaceQueue(@NotNull final ImageCache imageCacheOriginal, @NotNull final ImageCache imageCacheScaled, @NotNull final ImageCache imageCacheMagicMap)
Definition: FileCacheFaceQueue.java:93
com.realtime.crossfire.jxclient.faces.AbstractFaceQueue
Definition: AbstractFaceQueue.java:34
com.realtime.crossfire.jxclient.faces.FileCacheFaceQueue.loadFace
void loadFace(@NotNull final Face face)
Definition: FileCacheFaceQueue.java:108
com.realtime.crossfire.jxclient.faces.FileCacheFaceQueue.saveFace
void saveFace(@NotNull final Face face, @NotNull final FaceImages faceImages)
Definition: FileCacheFaceQueue.java:124
com.realtime.crossfire.jxclient.faces.ImageCache.load
ImageIcon load(@NotNull Face face)
com.realtime.crossfire.jxclient.faces.FileCacheFaceQueue.imageCacheScaled
final ImageCache imageCacheScaled
Definition: FileCacheFaceQueue.java:54
com.realtime.crossfire.jxclient.faces.Face
Definition: Face.java:37
com.realtime.crossfire.jxclient.faces.FaceImages
Definition: FaceImages.java:32
com.realtime.crossfire.jxclient.faces.FileCacheFaceQueue.SaveTask.run
void run()
Definition: FileCacheFaceQueue.java:225