View Javadoc

1   package org.apache.helix.controller.stages;
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.HashMap;
23  import java.util.Map;
24  
25  import org.apache.log4j.Logger;
26  
27  public class ClusterEvent
28  {
29    private static final Logger logger = Logger.getLogger(ClusterEvent.class
30        .getName());
31    private final String _eventName;
32    private final Map<String, Object> _eventAttributeMap;
33  
34    public ClusterEvent(String name)
35    {
36      _eventName = name;
37      _eventAttributeMap = new HashMap<String, Object>();
38    }
39  
40    public void addAttribute(String attrName, Object attrValue)
41    {
42      if (logger.isTraceEnabled())
43      {
44        logger.trace("Adding attribute:" + attrName);
45        logger.trace(" attribute value:" + attrValue);
46      }
47     
48      _eventAttributeMap.put(attrName, attrValue);
49    }
50  
51    public String getName()
52    {
53      return _eventName;
54    }
55  
56    @SuppressWarnings("unchecked")
57    public <T extends Object> T getAttribute(String attrName)
58    {
59      Object ret = _eventAttributeMap.get(attrName);
60      if (ret != null)
61      {
62        return (T) ret;
63      }
64      return null;
65    }
66    
67    @Override
68    public String toString()
69    {
70      StringBuilder sb = new StringBuilder();
71      sb.append("name:"+ _eventName).append("\n");
72      for(String key:_eventAttributeMap.keySet()){
73        sb.append(key).append(":").append(_eventAttributeMap.get(key)).append("\n");
74      }
75      return sb.toString();
76    }
77  }