<commit_before>/*
    JSPWiki - a JSP-based WikiWiki clone.

    Copyright (C) 2001-2007 <NAME> (<EMAIL>)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2.1 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package com.ecyrd.jspwiki.ui.progress;

import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.apache.log4j.Logger;
import org.safehaus.uuid.UUIDGenerator;

import com.ecyrd.jspwiki.rpc.RPCCallable;
import com.ecyrd.jspwiki.rpc.json.JSONRPCManager;

/**
 *  Manages progressing items.  In general this class is used whenever JSPWiki
 *  is doing something which may require a long time.  In addition, this manager
 *  provides a JSON interface for finding remotely what the progress is.  The
 *  JSON object name is JSON_PROGRESSTRACKER = "{@value JSON_PROGRESSTRACKER}".
 *
 *  @author <NAME>
 *  @since  2.6
 */
// FIXME: Gotta synchronize
public class ProgressManager
{
    private Map m_progressingTasks = new HashMap();

    /**
     *  The name of the progress tracker JSON object.  The current value is "{@value}",
     */
    public static final String JSON_PROGRESSTRACKER = "progressTracker";

    private static Logger log = Logger.getLogger( ProgressManager.class );

    public ProgressManager()
    {
        JSONRPCManager.registerGlobalObject( JSON_PROGRESSTRACKER, new JSONTracker() );
    }

    /**
     *  You can use this to get an unique process identifier.
     *  @return A new random value
     */
    public String getNewProgressIdentifier()
    {
        return UUIDGenerator.getInstance().generateRandomBasedUUID().toString();
    }

    /**
     *  Call this method to get your ProgressItem into the ProgressManager queue.
     *  The ProgressItem will be moved to state STARTED.
     *
     *  @param pi
     *  @param id
     */
    public void startProgress( ProgressItem pi, String id )
    {
        log.debug("Adding "+id+" to progress queue");
        m_progressingTasks.put( id, pi );
        pi.setState( ProgressItem.STARTED );
    }

    /**
     *  Call this method to remove your ProgressItem from the queue (after which
     *  getProgress() will no longer find it.  The ProgressItem will be moved to state
     *  STOPPED.
     *
     *  @param id
     */
    public void stopProgress( String id )
    {
        log.debug("Removed "+id+" from progress queue");
        ProgressItem pi = (ProgressItem) m_progressingTasks.remove( id );
        if( pi != null ) pi.setState( ProgressItem.STOPPED );
    }

    /**
     *  Get the progress in percents.
     *
     *  @param id
     *  @return
     *  @throws IllegalArgumentException If no such progress item exists.
     */
    public int getProgress( String id )
    {
        ProgressItem pi = (ProgressItem)m_progressingTasks.get( id );

        if( pi != null )
        {
            return pi.getProgress();
        }

        throw new IllegalArgumentException("No such id was found");
    }

    /**<commit_msg>Fixed javadocs<commit_after>/*
    JSPWiki - a JSP-based WikiWiki clone.

    Copyright (C) 2001-2007 <NAME> (<EMAIL>)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2.1 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package com.ecyrd.jspwiki.ui.progress;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.safehaus.uuid.UUIDGenerator;

import com.ecyrd.jspwiki.rpc.RPCCallable;
import com.ecyrd.jspwiki.rpc.json.JSONRPCManager;

/**
 *  Manages progressing items.  In general this class is used whenever JSPWiki
 *  is doing something which may require a long time.  In addition, this manager
 *  provides a JSON interface for finding remotely what the progress is.  The
 *  JSON object name is JSON_PROGRESSTRACKER = "{@value JSON_PROGRESSTRACKER}".
 *
 *  @author <NAME>
 *  @since  2.6
 */
// FIXME: Gotta synchronize
public class ProgressManager
{
    private Map m_progressingTasks = new HashMap();

    /**
     *  The name of the progress tracker JSON object.  The current value is "{@value}",
     */
    public static final String JSON_PROGRESSTRACKER = "progressTracker";

    private static Logger log = Logger.getLogger( ProgressManager.class );

    /**
     *  Creates a new ProgressManager.
     */
    public ProgressManager()
    {
        JSONRPCManager.registerGlobalObject( JSON_PROGRESSTRACKER, new JSONTracker() );
    }

    /**
     *  You can use this to get an unique process identifier.
     *  @return A new random value
     */
    public String getNewProgressIdentifier()
    {
        return UUIDGenerator.getInstance().generateRandomBasedUUID().toString();
    }

    /**
     *  Call this method to get your ProgressItem into the ProgressManager queue.
     *  The ProgressItem will be moved to state STARTED.
     *
     *  @param pi ProgressItem to start
     *  @param id The progress identifier
     */
    public void startProgress( ProgressItem pi, String id )
    {
        log.debug("Adding "+id+" to progress queue");
        m_progressingTasks.put( id, pi );
        pi.setState( ProgressItem.STARTED );
    }

    /**
     *  Call this method to remove your ProgressItem from the queue (after which
     *  getProgress() will no longer find it.  The ProgressItem will be moved to state
     *  STOPPED.
     *
     *  @param id The progress identifier
     */
    public void stopProgress( String id )
    {
        log.debug("Removed "+id+" from progress queue");
        ProgressItem pi = (ProgressItem) m_progressingTasks.remove( id );
        if( pi != null ) pi.setState( ProgressItem.STOPPED );
    }

    /**
     *  Get the progress in percents.
     *
     *  @param id The progress identifier.
     *  @return A value between 0 to 100 indicating the progress.
     *  @throws IllegalArgumentException If no such progress item exists.
     */
    public int getProgress( String id )
        throws IllegalArgumentException
    {
        ProgressItem pi = (ProgressItem)m_progressingTasks.get( id );

        if( pi != null )
        {
            return pi.getProgress();
        }

        throw new IllegalArgumentException("No such id was found");
    }

    /**
{'fraction_non_alphanumeric': 0.08049993280473054, 'fraction_numerical': 0.007794651256551538, 'mean_word_length': 2.7284569138276553, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6739496', 'n_tokens_mistral': 2105, 'n_tokens_neox': 2031, 'n_words': 784}