1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
29
30 public class PropertyTracker implements Iterable<PropertyKey>
31 {
32
33
34
35
36 public PropertyTracker(FacesBean.Type type)
37 {
38 _type = type;
39 }
40
41
42
43
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
55
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
68
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 }