|
|||||||||||||||||||
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
PersistentPropertyData.java | 90% | 97.5% | 100% | 96.6% |
|
1 | // Copyright 2005 The Apache Software Foundation | |
2 | // | |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | // you may not use this file except in compliance with the License. | |
5 | // You may obtain a copy of the License at | |
6 | // | |
7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
8 | // | |
9 | // Unless required by applicable law or agreed to in writing, software | |
10 | // distributed under the License is distributed on an "AS IS" BASIS, | |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | // See the License for the specific language governing permissions and | |
13 | // limitations under the License. | |
14 | ||
15 | package org.apache.tapestry.record; | |
16 | ||
17 | import java.util.ArrayList; | |
18 | import java.util.HashMap; | |
19 | import java.util.Iterator; | |
20 | import java.util.List; | |
21 | import java.util.Map; | |
22 | ||
23 | import org.apache.hivemind.util.Defense; | |
24 | ||
25 | /** | |
26 | * Stores persistent property changes concerning a single page. The data may be stored as an encoded | |
27 | * string and the PPD can turn between encoded and object form. | |
28 | * | |
29 | * @author Howard M. Lewis Ship | |
30 | * @since 4.0 | |
31 | */ | |
32 | public class PersistentPropertyData | |
33 | { | |
34 | /** | |
35 | * Keyed on {@link org.apache.tapestry.record.ChangeKey}, values are new objects. | |
36 | */ | |
37 | ||
38 | private Map _changes; | |
39 | ||
40 | private String _encoded; | |
41 | ||
42 | private final PersistentPropertyDataEncoder _encoder; | |
43 | ||
44 | /** | |
45 | * Creates a new data using the specified encoder. The set of page changes is initially empty. | |
46 | */ | |
47 | ||
48 | 8 | public PersistentPropertyData(PersistentPropertyDataEncoder encoder) |
49 | { | |
50 | 8 | Defense.notNull(encoder, "encoder"); |
51 | ||
52 | 8 | _encoder = encoder; |
53 | 8 | _changes = new HashMap(); |
54 | } | |
55 | ||
56 | 4 | public String getEncoded() |
57 | { | |
58 | 4 | if (_encoded == null) |
59 | 1 | _encoded = encode(); |
60 | ||
61 | 4 | return _encoded; |
62 | } | |
63 | ||
64 | 6 | public List getPageChanges() |
65 | { | |
66 | 6 | if (_changes == null) |
67 | { | |
68 | 2 | List pageChanges = _encoder.decodePageChanges(_encoded); |
69 | ||
70 | 2 | _changes = decode(pageChanges); |
71 | ||
72 | 2 | return pageChanges; |
73 | } | |
74 | ||
75 | 4 | return createPageChangeList(); |
76 | } | |
77 | ||
78 | 4 | public void store(String componentPath, String propertyName, Object newValue) |
79 | { | |
80 | 4 | Defense.notNull(propertyName, "propertyName"); |
81 | ||
82 | 4 | if (_changes == null) |
83 | 0 | _changes = decode(_encoder.decodePageChanges(_encoded)); |
84 | ||
85 | 4 | ChangeKey key = new ChangeKey(componentPath, propertyName); |
86 | ||
87 | 4 | _changes.put(key, newValue); |
88 | ||
89 | // With the new (or changed) value, the encoded string is no | |
90 | // longer valid. | |
91 | ||
92 | 4 | _encoded = null; |
93 | } | |
94 | ||
95 | 5 | public void storeEncoded(String encoded) |
96 | { | |
97 | 5 | Defense.notNull(encoded, "encoded"); |
98 | ||
99 | 5 | _encoded = encoded; |
100 | ||
101 | // The decoded data is no longer valid now. | |
102 | ||
103 | 5 | _changes = null; |
104 | } | |
105 | ||
106 | 5 | private List createPageChangeList() |
107 | { | |
108 | 5 | List result = new ArrayList(); |
109 | ||
110 | 5 | Iterator i = _changes.entrySet().iterator(); |
111 | ||
112 | 5 | while (i.hasNext()) |
113 | { | |
114 | 5 | Map.Entry me = (Map.Entry) i.next(); |
115 | ||
116 | 5 | ChangeKey changeKey = (ChangeKey) me.getKey(); |
117 | 5 | Object value = me.getValue(); |
118 | ||
119 | 5 | PropertyChange change = new PropertyChangeImpl(changeKey.getComponentPath(), changeKey |
120 | .getPropertyName(), value); | |
121 | ||
122 | 5 | result.add(change); |
123 | } | |
124 | ||
125 | 5 | return result; |
126 | } | |
127 | ||
128 | 1 | private String encode() |
129 | { | |
130 | 1 | List changes = createPageChangeList(); |
131 | ||
132 | 1 | return _encoder.encodePageChanges(changes); |
133 | } | |
134 | ||
135 | 2 | private Map decode(List pageChanges) |
136 | { | |
137 | 2 | Map result = new HashMap(); |
138 | ||
139 | 2 | Iterator i = pageChanges.iterator(); |
140 | 2 | while (i.hasNext()) |
141 | { | |
142 | 2 | PropertyChange pc = (PropertyChange) i.next(); |
143 | ||
144 | 2 | String propertyName = pc.getPropertyName(); |
145 | 2 | String componentPath = pc.getComponentPath(); |
146 | ||
147 | 2 | ChangeKey key = new ChangeKey(componentPath, propertyName); |
148 | ||
149 | 2 | result.put(key, pc.getNewValue()); |
150 | } | |
151 | ||
152 | 2 | return result; |
153 | } | |
154 | } |
|