001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 * 
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id: Registry.java 2531 2006-09-04 23:01:57Z janguenot $
020 */
021
022package org.nuxeo.common.utils;
023
024import java.util.HashMap;
025import java.util.Map;
026import java.util.Set;
027
028/**
029 * Generic registry implementation.
030 *
031 * @author <a href="mailto:ja@nuxeo.com">Julien Anguenot</a>
032 */
033public class Registry<T> {
034
035    private final String name;
036
037    private final Map<String, T> registry;
038
039    public Registry(String name) {
040        this.name = name;
041        registry = new HashMap<String, T>();
042    }
043
044    public String getName() {
045        return name;
046    }
047
048    public void register(String name, T object) {
049        if (!isRegistered(name) && !isRegistered(object)) {
050            registry.put(name, object);
051        }
052    }
053
054    public void unregister(String name) {
055        if (isRegistered(name)) {
056            registry.remove(name);
057        }
058    }
059
060    public boolean isRegistered(T object) {
061        return registry.containsValue(object);
062    }
063
064    public boolean isRegistered(String name) {
065        return registry.containsKey(name);
066    }
067
068    public int size() {
069        return registry.size();
070    }
071
072    public T getObjectByName(String name) {
073        return registry.get(name);
074    }
075
076    public void clear() {
077        registry.clear();
078    }
079
080    public Set<String> getKeys() {
081        return registry.keySet();
082    }
083
084}