referencemanager.java

来自「我想下载一个东西」· Java 代码 · 共 428 行 · 第 1/2 页

JAVA
428
字号
            }        }            }    /**     *  When initially building a ReferenceManager from scratch, call this method     * BEFORE calling updateReferences() with a full list of existing page names.     * It builds the refersTo and referredBy key lists, thus enabling      * updateReferences() to function correctly.     * <P>     * This method should NEVER be called after initialization. It clears all mappings      * from the reference tables.     *      * @param pages   a Collection containing WikiPage objects.     */    private synchronized void buildKeyLists( Collection pages )    {        m_refersTo.clear();        m_referredBy.clear();        if( pages == null )            return;        Iterator it = pages.iterator();        try        {            while( it.hasNext() )            {                WikiPage page = (WikiPage)it.next();                // We add a non-null entry to referredBy to indicate the referred page exists                m_referredBy.put( page.getName(), new HashSet() );                // Just add a key to refersTo; the keys need to be in sync with referredBy.                m_refersTo.put( page.getName(), null );            }        }        catch( ClassCastException e )        {            log.fatal( "Invalid collection entry in ReferenceManager.buildKeyLists().", e );        }    }    /**     * Marks the page as referred to by the referrer. If the page does not     * exist previously, nothing is done. (This means that some page, somewhere,     * has a link to a page that does not exist.)     * <P>     * This method is NOT synchronized. It should only be referred to from     * within a synchronized method, or it should be made synced if necessary.            */    private void updateReferredBy( String page, String referrer )    {        // We're not really interested in first level self-references.        if( page.equals( referrer ) )            return;        HashSet referrers = (HashSet)m_referredBy.get( page );        // Even if 'page' has not been created yet, it can still be referenced.        // This requires we don't use m_referredBy keys when looking up missing        // pages, of course.         if(referrers == null)        {            referrers = new HashSet();            m_referredBy.put( page, referrers );        }        referrers.add( referrer );    }    /**      *  Finds all unreferenced pages. This requires a linear scan through     *  m_referredBy to locate keys with null or empty values.     */    public synchronized Collection findUnreferenced()    {        ArrayList unref = new ArrayList();        Set keys = m_referredBy.keySet();        Iterator it = keys.iterator();        while( it.hasNext() )        {            String key = (String) it.next();            HashSet refs = (HashSet) m_referredBy.get( key );            if( refs == null || refs.isEmpty() )                unref.add( key );        }        return( unref );    }        /**     * Finds all references to non-existant pages. This requires a linear     * scan through m_refersTo values; each value must have a corresponding     * key entry in the reference HashMaps, otherwise such a page has never     * been created.      * <P>     * Returns a Collection containing Strings of unreferenced page names.     * Each non-existant page name is shown only once - we don't return information     * on who referred to it.     */    public synchronized Collection findUncreated()    {        HashSet uncreated = new HashSet();        // Go through m_refersTo values and check that m_refersTo has the corresponding keys.        // We want to reread the code to make sure our HashMaps are in sync...        Collection allReferences = m_refersTo.values();        Iterator it = allReferences.iterator();        while( it.hasNext() )        {            Collection refs = (Collection)it.next();            if( refs != null )            {                Iterator rit = refs.iterator();                while( rit.hasNext() )                {                    String aReference = (String)rit.next();                    if( m_engine.pageExists( aReference ) == false )                    {                        uncreated.add( aReference );                    }                }            }        }        return( uncreated );    }    /**     * Find all pages that refer to this page. Returns null if the page     * does not exist or is not referenced at all, otherwise returns a      * collection containint page names (String) that refer to this one.     */    public synchronized Collection findReferrers( String pagename )    {        HashSet refs = (HashSet)m_referredBy.get( pagename );        if( refs == null || refs.isEmpty() )            return( null );        else            return( refs );    }    /**     * Test method: dumps the contents of our link lists to stdout.     * This method is NOT synchronized, and should be used in testing     * with one user, one WikiEngine only.     */    // FIXME: Remove, not good putting debug code in distribution.    /*    public void dump()    {        try        {            System.out.println( "================================================================" );            System.out.println( "Referred By list:" );            Set keys = m_referredBy.keySet();            Iterator it = keys.iterator();            while( it.hasNext() )            {                String key = (String) it.next();                System.out.print( key + " referred by: " );                HashSet refs = (HashSet)m_referredBy.get( key );                Iterator rit = refs.iterator();                while( rit.hasNext() )                {                    String aRef = (String)rit.next();                    System.out.print( aRef + " " );                }                System.out.println();            }                                    System.out.println( "----------------------------------------------------------------" );            System.out.println( "Refers To list:" );            keys = m_refersTo.keySet();            it = keys.iterator();            while( it.hasNext() )            {                String key = (String) it.next();                System.out.print( key + " refers to: " );                Collection refs = (Collection)m_refersTo.get( key );                if(refs != null)                {                    Iterator rit = refs.iterator();                    while( rit.hasNext() )                    {                        String aRef = (String)rit.next();                        System.out.print( aRef + " " );                    }                    System.out.println();                }                else                    System.out.println("(no references)");            }            System.out.println( "================================================================" );        }        catch(Exception e)        {            System.out.println("Problem in dump(): " + e.getMessage());            e.printStackTrace();        }    }    */}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?