<commit_before>/*
 * $Id$
 * $Revision$ $Date$
 * 
 * ==============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package wicket.util.watch;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import wicket.util.listener.ChangeListenerSet;
import wicket.util.listener.IChangeListener;
import wicket.util.thread.ICode;
import wicket.util.thread.Task;
import wicket.util.time.Duration;
import wicket.util.time.Time;

/**
 * Monitors one or more Modifiable objects, calling a ChangeListener when a
 * given object's modification time changes.
 * 
 * @author <NAME>
 */
public final class ModificationWatcher
{
	/** Logging */
	private static final Log log = LogFactory.getLog(ModificationWatcher.class);

	/** Maps Modifiable objects to Entry objects */
	private final Map modifiableToEntry = new HashMap();

	// MarkupContainer class for holding modifiable entries to watch
	private static final class Entry
	{
		// The most recent lastModificationTime polled on the object
		Time lastModifiedTime;

		// The set of listeners to call when the modifiable changes
		final ChangeListenerSet listeners = new ChangeListenerSet();
		
		// The modifiable thing
		IModifiable modifiable;
	}

	/**
	 * For two-phase construction
	 */
	public ModificationWatcher()
	{
	}

	/**
	 * Constructor
	 * 
	 * @param pollFrequency
	 *            How often to check on modifiables
	 */
	public ModificationWatcher(final Duration pollFrequency)
	{
		start(pollFrequency);
	}

	/**
	 * Adds a Modifiable object and an IChangeListener to call when the
	 * modifiable object is modified.
	 * 
	 * @param modifiable
	 *            The modifiable thing to monitor
	 * @param listener
	 *            The listener to call if the modifiable is modified
	 * @return <tt>true</tt> if the set did not already contain the specified element.
	 */
	public final boolean add(final IModifiable modifiable, final IChangeListener listener)
	{
		// Look up entry for modifiable
		final Entry entry = (Entry)modifiableToEntry.get(modifiable);

		// Found it?
		if (entry == null)
		{
			if (modifiable.lastModifiedTime() != null)
			{
				// Construct new entry
				final Entry newEntry = new Entry();

				newEntry.modifiable = modifiable;
				newEntry.lastModifiedTime = modifiable.lastModifiedTime();
				newEntry.listeners.add(listener);

				// Put in map
				modifiableToEntry.put(modifiable, newEntry);
			}
			else
			{
				// The IModifiable is not returning a valid lastModifiedTime
				log.info("Cannot track modifications to resource " + modifiable);
			}
			
			return true;
		}
		else
		{
			// Add listener to existing entry
			return entry.listeners.add(listener);
		}
	}

	/**
	 * Remove all entries associated with 'modifiable'
	 * 
	 * @param modifiable
	 * @return the object removed, else null
	 */
	public IModifiable remove(final IModifiable modifiable)
	{
		return (IModifiable) modifiableToEntry.remove(modifiable);
	}
	
	/**<commit_msg>class cast exception fix in remove<commit_after>/*
 * $Id$
 * $Revision$ $Date$
 * 
 * ==============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package wicket.util.watch;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import wicket.util.listener.ChangeListenerSet;
import wicket.util.listener.IChangeListener;
import wicket.util.thread.ICode;
import wicket.util.thread.Task;
import wicket.util.time.Duration;
import wicket.util.time.Time;

/**
 * Monitors one or more Modifiable objects, calling a ChangeListener when a
 * given object's modification time changes.
 * 
 * @author <NAME>
 */
public final class ModificationWatcher
{
	/** Logging */
	private static final Log log = LogFactory.getLog(ModificationWatcher.class);

	/** Maps Modifiable objects to Entry objects */
	private final Map modifiableToEntry = new HashMap();

	// MarkupContainer class for holding modifiable entries to watch
	private static final class Entry
	{
		// The most recent lastModificationTime polled on the object
		Time lastModifiedTime;

		// The set of listeners to call when the modifiable changes
		final ChangeListenerSet listeners = new ChangeListenerSet();
		
		// The modifiable thing
		IModifiable modifiable;
	}

	/**
	 * For two-phase construction
	 */
	public ModificationWatcher()
	{
	}

	/**
	 * Constructor
	 * 
	 * @param pollFrequency
	 *            How often to check on modifiables
	 */
	public ModificationWatcher(final Duration pollFrequency)
	{
		start(pollFrequency);
	}

	/**
	 * Adds a Modifiable object and an IChangeListener to call when the
	 * modifiable object is modified.
	 * 
	 * @param modifiable
	 *            The modifiable thing to monitor
	 * @param listener
	 *            The listener to call if the modifiable is modified
	 * @return <tt>true</tt> if the set did not already contain the specified element.
	 */
	public final boolean add(final IModifiable modifiable, final IChangeListener listener)
	{
		// Look up entry for modifiable
		final Entry entry = (Entry)modifiableToEntry.get(modifiable);

		// Found it?
		if (entry == null)
		{
			if (modifiable.lastModifiedTime() != null)
			{
				// Construct new entry
				final Entry newEntry = new Entry();

				newEntry.modifiable = modifiable;
				newEntry.lastModifiedTime = modifiable.lastModifiedTime();
				newEntry.listeners.add(listener);

				// Put in map
				modifiableToEntry.put(modifiable, newEntry);
			}
			else
			{
				// The IModifiable is not returning a valid lastModifiedTime
				log.info("Cannot track modifications to resource " + modifiable);
			}
			
			return true;
		}
		else
		{
			// Add listener to existing entry
			return entry.listeners.add(listener);
		}
	}

	/**
	 * Remove all entries associated with 'modifiable'
	 * 
	 * @param modifiable
	 * @return the object removed, else null
	 */
	public IModifiable remove(final IModifiable modifiable)
	{
		final Entry entry = (Entry)modifiableToEntry.remove(modifiable);
		if(entry != null)
		{
			return entry.modifiable;
		}
		return null;
	}
	
	/**
{'fraction_non_alphanumeric': 0.10694896851248643, 'fraction_numerical': 0.0010857763300760044, 'mean_word_length': 3.574177529484792, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '3519970', 'n_tokens_mistral': 2238, 'n_tokens_neox': 1991, 'n_words': 706}