<commit_before>/*******************************************************************************
 * This file is part of TERMINAL RECALL
 * Copyright (c) 2012-2014 <NAME>
 * Part of the jTRFP.org project
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     chuck - initial API and implementation
 ******************************************************************************/

package org.jtrfp.trcl.pool;

import java.util.Map;

import com.ochafik.util.Adapter;

/**
 * Not related to the JNDI, but likely to be replaced by it in the future.
 * @author <NAME>
 *
 * @param <K>
 * @param <V>
 */
public class ObjectFactory<K,V> {
    private final Map    <K,V> mapToUse;
    private final Adapter<K,V> keyValueAdapter;
    
    public ObjectFactory(Map<K,V> mapToUse, Adapter<K,V> keyValueAdapter){
	this.mapToUse        = mapToUse;
	this.keyValueAdapter = keyValueAdapter;
    }
    
    public V get(K key){
	V result = mapToUse.get(key);
	if(result==null)
	    mapToUse.put(key, result=keyValueAdapter.adapt(key));
	return result;
    }//end public(key)
}//end CachedObjectFactory
<commit_msg>Add peek() and javadocs to ObjectFactory.<commit_after>/*******************************************************************************
 * This file is part of TERMINAL RECALL
 * Copyright (c) 2012-2014 <NAME>
 * Part of the jTRFP.org project
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     chuck - initial API and implementation
 ******************************************************************************/

package org.jtrfp.trcl.pool;

import java.util.Map;

import com.ochafik.util.Adapter;

/**
 * Not related to the JNDI, but likely to be replaced by it in the future.
 * Creates and caches objects of type V from keys of type K.
 * @author <NAME>
 *
 * @param <K>
 * @param <V>
 */
public class ObjectFactory<K,V> {
    private final Map    <K,V> mapToUse;
    private final Adapter<K,V> keyValueAdapter;
    
    public ObjectFactory(Map<K,V> mapToUse, Adapter<K,V> keyValueAdapter){
	this.mapToUse        = mapToUse;
	this.keyValueAdapter = keyValueAdapter;
    }
    
    /**
     * Obtain an instance of V based on the key K, creating one if its key isn't present in the currently-used map.
     * @param key
     * @return Instance of V created with the supplied adapter from K
     * @since Mar 27, 2015
     */
    public V get(K key){
	V result = mapToUse.get(key);
	if(result==null)
	    mapToUse.put(key, result=keyValueAdapter.adapt(key));
	return result;
    }//end get(key)
    
    /**
     * Query the currently-used map for already-created object matching the given key.
     * @param key
     * @return Teh pre-created object, if any, or null if none exists.
     * @since Mar 27, 2015
     */
    public V peek(K key){
	return mapToUse.get(key);
    }//end peek(key)
    
    /**
     * Query the map being used to back this ObjectFactory's caching of already-created objects.
     * @return
     * @since Mar 27, 2015
     */
    public Map<K,V> getMap(){
	return mapToUse;
    }
}//end CachedObjectFactory

{'fraction_non_alphanumeric': 0.1849176538572667, 'fraction_numerical': 0.01097948569777521, 'mean_word_length': 3.4670967741935486, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '8605', 'n_tokens_mistral': 1073, 'n_tokens_neox': 1011, 'n_words': 348}