Crossfire JXClient, Trunk
JXCSkinCache.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.skin.skin;
24 
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.Iterator;
29 import java.util.LinkedHashMap;
30 import java.util.List;
31 import java.util.Map;
32 import org.jetbrains.annotations.NotNull;
33 import org.jetbrains.annotations.Nullable;
34 
40 public class JXCSkinCache<T> implements Iterable<T> {
41 
45  @NotNull
46  private final String ident;
47 
52  @NotNull
53  private final Map<String, T> cache = new LinkedHashMap<>();
54 
60  public JXCSkinCache(@NotNull final String ident) {
61  this.ident = ident;
62  }
63 
67  public void clear() {
68  cache.clear();
69  }
70 
77  public void insert(@NotNull final String name, @NotNull final T t) throws JXCSkinException {
78  if (cache.containsKey(name)) {
79  throw new JXCSkinException("duplicate "+ident+" name: "+name);
80  }
81 
82  cache.put(name, t);
83  }
84 
91  @NotNull
92  public T lookup(@NotNull final String name) throws JXCSkinException {
93  final T t = cache.get(name);
94  if (t == null) {
95  throw new JXCSkinException("undefined "+ident+" name: "+name);
96  }
97 
98  return t;
99  }
100 
105  @NotNull
106  public Collection<String> getNames() {
107  final List<String> result = new ArrayList<>(cache.keySet());
108  result.sort(String::compareTo);
109  return result;
110  }
111 
117  @Nullable
118  public T lookupOptional(@NotNull final String name) {
119  return cache.get(name);
120  }
121 
126  @NotNull
127  @Override
128  public Iterator<T> iterator() {
129  return Collections.unmodifiableCollection(cache.values()).iterator();
130  }
131 
132 }
com.realtime.crossfire.jxclient.skin.skin.JXCSkinCache.cache
final Map< String, T > cache
Definition: JXCSkinCache.java:53
com.realtime.crossfire.jxclient.skin.skin.JXCSkinCache.lookup
T lookup(@NotNull final String name)
Definition: JXCSkinCache.java:92
com.realtime.crossfire.jxclient.skin.skin.JXCSkinCache.clear
void clear()
Definition: JXCSkinCache.java:67
com.realtime.crossfire.jxclient.skin.skin.JXCSkinException
Definition: JXCSkinException.java:31
com.realtime.crossfire.jxclient.skin.skin.JXCSkinCache.ident
final String ident
Definition: JXCSkinCache.java:46
com.realtime.crossfire.jxclient.skin.skin.JXCSkinCache.insert
void insert(@NotNull final String name, @NotNull final T t)
Definition: JXCSkinCache.java:77
com.realtime.crossfire.jxclient.skin.skin.JXCSkinCache
Definition: JXCSkinCache.java:40
com.realtime.crossfire.jxclient.skin.skin.JXCSkinCache.iterator
Iterator< T > iterator()
Definition: JXCSkinCache.java:128
com.realtime.crossfire.jxclient.skin.skin.JXCSkinCache.getNames
Collection< String > getNames()
Definition: JXCSkinCache.java:106
com.realtime.crossfire.jxclient.skin.skin.JXCSkinCache.JXCSkinCache
JXCSkinCache(@NotNull final String ident)
Definition: JXCSkinCache.java:60
com.realtime.crossfire.jxclient.skin.skin.JXCSkinCache.lookupOptional
T lookupOptional(@NotNull final String name)
Definition: JXCSkinCache.java:118