Home | History | Annotate | Download | only in shadows
      1 package com.xtremelabs.robolectric.shadows;
      2 
      3 import com.xtremelabs.robolectric.internal.Implementation;
      4 import com.xtremelabs.robolectric.internal.Implements;
      5 
      6 import android.database.CursorIndexOutOfBoundsException;
      7 import android.database.MatrixCursor;
      8 
      9 import java.util.ArrayList;
     10 import java.util.List;
     11 
     12 @Implements(MatrixCursor.class)
     13 public class ShadowMatrixCursor extends ShadowAbstractCursor {
     14     private List<Object[]> data = new ArrayList<Object[]>();
     15 
     16     public void __constructor__(String[] columns) {
     17         __constructor__(columns, 0);
     18     }
     19 
     20     public void __constructor__(String[] columns, int initialCapacity) {
     21         columnNameArray = columns;
     22     }
     23 
     24     @Implementation
     25     public void addRow(Object[] data) {
     26         rowCount++;
     27         this.data.add(data);
     28     }
     29 
     30     @Implementation
     31     public String getString(int column) {
     32         Object columnValue = get(column);
     33         return columnValue == null ? null : columnValue.toString();
     34     }
     35 
     36     @Implementation
     37     public long getLong(int column) {
     38         Number numberValue = (Number) get(column);
     39         return numberValue == null ? 0 : numberValue.longValue();
     40     }
     41 
     42     @Implementation
     43     public short getShort(int column) {
     44         Number numberValue = (Number) get(column);
     45         return numberValue == null ? 0 : numberValue.shortValue();
     46     }
     47 
     48     @Implementation
     49     public int getInt(int column) {
     50         Number numberValue = (Number) get(column);
     51         return numberValue == null ? 0 : numberValue.intValue();
     52     }
     53 
     54     @Implementation
     55     public float getFloat(int column) {
     56         Number numberValue = (Number) get(column);
     57         return numberValue == null ? 0.0f : numberValue.floatValue();
     58     }
     59 
     60     @Implementation
     61     public double getDouble(int column) {
     62         Number numberValue = (Number) get(column);
     63         return numberValue == null ? 0.0 : numberValue.doubleValue();
     64     }
     65 
     66     @Implementation
     67     public byte[] getBlob(int column) {
     68         return (byte[]) get(column);
     69     }
     70 
     71     @Implementation
     72     public boolean isNull(int column) {
     73         return get(column) == null;
     74     }
     75 
     76     private Object get(int column) {
     77         if (column < 0 || column >= columnNameArray.length) {
     78             throw new CursorIndexOutOfBoundsException(null);
     79         }
     80         if (currentRowNumber < 0) {
     81             throw new CursorIndexOutOfBoundsException("Before first row.");
     82         }
     83         if (currentRowNumber >= rowCount) {
     84             throw new CursorIndexOutOfBoundsException("After last row.");
     85         }
     86         return data.get(currentRowNumber)[column];
     87     }
     88 }
     89