001/* Copyright (C) 2013 TU Dortmund
002 * This file is part of LearnLib, http://www.learnlib.de/.
003 * 
004 * LearnLib is free software; you can redistribute it and/or
005 * modify it under the terms of the GNU Lesser General Public
006 * License version 3.0 as published by the Free Software Foundation.
007 * 
008 * LearnLib is distributed in the hope that it will be useful,
009 * but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
011 * Lesser General Public License for more details.
012 * 
013 * You should have received a copy of the GNU Lesser General Public
014 * License along with LearnLib; if not, see
015 * <http://www.gnu.de/documents/lgpl.en.html>.
016 */
017
018package de.learnlib.algorithms.dhc;
019
020import java.util.HashMap;
021import java.util.Map;
022
023/**
024 * A utility class that deduplicates Objects regarding their equals
025 * function.
026 * 
027 * @author Maik Merten <maikmerten@googlemail.com>
028 */
029public class Deduplicator<C> {
030        
031        private final Map<C,C> cache = new HashMap<>();
032        
033        /**
034         * Find an equal representative object for the provided object.
035         * @param instance object which should be deduplicated
036         * @return equal representative object or input object
037         */
038        public C deduplicate(C instance) {
039                C cached = cache.get(instance);
040                if(cached != null)
041                        return cached;
042                cache.put(instance, instance);
043                return instance;
044        }
045        
046        
047}