View Javadoc

1   package org.apache.helix.store;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Comparator;
23  
24  import org.apache.log4j.Logger;
25  
26  public class PropertyJsonComparator<T> implements Comparator<T>
27  {
28    static private Logger LOG = Logger.getLogger(PropertyJsonComparator.class);
29    private final PropertyJsonSerializer<T> _serializer;
30    
31    public PropertyJsonComparator(Class<T> clazz)
32    {
33      _serializer = new PropertyJsonSerializer<T>(clazz);
34    }
35  
36    @Override
37    public int compare(T arg0, T arg1)
38    {
39      if (arg0 == null && arg1 == null)
40      {
41        return 0;
42      }
43      else if (arg0 == null && arg1 != null)
44      {
45        return -1;
46      }
47      else if (arg0 != null && arg1 == null)
48      {
49        return 1;
50      }
51      else
52      {
53        try
54        {
55          String s0 = new String(_serializer.serialize(arg0));
56          String s1 = new String(_serializer.serialize(arg1));
57  
58          return s0.compareTo(s1);
59        }
60        catch (PropertyStoreException e)
61        {
62          // e.printStackTrace();
63          LOG.warn(e.getMessage());
64          return -1;
65        }
66      }
67    }
68  
69  }