/[classpath]/classpath/gnu/java/beans/decoder/GrowableArrayContext.java
ViewVC logotype

Diff of /classpath/gnu/java/beans/decoder/GrowableArrayContext.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by rschuster, Sun Jan 2 23:53:42 2005 UTC revision 1.2 by robilad, Wed Feb 2 00:10:55 2005 UTC
# Line 37  exception statement from your version. * Line 37  exception statement from your version. *
37  package gnu.java.beans.decoder;  package gnu.java.beans.decoder;
38    
39  import java.lang.reflect.Array;  import java.lang.reflect.Array;
 import java.util.ArrayList;  
40    
41  /** A Context implementation for a growable array. The array  /** A Context implementation for a growable array. The array
42   * elements have to be set using expressions.   * elements have to be set using expressions.
# Line 46  import java.util.ArrayList; Line 45  import java.util.ArrayList;
45   */   */
46  class GrowableArrayContext extends AbstractContext  class GrowableArrayContext extends AbstractContext
47  {  {
48        private static final int INITIAL_SIZE = 16;
49        
50      private Class klass;      private Class klass;
     private ArrayList list = new ArrayList();  
51      private Object array;      private Object array;
52        private int length;
53        
54      GrowableArrayContext(String id, Class newClass)      GrowableArrayContext(String id, Class newClass)
55      {      {
56          setId(id);          setId(id);
57          klass = newClass;          klass = newClass;
58            array = Array.newInstance(klass, INITIAL_SIZE);
59      }      }
60    
61      /* (non-Javadoc)      /* (non-Javadoc)
# Line 61  class GrowableArrayContext extends Abstr Line 63  class GrowableArrayContext extends Abstr
63       */       */
64      public void addParameterObject(Object o) throws AssemblyException      public void addParameterObject(Object o) throws AssemblyException
65      {      {
66          if (!klass.isInstance(o))        if (length == Array.getLength(array))
67              throw new AssemblyException(          {
68                  new IllegalArgumentException(            Object tmp = Array.newInstance(klass, length * 2);
69                      "Cannot add object "            System.arraycopy(array, 0, tmp, 0, length);
70                          + o            array = tmp;
71                          + " to array where the elements are of class "          }
72                          + klass));          
73          try {
74          list.add(o);          Array.set(array, length++, o);
75          } catch(IllegalArgumentException iae) {
76            throw new AssemblyException(iae);
77          }
78      }      }
79    
80      /* (non-Javadoc)      /* (non-Javadoc)
# Line 86  class GrowableArrayContext extends Abstr Line 91  class GrowableArrayContext extends Abstr
91       */       */
92      public Object endContext(Context outerContext) throws AssemblyException      public Object endContext(Context outerContext) throws AssemblyException
93      {      {
94          if (array == null)          if (length != Array.getLength(array))
95          {            {
96              array = Array.newInstance(klass, list.size());              Object tmp = Array.newInstance(klass, length);
97                System.arraycopy(array, 0, tmp, 0, length);
98              for (int i = 0; i < list.size(); i++)              array = tmp;
99                  Array.set(array, i, list.get(i));            }
100          }          
   
101          return array;          return array;
102      }      }
103    
# Line 112  class GrowableArrayContext extends Abstr Line 116  class GrowableArrayContext extends Abstr
116       */       */
117      public void set(int index, Object o) throws AssemblyException      public void set(int index, Object o) throws AssemblyException
118      {      {
119          if (array == null)        try {
120          {          Array.set(array, index, o);
121              if (klass.isInstance(o))        } catch(IllegalArgumentException iae) {
122                  list.add(index, o);          throw new AssemblyException(iae);  
123              else        }
                 throw new AssemblyException(  
                     new IllegalArgumentException("Argument is not compatible to array component type."));  
         }  
         else  
             Array.set(array, index, o);  
124      }      }
125    
126      /* (non-Javadoc)      /* (non-Javadoc)
# Line 129  class GrowableArrayContext extends Abstr Line 128  class GrowableArrayContext extends Abstr
128       */       */
129      public Object get(int index) throws AssemblyException      public Object get(int index) throws AssemblyException
130      {      {
131          if (array == null)        return Array.get(array, index);
             return list.get(index);  
         else  
             return Array.get(array, index);  
132      }      }
133    
134      public Object getResult()      public Object getResult()

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26