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  package org.apache.myfaces.trinidad.bean.util;
20  
21  import java.util.Iterator;
22  
23  import java.util.NoSuchElementException;
24  import org.apache.myfaces.trinidad.bean.FacesBean;
25  import org.apache.myfaces.trinidad.bean.PropertyKey;
26  
27  /**
28   * Utility class for tracking indexed PropertyKeys in a bitmask
29   */
30  public class PropertyTracker implements Iterable<PropertyKey>
31  {
32    /**
33     * Constructor
34     * @param type Facesbean Type for the bean that owns the tracker
35     */
36    public PropertyTracker(FacesBean.Type type)
37    {
38      _type = type;
39    }
40    
41    /**
42     * Adds a property to the bitmask
43     * @param key property key to be tracked in a bitmask
44     */
45    public void addProperty(PropertyKey key)
46    {
47      int index = key.getIndex();
48      _checkIndex(index);
49      
50      _propertiesMask |= (1L << index);
51    }
52    
53    /**
54     * Stops tracking property in a bitmask
55     * @param key property key that should be removed from the bitmask
56     */
57    public void removeProperty(PropertyKey key)
58    {
59      int index = key.getIndex();
60      _checkIndex(index);
61      
62      long mask  = ~(1L << index);
63      _propertiesMask &= mask;
64    }
65    
66    /**
67     * Provides Iterator of the propoerty being tracked in a bitmask
68     * @return iterator of property keys
69     */
70    public Iterator<PropertyKey> iterator()
71    {
72      return new PropertyBitIterator();
73    }
74    
75    private void _checkIndex(int index)
76    {
77      if (index < 0 || index >= 64)
78      {
79        throw new IllegalArgumentException("Only indexed properties may be tracked");
80      }
81    }
82    
83    private class PropertyBitIterator implements Iterator<PropertyKey>
84    {
85      @Override
86      public void remove()
87      {
88        throw new UnsupportedOperationException();
89      }
90      
91      @Override
92      public boolean hasNext()
93      {
94        if (_pos >= 64)
95          return false;
96        
97        return _propertiesMask >= (1L << _pos); 
98      }
99      
100     @Override
101     public PropertyKey next()
102     {
103       while (_pos < 64)
104       {
105         int current = _pos++;
106         
107         if ((_propertiesMask &  (1L << current)) != 0)
108         {
109           return _type.findKey(current);
110         }
111       }
112       throw new NoSuchElementException();
113     }
114     
115     private int _pos = 0;
116   }
117   
118   private long _propertiesMask = 0;
119   private FacesBean.Type _type;
120 }