Crossfire JXClient, Trunk  R20561
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-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.skin.skin;
23 
24 import java.util.Collections;
25 import java.util.Iterator;
26 import java.util.LinkedHashMap;
27 import java.util.Map;
28 import org.jetbrains.annotations.NotNull;
29 import org.jetbrains.annotations.Nullable;
30 
36 public class JXCSkinCache<T> implements Iterable<T> {
37 
41  @NotNull
42  private final String ident;
43 
48  @NotNull
49  private final Map<String, T> cache = new LinkedHashMap<>();
50 
56  public JXCSkinCache(@NotNull final String ident) {
57  this.ident = ident;
58  }
59 
63  public void clear() {
64  cache.clear();
65  }
66 
73  public void insert(@NotNull final String name, @NotNull final T t) throws JXCSkinException {
74  if (cache.containsKey(name)) {
75  throw new JXCSkinException("duplicate "+ident+" name: "+name);
76  }
77 
78  cache.put(name, t);
79  }
80 
87  @NotNull
88  public T lookup(@NotNull final String name) throws JXCSkinException {
89  final T t = cache.get(name);
90  if (t == null) {
91  throw new JXCSkinException("undefined "+ident+" name: "+name);
92  }
93 
94  return t;
95  }
96 
102  @Nullable
103  public T lookupOptional(@NotNull final String name) {
104  return cache.get(name);
105  }
106 
111  @NotNull
112  @Override
113  public Iterator<T> iterator() {
114  return Collections.unmodifiableCollection(cache.values()).iterator();
115  }
116 
117 }
Iterator< T > iterator()
Returns all stored values.
final String ident
The description of this cache used for creating error messages.
JXCSkinCache(@NotNull final String ident)
Creates a new instance.
void insert(@NotNull final String name, @NotNull final T t)
Adds a new element to the cache.
T lookupOptional(@NotNull final String name)
Looks up an element by name.
Implements a cache for elements identified by name.
Exception thrown if a skin related problem occurs.
final Map< String, T > cache
The cached elements.
T lookup(@NotNull final String name)
Looks up an element by name.