View Javadoc

1   package org.apache.helix.store.zk;
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.Collections;
23  import java.util.HashSet;
24  import java.util.List;
25  import java.util.Set;
26  
27  import org.apache.zookeeper.data.Stat;
28  
29  public class ZNode
30  {
31    // used for a newly created item, because zkclient.create() doesn't return stat
32    // or used for places where we don't care about stat
33    public static final Stat ZERO_STAT = new Stat();
34    
35    final String _zkPath;
36    private Stat _stat;
37    Object _data;
38    Set<String> _childSet;
39  
40    public ZNode(String zkPath, Object data, Stat stat)
41    {
42      _zkPath = zkPath;
43      _childSet = Collections.<String>emptySet(); // new HashSet<String>();
44      _data = data;
45      _stat = stat;
46    }
47  
48    public void removeChild(String child)
49    {
50      if (_childSet != Collections.<String>emptySet())
51      {
52        _childSet.remove(child);
53      }
54    }
55    
56    public void addChild(String child)
57    {
58      if (_childSet == Collections.<String>emptySet())
59      {
60        _childSet = new HashSet<String>();
61      }
62      
63      _childSet.add(child);
64    }
65    
66    public void addChildren(List<String> children)
67    {
68      if (children != null && !children.isEmpty())
69      {
70        if (_childSet == Collections.<String>emptySet())
71        {
72          _childSet = new HashSet<String>();
73        }
74  
75        _childSet.addAll(children);
76      }
77    }
78  
79    public boolean hasChild(String child)
80    {
81      return _childSet.contains(child);
82    }
83  
84    public Set<String> getChildSet()
85    {
86      return _childSet;
87    }
88    
89    public void setData(Object data)
90    {
91  //    System.out.println("setData: " + _zkPath + ", data: " + data);
92      _data= data;    
93    }
94    
95    public Object getData()
96    {
97      return _data;
98    }
99    
100   public void setStat(Stat stat)
101   {
102     _stat = stat;
103   }
104   
105   public Stat getStat()
106   {
107     return _stat;
108   }
109   
110   public void setChildSet(List<String> childNames)
111   {
112     if (childNames != null && !childNames.isEmpty())
113     {
114       if (_childSet == Collections.<String>emptySet())
115       {
116         _childSet = new HashSet<String>();
117       }
118 
119       _childSet.clear();
120       _childSet.addAll(childNames);
121     }
122   }
123   
124   @Override
125   public String toString()
126   {
127     return _zkPath + ", " + _data + ", " + _childSet + ", " + _stat;
128   }
129 }