Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.01 KB | None | 0 0
  1. package dataFrame;
  2.  
  3. import values.StringValue;
  4. import values.Value;
  5.  
  6. import java.lang.reflect.Array;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9.  
  10. public class DataFrame {
  11.     private List<ArrayList<Value>> mColumns;
  12.     protected String[] mColumnNames;
  13.     protected String[] mColumnTypes;
  14.  
  15.     public DataFrame(String[] columnNames, String[] columnTypes) {
  16.         mColumnNames = columnNames;
  17.         mColumnTypes = columnTypes;
  18.     }
  19.  
  20.     /*
  21.     – zwracającą ilość wierszy w całej DF (uwaga – DF nie może mieć jednej z kolumn dłuższej niż pozostałe
  22.      */
  23.     public int size()
  24.     {
  25.         ArrayList<Value> firstColumn = mColumns.get(0);
  26.  
  27.         if (firstColumn == null) {
  28.             return 0;
  29.         }
  30.  
  31.         return firstColumn.size();
  32.     }
  33.  
  34.     /*
  35.      – zwracającą kolumnę o podanej nazwie
  36.      */
  37.     public ArrayList<Value> get(String columnName)
  38.     {
  39.         int columnIndex = -1;
  40.  
  41.         for (int i = 0; i < mColumnNames.length; i++) {
  42.             if (mColumnNames[i].equals(columnName)) {
  43.                 columnIndex = i;
  44.                 break;
  45.             }
  46.         }
  47.  
  48.         if (columnIndex == -1) {
  49.             return null;
  50.         }
  51.  
  52.         return mColumns.get(columnIndex);
  53.     }
  54.  
  55.     /*
  56.     – zwracającą nową DataFrame z kolumnami podanymi jako parametry. W zależności od wartości parametru copy albo tworzona jest głęboka kopia, albo płytka.
  57.      */
  58.     public DataFrame get(String[] colNames, boolean copy)
  59.     {
  60.         List<ArrayList<Value>> columns = new ArrayList<ArrayList<Value>>();
  61.         String[] colTypes = new String[colNames.length];
  62.  
  63.         for (int i = 0; i < colNames.length; i++) {
  64.             String currentColName = colNames[i];
  65.             ArrayList<Value> currentColumn = get(currentColName);
  66.  
  67.             for (int j = 0; j < mColumnNames.length; j++)
  68.             {
  69.                 if (mColumnNames[j].equals(currentColName))
  70.                 {
  71.                     colTypes[i] = mColumnTypes[j];
  72.                     break;
  73.                 }
  74.             }
  75.  
  76.             if(copy)
  77.             {
  78.                 ArrayList<Value> copiedColumn = new ArrayList<>();
  79.  
  80.                 for (Value x: currentColumn){
  81.                     copiedColumn.add(new StringValue(new StringBuilder(x.toString()).toString()));
  82.                 }
  83.  
  84.                 columns.add(copiedColumn);
  85.             }
  86.             else
  87.             {
  88.                 columns.add(currentColumn);
  89.             }
  90.         }
  91.  
  92.         DataFrame dataFrame = new DataFrame(colNames, colTypes);
  93.  
  94.         dataFrame.set(columns);
  95.  
  96.         return dataFrame;
  97.     }
  98.  
  99.     /*
  100.      – zwracającą wiersz o podanym indeksie (jako nową DataFrame)
  101.      */
  102.     public DataFrame iloc(int i)
  103.     {
  104.         DataFramerowDataFrame = new DataFrame(mColumnNames, mColumnTypes);
  105.  
  106.         List<ArrayList<Value>> columnsForRowDataFrame = getRowsFromTo(i, i);
  107.  
  108.         rowDataFrame.set(columnsForRowDataFrame);
  109.  
  110.         return rowDataFrame;
  111.     }
  112.  
  113.     /*
  114.      – zwracającą nową DataFrame z wierszami z podanego zakresu
  115.      */
  116.     public DataFrame iloc(int from, int to)
  117.     {
  118.         DataFramerowDataFrame = new DataFrame(mColumnNames, mColumnTypes);
  119.  
  120.         List<ArrayList<Value>> columnsForRowDataFrame = getRowsFromTo(from, to);
  121.  
  122.         rowDataFrame.set(columnsForRowDataFrame);
  123.  
  124.         return rowDataFrame;
  125.     }
  126.  
  127.     public void set(List<ArrayList<Value>> columns)
  128.     {
  129.         mColumns = columns;
  130.     }
  131.  
  132.     private List<ArrayList<Value>> getRowsFromTo(int from, int to)
  133.     {
  134.         List<ArrayList<Value>> columns = new ArrayList<ArrayList<Value>>();
  135.  
  136.         for (ArrayList<Value> column: mColumns)
  137.         {
  138.             ArrayList<Value> newColumn = new ArrayList<>();
  139.  
  140.             for (int i = from; i <= to; i++)
  141.             {
  142.                 Value valueAtIndex = column.get(i);
  143.                 newColumn.add(valueAtIndex);
  144.             }
  145.  
  146.             columns.add(newColumn);
  147.         }
  148.  
  149.         return columns;
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement