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 *    https://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.directory.api.ldap.model.schema.comparators;
021
022
023import java.text.ParseException;
024
025import org.apache.directory.api.i18n.I18n;
026import org.apache.directory.api.ldap.model.schema.LdapComparator;
027import org.apache.directory.api.util.GeneralizedTime;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031
032/**
033 * A class for the generalizedTimeOrderingMatch matchingRule (RFC 4517, par. 4.2.17)
034 * 
035 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
036 */
037public class GeneralizedTimeComparator extends LdapComparator<String>
038{
039    /** The serial version UID */
040    private static final long serialVersionUID = 2L;
041
042    /** A logger for this class */
043    private static final Logger LOG = LoggerFactory.getLogger( GeneralizedTimeComparator.class );
044
045
046    /**
047     * The GeneralizedTimeComparator constructor. Its OID is the
048     * generalizedTimeOrderingMatch matching rule OID.
049     * 
050     * @param oid The Comparator's OID
051     */
052    public GeneralizedTimeComparator( String oid )
053    {
054        super( oid );
055    }
056
057
058    /**
059     * {@inheritDoc}
060     */
061    public int compare( String backendValue, String assertValue )
062    {
063        if ( LOG.isDebugEnabled() )
064        {
065            LOG.debug( I18n.msg( I18n.MSG_13753_COMPARING_GENERALIZED_TIME_ORDERING, backendValue, assertValue ) );
066        }
067
068        // First, shortcut the process by comparing
069        // references. If they are equals, then o1 and o2
070        // reference the same object
071        if ( backendValue == assertValue )
072        {
073            return 0;
074        }
075
076        // Then, deal with one of o1 or o2 being null
077        // Both can't be null, because then they would
078        // have been caught by the previous test
079        if ( ( backendValue == null ) || ( assertValue == null ) )
080        {
081            return backendValue == null ? -1 : 1;
082        }
083
084        // Both objects must be stored as String for generalized tim.
085        // But we need to normalize the values first.
086        GeneralizedTime backendTime;
087        try
088        {
089            backendTime = new GeneralizedTime( backendValue );
090        }
091        catch ( ParseException pe )
092        {
093            throw new IllegalArgumentException( I18n.err( I18n.ERR_13724_INVALID_VALUE, backendValue ), pe );
094        }
095
096        GeneralizedTime assertTime;
097        
098        try
099        {
100            assertTime = new GeneralizedTime( assertValue );
101        }
102        catch ( ParseException pe )
103        {
104            throw new IllegalArgumentException( I18n.err( I18n.ERR_13724_INVALID_VALUE, assertValue ), pe );
105        }
106
107        return backendTime.compareTo( assertTime );
108    }
109}