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