001 /**
002 * Copyright (c) 2010, SIB. All rights reserved.
003 *
004 * SIB (Swiss Institute of Bioinformatics) - http://www.isb-sib.ch Host -
005 * https://sourceforge.net/projects/javaprotlib/
006 *
007 * Redistribution and use in source and binary forms, with or without
008 * modification, are permitted provided that the following conditions are met:
009 * Redistributions of source code must retain the above copyright notice, this
010 * list of conditions and the following disclaimer. Redistributions in binary
011 * form must reproduce the above copyright notice, this list of conditions and
012 * the following disclaimer in the documentation and/or other materials provided
013 * with the distribution. Neither the name of the SIB/GENEBIO nor the names of
014 * its contributors may be used to endorse or promote products derived from this
015 * software without specific prior written permission.
016 *
017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
020 * ARE DISCLAIMED. IN NO EVENT SHALL SIB/GENEBIO BE LIABLE FOR ANY DIRECT,
021 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
024 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027 */
028 package org.expasy.jpl.commons.collection;
029
030
031 import java.util.HashMap;
032 import java.util.HashSet;
033 import java.util.Map;
034 import java.util.Set;
035
036
037 /**
038 * This pool stores unique and immutable objects.
039 *
040 * @author nikitin
041 *
042 * @param <T> the {@code Cachable} objects.
043 *
044 * @version 1.0
045 *
046 */
047 public final class PoolImpl<T extends Cachable> implements
048 Pool<T> {
049
050 /** cached object */
051 private final Map<String, T> pool;
052 private final Map<T, String> revPool;
053
054 /**
055 * Private constructor.
056 */
057 private PoolImpl() {
058 pool = new HashMap<String, T>();
059 revPool = new HashMap<T, String>();
060 }
061
062 public static <T extends Cachable> PoolImpl<T> newInstance() {
063 return new PoolImpl<T>();
064 }
065
066 public void register(final T o) {
067
068 String id = o.getId();
069
070 if (revPool.containsKey(o)) {
071 throw new ObjectAlreadyExistException(o);
072 }
073
074 if (!pool.containsKey(id)) {
075 pool.put(id, o);
076 revPool.put(o, id);
077 } else {
078 throw new KeyAlreadyExistException(id);
079 }
080 }
081
082 public T unregister(String id) {
083 if (isRegistered(id)) {
084 return pool.remove(lookup(id));
085 }
086 return null;
087 }
088
089 /** {@inheritDoc} */
090 public final boolean isRegistered(final String id) {
091 return pool.containsKey(id);
092 }
093
094 /** {@inheritDoc} */
095 public final boolean isRegistered(T o) {
096 return revPool.containsKey(o);
097 }
098
099 /** {@inheritDoc} */
100 public T lookup(final String id) {
101 if (isRegistered(id)) {
102 return pool.get(id);
103 }
104 return null;
105 }
106
107 /** {@inheritDoc} */
108 public Set<String> getKeys() {
109 return pool.keySet();
110 }
111
112 /** {@inheritDoc} */
113 public Set<T> getValues() {
114 return new HashSet<T>(pool.values());
115 }
116
117 /** {@inheritDoc} */
118 public int size() {
119 return pool.size();
120 }
121
122 /** {@inheritDoc} */
123 public void clear() {
124 pool.clear();
125 revPool.clear();
126 }
127
128 @Override
129 public String toString() {
130 return pool.toString();
131 }
132 }