View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    * 
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   * 
19   */
20  package org.apache.directory.api.ldap.model.schema.comparators;
21  
22  
23  import java.io.IOException;
24  import java.text.ParseException;
25  
26  import org.apache.directory.api.i18n.I18n;
27  import org.apache.directory.api.ldap.model.schema.LdapComparator;
28  import org.apache.directory.api.ldap.model.schema.PrepareString;
29  import org.apache.directory.api.util.GeneralizedTime;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  
34  /**
35   * A class for the generalizedTimeOrderingMatch matchingRule (RFC 4517, par. 4.2.17)
36   * 
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   */
39  public class GeneralizedTimeComparator extends LdapComparator<String>
40  {
41      /** The serial version UID */
42      private static final long serialVersionUID = 2L;
43  
44      /** A logger for this class */
45      private static final Logger LOG = LoggerFactory.getLogger( GeneralizedTimeComparator.class );
46  
47  
48      /**
49       * The GeneralizedTimeComparator constructor. Its OID is the
50       * generalizedTimeOrderingMatch matching rule OID.
51       */
52      public GeneralizedTimeComparator( String oid )
53      {
54          super( oid );
55      }
56  
57  
58      /**
59       * Implementation of the Compare method
60       */
61      @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "ES_COMPARING_PARAMETER_STRING_WITH_EQ",
62          justification = "false positive")
63      public int compare( String backendValue, String assertValue )
64      {
65          LOG.debug( "comparing generalizedTimeOrdering objects '{}' with '{}'", backendValue, assertValue );
66  
67          // First, shortcut the process by comparing
68          // references. If they are equals, then o1 and o2
69          // reference the same object
70          if ( backendValue == assertValue )
71          {
72              return 0;
73          }
74  
75          // Then, deal with one of o1 or o2 being null
76          // Both can't be null, because then they would
77          // have been caught by the previous test
78          if ( ( backendValue == null ) || ( assertValue == null ) )
79          {
80              return ( backendValue == null ? -1 : 1 );
81          }
82  
83          // Both objects must be stored as String for generalized tim.
84          // But we need to normalize the values first.
85          GeneralizedTime backendTime;
86          try
87          {
88              String prepared = PrepareString.normalize( backendValue, PrepareString.StringType.DIRECTORY_STRING );
89              backendTime = new GeneralizedTime( prepared );
90          }
91          catch ( IOException ioe )
92          {
93              throw new IllegalArgumentException( I18n.err( I18n.ERR_04224, backendValue ) );
94          }
95          catch ( ParseException pe )
96          {
97              throw new IllegalArgumentException( I18n.err( I18n.ERR_04224, backendValue ) );
98          }
99  
100         GeneralizedTime assertTime;
101         try
102         {
103             String prepared = PrepareString.normalize( assertValue, PrepareString.StringType.DIRECTORY_STRING );
104             assertTime = new GeneralizedTime( prepared );
105         }
106         catch ( IOException ioe )
107         {
108             throw new IllegalArgumentException( I18n.err( I18n.ERR_04224, assertValue ) );
109         }
110         catch ( ParseException pe )
111         {
112             throw new IllegalArgumentException( I18n.err( I18n.ERR_04224, assertValue ) );
113         }
114 
115         return backendTime.compareTo( assertTime );
116     }
117 }