00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 package com.realtime.crossfire.jxclient.skin.skin;
00023
00024 import java.util.Collections;
00025 import java.util.Iterator;
00026 import java.util.LinkedHashMap;
00027 import java.util.Map;
00028 import org.jetbrains.annotations.NotNull;
00029 import org.jetbrains.annotations.Nullable;
00030
00035 public class JXCSkinCache<T> implements Iterable<T> {
00036
00040 @NotNull
00041 private final String ident;
00042
00047 @NotNull
00048 private final Map<String, T> cache = new LinkedHashMap<String, T>();
00049
00055 public JXCSkinCache(@NotNull final String ident) {
00056 this.ident = ident;
00057 }
00058
00062 public void clear() {
00063 cache.clear();
00064 }
00065
00072 public void insert(@NotNull final String name, @NotNull final T t) throws JXCSkinException {
00073 if (cache.containsKey(name)) {
00074 throw new JXCSkinException("duplicate "+ident+" name: "+name);
00075 }
00076
00077 cache.put(name, t);
00078 }
00079
00086 @NotNull
00087 public T lookup(@NotNull final String name) throws JXCSkinException {
00088 final T t = cache.get(name);
00089 if (t == null) {
00090 throw new JXCSkinException("undefined "+ident+" name: "+name);
00091 }
00092
00093 return t;
00094 }
00095
00101 @Nullable
00102 public T lookupOptional(@NotNull final String name) {
00103 return cache.get(name);
00104 }
00105
00110 @NotNull
00111 @Override
00112 public Iterator<T> iterator() {
00113 return Collections.unmodifiableCollection(cache.values()).iterator();
00114 }
00115
00116 }