harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From le...@apache.org
Subject svn commit: r557846 - in /harmony/enhanced/classlib/trunk/modules/beans/src: main/java/java/beans/Encoder.java main/java/java/beans/UtilListPersistenceDelegate.java test/java/org/apache/harmony/beans/tests/java/beans/PersistenceDelegateTest.java
Date Fri, 20 Jul 2007 02:35:30 GMT
Author: leoli
Date: Thu Jul 19 19:35:28 2007
New Revision: 557846

URL: http://svn.apache.org/viewvc?view=rev&rev=557846
Log:
Apply patch for HARMONY-4488([classlib][beans] Add specific persistence delegate for j.u.List
to improve performance).

Added:
    harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/UtilListPersistenceDelegate.java
  (with props)
Modified:
    harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Encoder.java
    harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/PersistenceDelegateTest.java

Modified: harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Encoder.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Encoder.java?view=diff&rev=557846&r1=557845&r2=557846
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Encoder.java (original)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Encoder.java Thu
Jul 19 19:35:28 2007
@@ -188,6 +188,10 @@
 		if (registeredPD != null) {
 			return registeredPD;
 		}
+        
+        if (java.util.List.class.isAssignableFrom(type)) {
+            return new UtilListPersistenceDelegate();
+        }
 		
         if (Collection.class.isAssignableFrom(type)) {
             return new UtilCollectionPersistenceDelegate();

Added: harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/UtilListPersistenceDelegate.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/UtilListPersistenceDelegate.java?view=auto&rev=557846
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/UtilListPersistenceDelegate.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/UtilListPersistenceDelegate.java
Thu Jul 19 19:35:28 2007
@@ -0,0 +1,72 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package java.beans;
+
+import java.util.List;
+
+class UtilListPersistenceDelegate extends DefaultPersistenceDelegate {
+    protected void initialize(Class<?> type, Object oldInstance,
+            Object newInstance, Encoder enc) {
+
+        List list = (List) oldInstance;
+        int size = list.size();
+        for (int i = 0; i < size; i++) {
+            Expression getterExp = new Expression(oldInstance, "get",
+                    new Object[] { i });
+            try {
+                // Calculate the old value of the property
+                Object oldVal = getterExp.getValue();
+                // Write the getter expression to the encoder
+                enc.writeExpression(getterExp);
+                // Get the target value that exists in the new environment
+                Object targetVal = enc.get(oldVal);
+                // Get the current property value in the new environment
+                Object newVal = null;
+                try {
+                    newVal = new Expression(newInstance, "get",
+                        new Object[] { i }).getValue();
+                } catch (IndexOutOfBoundsException ex) {
+                    // The newInstance has no elements, so current property
+                    // value remains null
+                }
+                /*
+                 * Make the target value and current property value equivalent
+                 * in the new environment
+                 */
+                if (null == targetVal) {
+                    if (null != newVal) {
+                        // Set to null
+                        Statement setterStm = new Statement(oldInstance, "add",
+                                new Object[] { null });
+                        enc.writeStatement(setterStm);
+                    }
+                } else {
+                    PersistenceDelegate pd = enc
+                            .getPersistenceDelegate(targetVal.getClass());
+                    if (!pd.mutatesTo(targetVal, newVal)) {
+                        Statement setterStm = new Statement(oldInstance, "add",
+                                new Object[] { oldVal });
+                        enc.writeStatement(setterStm);
+                    }
+                }
+            } catch (Exception ex) {
+                enc.getExceptionListener().exceptionThrown(ex);
+            }
+        }
+    }
+}
\ No newline at end of file

Propchange: harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/UtilListPersistenceDelegate.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/PersistenceDelegateTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/PersistenceDelegateTest.java?view=diff&rev=557846&r1=557845&r2=557846
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/PersistenceDelegateTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/PersistenceDelegateTest.java
Thu Jul 19 19:35:28 2007
@@ -21,26 +21,10 @@
 import java.awt.event.*;
 import java.awt.font.TextAttribute;
 import java.awt.dnd.DropTarget;
-import java.beans.Encoder;
-import java.beans.Expression;
-import java.beans.PersistenceDelegate;
-import java.beans.PropertyChangeEvent;
-import java.beans.PropertyChangeListener;
-import java.beans.Statement;
-import java.beans.XMLDecoder;
-import java.beans.XMLEncoder;
-import java.io.BufferedOutputStream;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.DataInputStream;
-import java.io.IOException;
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-import java.util.EmptyStackException;
-import java.util.LinkedList;
-import java.util.Locale;
-import java.util.Stack;
-import java.util.TreeMap;
+import java.beans.*;
+import java.io.*;
+import java.lang.reflect.*;
+import java.util.*;
 
 import javax.swing.*;
 
@@ -659,7 +643,7 @@
         XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
             byteArrayOutputStream));
 
-        java.awt.List list = new List();
+        java.awt.List list = new java.awt.List();
         list.setBounds(0, 0, 10, 10);
         list.add(new PopupMenu("popupMenu"));
         list.add("1");
@@ -670,7 +654,7 @@
         DataInputStream stream = new DataInputStream(new ByteArrayInputStream(
                 byteArrayOutputStream.toByteArray()));
         XMLDecoder decoder = new XMLDecoder(stream);
-        List aList = (List) decoder.readObject();
+        java.awt.List aList = (java.awt.List) decoder.readObject();
         assertEquals(list.getItem(0), aList.getItem(0));
         assertEquals(list.getHeight(), aList.getHeight());
         assertEquals(list.getBackground(), aList.getBackground());
@@ -817,6 +801,85 @@
         TreeMap aMap = (TreeMap) decoder.readObject();
         assertEquals(map.size(), aMap.size());
         assertEquals(map.get(10), aMap.get(10));
+    }
+    
+    public void test_writeObject_java_util_List() {
+        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+        XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
+            byteArrayOutputStream));
+        Vector vector = new Vector();
+        vector.add("test");
+        vector.add(1);
+
+        encoder.writeObject(vector);
+        encoder.close();
+        DataInputStream stream = new DataInputStream(new ByteArrayInputStream(
+                byteArrayOutputStream.toByteArray()));
+        XMLDecoder decoder = new XMLDecoder(stream);
+        Vector aVector = (Vector) decoder.readObject();
+        assertEquals(vector, aVector);
+        assertEquals(2, aVector.size());
+        assertEquals(vector.get(0), aVector.get(0));
+        assertEquals(1, aVector.get(1));
+        
+        Vector v = new Vector();
+        v.add("The first item.");
+        v.add("The second item.");
+        Vector v2 = new Vector();
+        v2.add("A nested item.");
+        v.add(v2);
+        
+        byteArrayOutputStream = new ByteArrayOutputStream();
+        encoder = new XMLEncoder(byteArrayOutputStream);
+        encoder.writeObject(v);
+        encoder.flush();
+        encoder.close();
+        
+        stream = new DataInputStream(new ByteArrayInputStream(
+                byteArrayOutputStream.toByteArray()));
+        decoder = new XMLDecoder(stream);
+        aVector = (Vector) decoder.readObject();
+        assertEquals(v, aVector);
+        assertEquals(3, aVector.size());
+        assertEquals(v.get(0), aVector.get(0));
+        assertEquals(v.get(2), aVector.get(2));
+    }
+
+    public void test_writeObject_java_util_AbstractList(){
+        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+        XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
+            byteArrayOutputStream));
+        ArrayList<Integer> arrayList = new ArrayList<Integer>();
+        arrayList.add(10);
+
+        encoder.writeObject(arrayList);
+        encoder.close();
+        DataInputStream stream = new DataInputStream(new ByteArrayInputStream(
+                byteArrayOutputStream.toByteArray()));
+        XMLDecoder decoder = new XMLDecoder(stream);
+        ArrayList aList = (ArrayList) decoder.readObject();
+        assertEquals(arrayList.size(), aList.size());
+        assertEquals(arrayList.get(0), arrayList.get(0));
+    }
+
+    public void test_writeObject_java_util_AbstractMap() {
+        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+        XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
+            byteArrayOutputStream));
+
+        HashMap<Integer, String> hMap = new HashMap<Integer, String>();
+        hMap.put(1, "1");
+        hMap.put(2, "test");
+
+        encoder.writeObject(hMap);
+        encoder.close();
+        DataInputStream stream = new DataInputStream(new ByteArrayInputStream(
+                byteArrayOutputStream.toByteArray()));
+        XMLDecoder decoder = new XMLDecoder(stream);
+        HashMap aHmap = (HashMap) decoder.readObject();
+        assertEquals(hMap.size(), aHmap.size());
+        assertEquals(hMap.get(1), aHmap.get(1));
+        assertEquals("test", aHmap.get(2));
     }
 
     // <--



Mime
View raw message