001/* 
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.  
018 */
019
020package org.apache.wiki.pages;
021
022import java.util.Arrays;
023import java.util.Collections;
024import java.util.Comparator;
025import java.util.List;
026import java.util.Properties;
027
028import org.apache.log4j.Logger;
029import org.apache.wiki.util.ClassUtil;
030import org.apache.wiki.util.comparators.JavaNaturalComparator;
031
032/**
033 * Wrapper class for managing and using the PageNameComparator.
034 * <p>
035 * <b>Note</b> - this class is deliberately not null safe. Never call any of the methods with a null argument!
036 */
037public class PageSorter implements Comparator< String > {
038    
039    private static final Logger LOG = Logger.getLogger( PageSorter.class );
040
041    // The name of the property that specifies the desired page name comparator
042    protected static final String PROP_PAGE_NAME_COMPARATOR = "jspwiki.pageNameComparator.class";
043
044    private Comparator< String > m_comparator;
045
046    /** Default constructor uses Java "natural" ordering. */
047    public PageSorter() {
048        m_comparator = JavaNaturalComparator.DEFAULT_JAVA_COMPARATOR;
049    }
050
051    /**
052     * Construct with a particular comparator.
053     * 
054     * @param comparator the Comparator to use
055     */
056    public PageSorter( Comparator<String> comparator ) {
057        m_comparator = comparator;
058    }
059
060    /**
061     * Compare two page names (String version).
062     * 
063     * @param pageName1 the first page name
064     * @param pageName2 the second page name
065     * @return see java.util.Comparator
066     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
067     */
068    @Override
069    public int compare( String pageName1, String pageName2 ) {
070        return m_comparator.compare( pageName1, pageName2 );
071    }
072
073    @Override
074    public boolean equals( Object o ) {
075        if( !(o instanceof PageSorter) ) {
076            return false; // Definitely not equal
077        }
078        PageSorter that = (PageSorter) o;
079        if( this == that || m_comparator == that.m_comparator ) {
080            return true; // Essentially the same object
081        }
082        return m_comparator.equals( that.m_comparator );
083    }
084
085    /**
086     * Called by WikiEngine to initialise this instance. Tries to use class
087     * given by the PROP_PAGE_NAME_COMPARATOR property as the page name
088     * comparator. Uses a default comparator if this property is not set or
089     * there is any problem loading the specified class.
090     * 
091     * @param props this WikiEngine's properties.
092     */
093    @SuppressWarnings( "unchecked" )
094    public void initialize( Properties props ) {
095        // Default is Java natural order
096        m_comparator = JavaNaturalComparator.DEFAULT_JAVA_COMPARATOR;
097        String className = props.getProperty( PROP_PAGE_NAME_COMPARATOR );
098        if( className != null && className.length() > 0 ) {
099            try {
100                m_comparator = (Comparator<String>) ClassUtil.findClass( "org.apache.wiki.util.comparators", className ).newInstance();
101            } catch( Exception e ) {
102                LOG.error( "Falling back to default \"natural\" comparator", e );
103            }
104        }
105    }
106
107    /**
108     * Sorts the specified list into ascending order based on the
109     * PageNameComparator. The actual sort is done using
110     * <code>Collections.sort()</code>.
111     * 
112     * @param nameList the page names to be sorted
113     */
114    public void sort( List< String > nameList ) {
115        Collections.sort( nameList, m_comparator );
116    }
117
118    /**
119     * Sorts the specified array into ascending order based on the
120     * PageNameComparator. The actual sort is done using
121     * <code>Arrays.sort()</code>.
122     * 
123     * @param nameArray the page names to be sorted
124     */
125    public void sort( String[] nameArray ) {
126        Arrays.sort( nameArray, m_comparator );
127    }
128
129}