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 */
019package org.apache.wiki.pages;
020
021import java.io.Serializable;
022import java.util.Comparator;
023import java.util.Date;
024
025import org.apache.log4j.Logger;
026import org.apache.wiki.WikiPage;
027
028/**
029 *  Compares the lastModified date of its arguments.  Both o1 and o2 MUST
030 *  be WikiPage objects, or else you will receive a ClassCastException.
031 *  <p>
032 *  If the lastModified date is the same, then the next key is the page name.
033 *  If the page name is also equal, then returns 0 for equality.
034 */
035// FIXME: Does not implement equals().
036public class PageTimeComparator implements Comparator<WikiPage>, Serializable {
037    
038    private static final long serialVersionUID = 0L;
039
040    private static final Logger log = Logger.getLogger( PageTimeComparator.class ); 
041
042    /**
043     *  {@inheritDoc}
044     */
045    public int compare( WikiPage w1, WikiPage w2 ) {
046        if( w1 == null || w2 == null ) {
047            log.error( "W1 or W2 is NULL in PageTimeComparator!");
048            return 0; // FIXME: Is this correct?
049        }
050
051        Date w1LastMod = w1.getLastModified();
052        Date w2LastMod = w2.getLastModified();
053
054        if( w1LastMod == null ) {
055            log.error( "NULL MODIFY DATE WITH " + w1.getName() );
056            return 0;
057        } else if( w2LastMod == null ) {
058            log.error( "NULL MODIFY DATE WITH " + w2.getName() );
059            return 0;
060        }
061
062        // This gets most recent on top
063        int timecomparison = w2LastMod.compareTo( w1LastMod );
064
065        if( timecomparison == 0 ) {
066            return w1.compareTo( w2 );
067        }
068
069        return timecomparison;
070    }
071
072}