01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package java.beans;
19:
20: import java.util.Collection;
21: import java.util.Iterator;
22:
23: class UtilCollectionPersistenceDelegate extends
24: DefaultPersistenceDelegate {
25: @Override
26: @SuppressWarnings("nls")
27: protected void initialize(Class<?> type, Object oldInstance,
28: Object newInstance, Encoder enc) {
29:
30: Collection<?> oldList = (Collection) oldInstance, newList = (Collection) newInstance;
31: Iterator<?> oldIterator = oldList.iterator(), newIterator = newList
32: .iterator();
33: for (; oldIterator.hasNext();) {
34: Expression getterExp = new Expression(oldIterator, "next",
35: null);
36: try {
37: // Calculate the old value of the property
38: Object oldVal = getterExp.getValue();
39:
40: Object newVal = null;
41: try {
42: newVal = new Expression(newIterator, "next", null)
43: .getValue();
44: } catch (ArrayIndexOutOfBoundsException ex) {
45: // The newInstance has no elements, so current property
46: // value remains null
47: }
48: /*
49: * Make the target value and current property value equivalent
50: * in the new environment
51: */
52: if (null == oldVal) {
53: if (null != newVal) {
54: // Set to null
55: Statement setterStm = new Statement(
56: oldInstance, "add",
57: new Object[] { null });
58: enc.writeStatement(setterStm);
59: }
60: } else {
61: PersistenceDelegate pd = enc
62: .getPersistenceDelegate(oldVal.getClass());
63: if (!pd.mutatesTo(oldVal, newVal)) {
64: Statement setterStm = new Statement(
65: oldInstance, "add",
66: new Object[] { oldVal });
67: enc.writeStatement(setterStm);
68: }
69: }
70: } catch (Exception ex) {
71: enc.getExceptionListener().exceptionThrown(ex);
72: }
73: }
74: }
75: }
|