Author: nick
Date: Sun Apr 20 11:40:27 2008
New Revision: 649952

URL: http://svn.apache.org/viewvc?rev=649952&view=rev
Log:
ArrayList implementation and tests

Added:
    commons/sandbox/me/trunk/src/org/apache/commons/me/util/ArrayList.java   
(with props)
    commons/sandbox/me/trunk/src/org/apache/commons/me/util/List.java   (with 
props)
    
commons/sandbox/me/trunk/test-src/org/apache/commons/me/util/TestArrayList.java 
  (with props)

Added: commons/sandbox/me/trunk/src/org/apache/commons/me/util/ArrayList.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/me/trunk/src/org/apache/commons/me/util/ArrayList.java?rev=649952&view=auto
==============================================================================
--- commons/sandbox/me/trunk/src/org/apache/commons/me/util/ArrayList.java 
(added)
+++ commons/sandbox/me/trunk/src/org/apache/commons/me/util/ArrayList.java Sun 
Apr 20 11:40:27 2008
@@ -0,0 +1,59 @@
+/* ====================================================================
+   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 org.apache.commons.me.util;
+
+import java.util.Vector;
+
+/**
+ * An implementation of java.util.Array for JavaME.
+ * Uses a Vector to implement the functionality.
+ */
+public class ArrayList extends Vector implements List {
+       public boolean add(Object o) {
+               addElement(o);
+               return true;
+       }
+       public boolean addAll(List list) {
+               for(int i=0; i<list.size(); i++) {
+                       addElement(list.get(i));
+               }
+               return true;
+       }
+
+       public void clear() {
+               removeAllElements();
+       }
+
+       public Object get(int index) {
+               return elementAt(index);
+       }
+       public Object set(int index, Object o) {
+               setElementAt(o, index);
+               return o;
+       }
+
+       public Object[] toArray() {
+               return toArray(new Object[size()]);
+       }
+       public Object[] toArray(Object[] o) {
+               if(o.length < size()) {
+                       throw new IllegalArgumentException("Passed array must 
be at least the same size as the ArrayList, as JavaME won't allow us to 
dynamically create a new, larger array!");
+               }
+               copyInto(o);
+               return o;
+       }
+}

Propchange: 
commons/sandbox/me/trunk/src/org/apache/commons/me/util/ArrayList.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/sandbox/me/trunk/src/org/apache/commons/me/util/List.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/me/trunk/src/org/apache/commons/me/util/List.java?rev=649952&view=auto
==============================================================================
--- commons/sandbox/me/trunk/src/org/apache/commons/me/util/List.java (added)
+++ commons/sandbox/me/trunk/src/org/apache/commons/me/util/List.java Sun Apr 
20 11:40:27 2008
@@ -0,0 +1,38 @@
+/* ====================================================================
+   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 org.apache.commons.me.util;
+
+/**
+ * A definition of java.util.List for JavaME
+ */
+public interface List {
+       public boolean add(Object o);
+       public boolean addAll(List l);
+
+       public void clear();
+       public int size();
+
+       public boolean contains(Object o);
+       public Object get(int index);
+       public Object set(int index, Object o);
+
+       public int indexOf(Object o);
+       public int lastIndexOf(Object o);
+
+       public Object[] toArray();
+       public Object[] toArray(Object[] o);
+}

Propchange: commons/sandbox/me/trunk/src/org/apache/commons/me/util/List.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
commons/sandbox/me/trunk/test-src/org/apache/commons/me/util/TestArrayList.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/me/trunk/test-src/org/apache/commons/me/util/TestArrayList.java?rev=649952&view=auto
==============================================================================
--- 
commons/sandbox/me/trunk/test-src/org/apache/commons/me/util/TestArrayList.java 
(added)
+++ 
commons/sandbox/me/trunk/test-src/org/apache/commons/me/util/TestArrayList.java 
Sun Apr 20 11:40:27 2008
@@ -0,0 +1,68 @@
+/* ====================================================================
+   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 org.apache.commons.me.util;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests for the ArrayList implementation
+ */
+public class TestArrayList extends TestCase {
+       public void testNormalUse() {
+               ArrayList l = new ArrayList();
+               assertEquals(0, l.size());
+
+               String s1 = "test";
+               String s2 = "test2";
+               Integer i1 = new Integer(11);
+
+               assertFalse(l.contains(s1));
+               assertFalse(l.contains(s2));
+               assertFalse(l.contains(i1));
+
+               l.add(s1);
+               assertEquals(1, l.size());
+               assertTrue(l.contains(s1));
+               assertFalse(l.contains(s2));
+               assertFalse(l.contains(i1));
+
+               l.add(s2);
+               assertEquals(2, l.size());
+               assertTrue(l.contains(s1));
+               assertTrue(l.contains(s2));
+               assertFalse(l.contains(i1));
+
+               l.set(1, i1);
+               assertEquals(2, l.size());
+               assertTrue(l.contains(s1));
+               assertFalse(l.contains(s2));
+               assertTrue(l.contains(i1));
+
+               assertEquals(s1, l.get(0));
+               assertEquals(i1, l.get(1));
+
+               assertEquals(2, l.toArray().length);
+               assertEquals(2, l.toArray(new Object[2]).length);
+               assertEquals(3, l.toArray(new Object[3]).length);
+               try {
+                       l.toArray(new Object[0]);
+                       fail();
+               } catch(IllegalArgumentException e) {
+                       // good, expected
+               }
+       }
+}

Propchange: 
commons/sandbox/me/trunk/test-src/org/apache/commons/me/util/TestArrayList.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to