View Javadoc

1   package org.apache.helix.util;
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.lang.reflect.Constructor;
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.apache.helix.ZNRecord;
29  import org.apache.log4j.Logger;
30  
31  
32  //TODO find a proper place for these methods
33  public final class ZNRecordUtil
34  {
35    private static final Logger logger = Logger.getLogger(ZNRecordUtil.class.getName());
36  
37    private ZNRecordUtil()
38    {
39    }
40  
41    public static ZNRecord find(String id, List<ZNRecord> list)
42    {
43      for (ZNRecord record : list)
44      {
45        if (record.getId() != null && record.getId().equals(id))
46        {
47          return record;
48        }
49      }
50      return null;
51    }
52  
53    public static Map<String, ZNRecord> convertListToMap(List<ZNRecord> recordList)
54    {
55      Map<String, ZNRecord> recordMap = new HashMap<String, ZNRecord>();
56      for (ZNRecord record : recordList)
57      {
58        if (record.getId() != null)
59        {
60          recordMap.put(record.getId(), record);
61        }
62      }
63      return recordMap;
64    }
65  
66    public static <T extends Object> List<T> convertListToTypedList(List<ZNRecord> recordList,
67                                                                    Class<T> clazz)
68    {
69      List<T> list = new ArrayList<T>();
70      for (ZNRecord record : recordList)
71      {
72        if (record.getId() == null)
73        {
74          logger.error("Invalid record: Id missing in " + record);
75          continue;
76        }
77        try
78        {
79  
80          Constructor<T> constructor = clazz.getConstructor(new Class[] { ZNRecord.class });
81          T instance = constructor.newInstance(record);
82          list.add(instance);
83        }
84        catch (Exception e)
85        {
86          logger.error("Error creating an Object of type:" + clazz.getCanonicalName(), e);
87        }
88      }
89      return list;
90    }
91  
92    public static <T extends Object> Map<String, T> convertListToTypedMap(List<ZNRecord> recordList,
93                                                                          Class<T> clazz)
94    {
95      Map<String, T> map = new HashMap<String, T>();
96      for (ZNRecord record : recordList)
97      {
98        if (record.getId() == null)
99        {
100         logger.error("Invalid record: Id missing in " + record);
101         continue;
102       }
103       try
104       {
105 
106         Constructor<T> constructor = clazz.getConstructor(new Class[] { ZNRecord.class });
107         T instance = constructor.newInstance(record);
108         map.put(record.getId(), instance);
109       }
110       catch (Exception e)
111       {
112         logger.error("Error creating an Object of type:" + clazz.getCanonicalName(), e);
113       }
114     }
115     return map;
116   }
117 
118   public static <T extends Object> List<T> convertMapToList(Map<String, T> map)
119   {
120     List<T> list = new ArrayList<T>();
121     for (T t : map.values())
122     {
123       list.add(t);
124     }
125     return list;
126   }
127 }