Gridarta Editor
AbstractFaceProvider.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.model.face;
21 
22 import java.lang.ref.SoftReference;
23 import java.util.HashMap;
24 import java.util.Map;
25 import javax.swing.ImageIcon;
26 import org.apache.log4j.Category;
27 import org.apache.log4j.Logger;
28 import org.jetbrains.annotations.NotNull;
29 import org.jetbrains.annotations.Nullable;
30 
36 public abstract class AbstractFaceProvider implements FaceProvider {
37 
41  @NotNull
42  private static final Category LOG = Logger.getLogger(AbstractFaceProvider.class);
43 
48  @NotNull
49  private final Map<String, SoftReference<ImageIcon>> cache = new HashMap<>();
50 
51  @Nullable
52  @Override
53  public ImageIcon getImageIconForFacename(@NotNull final String faceName, final long stretch) {
54  ImageIcon icon = null;
55  final String realFaceName = String.format("%s-%d", faceName, stretch);
56  final SoftReference<ImageIcon> ref = cache.get(realFaceName);
57  if (ref != null) {
58  icon = ref.get();
59  }
60  if (icon == null) {
61  icon = createImage(faceName, stretch);
62  if (icon != null) {
63  cache.put(realFaceName, new SoftReference<>(icon));
64  }
65  }
66  return icon;
67  }
68 
69  @Override
70  public void reload() {
71  if (LOG.isDebugEnabled()) {
72  LOG.debug("Clearing image cache.");
73  }
74  cache.clear();
75  }
76 
83  @Nullable
84  protected abstract ImageIcon createImage(@NotNull String faceName, long stretch);
85 
86 }
net.sf.gridarta.model.face.FaceProvider
This interface represents a lazy loader that provides images on demand.
Definition: FaceProvider.java:30
net.sf.gridarta.model.face.AbstractFaceProvider
Abstract Base class for FaceProviders which implements a memory sensitive cache.
Definition: AbstractFaceProvider.java:36
net.sf.gridarta.model.face.AbstractFaceProvider.getImageIconForFacename
ImageIcon getImageIconForFacename(@NotNull final String faceName, final long stretch)
Get an image from this FaceProvider.
Definition: AbstractFaceProvider.java:53
net.sf.gridarta.model.face.AbstractFaceProvider.reload
void reload()
Reload faces.
Definition: AbstractFaceProvider.java:70
net.sf.gridarta.model.face.AbstractFaceProvider.cache
final Map< String, SoftReference< ImageIcon > > cache
The HashMap to provide the icons from.
Definition: AbstractFaceProvider.java:49
net.sf.gridarta.model.face.AbstractFaceProvider.createImage
abstract ImageIcon createImage(@NotNull String faceName, long stretch)
Creates an image not found in the cache.
net.sf.gridarta.model.face.AbstractFaceProvider.LOG
static final Category LOG
The Logger for printing log messages.
Definition: AbstractFaceProvider.java:42