1 /* 2 * This file is a part of CAST project. 3 * (c) Copyright 2007, AGH University of Science & Technology 4 * https://caribou.iisg.agh.edu.pl/trac/cast 5 * 6 * Licensed under the Eclipse Public License, Version 1.0 (the "License"). 7 * You may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * http://www.eclipse.org/legal/epl-v10.html 10 */ 11 /* 12 * File: DataRow.java 13 * Created: 2007-05-07 14 * Author: entrop, kpietak 15 * $Id: DataRow.java 2308 2009-01-12 22:33:45Z kpietak $ 16 */ 17 18 package pl.edu.agh.cast.rawdata; 19 20 import java.util.LinkedList; 21 22 import pl.edu.agh.cast.rawdata.logging.AbstractRawDataMonitorable; 23 import pl.edu.agh.cast.rawdata.logging.IRawDataObserver; 24 25 /** 26 * Class which represents one row of data read from source data file. It contains list of strings object each 27 * representing one cell. 28 * 29 * @author AGH CAST Team 30 * 31 */ 32 public class DataRow extends AbstractRawDataMonitorable { 33 private static final long serialVersionUID = 1L; 34 35 /** 36 * Indicates that row has unspecified number in tabular data. 37 */ 38 public static final int UNSPECIFIED_ROW_NUMBER = -1; 39 40 private int rowNumber = UNSPECIFIED_ROW_NUMBER; 41 42 private LinkedList<String> cells; 43 44 /** 45 * Default constructor. 46 */ 47 public DataRow() { 48 cells = new LinkedList<String>(); 49 } 50 51 // only package access 52 void setRowNumber(int rowNumber) { 53 this.rowNumber = rowNumber; 54 } 55 56 public int getRowNumber() { 57 return rowNumber; 58 } 59 60 /** 61 * Replaces cells specified by index, by element. 62 * 63 * @param index 64 * index of cell to replace 65 * @param element 66 * new element 67 * @return new element or <code>null</code> if specified index is out of bounds 68 */ 69 public String replace(int index, String element) { 70 if (index < 0 || index > cells.size()) { 71 return null; 72 } 73 String oldValue = cells.remove(index); 74 cells.add(index, element); 75 76 notifyValueChanged(oldValue, element); 77 78 return element; 79 } 80 81 /* 82 * -------------------------------------------------------------------------------------- 83 * --------------------------DELEGATED METHODS---------------------------------- 84 * -------------------------------------------------------------------------------------- 85 */ 86 87 /** 88 * Appends the specified element to the end of this list. 89 * 90 * @param element 91 * element to be inserted 92 * @return <code>true</code> (as per the general contract of Collection.add). 93 */ 94 public boolean add(String element) { 95 return cells.add(element); 96 } 97 98 /** 99 * Inserts the specified element at the specified position in this list. Shifts the element currently at that 100 * position (if any) and any subsequent elements to the right (adds one to their indices). 101 * 102 * @param index 103 * index at which the specified element is to be inserted 104 * @param element 105 * element to be inserted 106 * @throws IndexOutOfBoundsException 107 * if the specified index is out of range (<code>index < 0 || index > size()</code>). 108 */ 109 public void add(int index, String element) throws IndexOutOfBoundsException { 110 cells.add(index, element); 111 } 112 113 /** 114 * Returns the number of elements in this list. 115 * 116 * @return the number of elements in this list. 117 */ 118 public int size() { 119 return cells.size(); 120 } 121 122 /** 123 * Returns the element at the specified position in this list. 124 * 125 * @param index 126 * index of element to return 127 * @return the element at the specified position in this list. 128 * 129 * @throws IndexOutOfBoundsException 130 * if the specified index is out of range (<code>index < 0 || index > size()</code>). 131 */ 132 public String get(int index) throws IndexOutOfBoundsException { 133 return cells.get(index); 134 } 135 136 /** 137 * Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts 138 * one from their indices). Returns the element that was removed from the list. 139 * 140 * @param index 141 * the index of the element to removed 142 * @return the element previously at the specified position. 143 * @throws IndexOutOfBoundsException 144 * if the specified index is out of range (<code>index < 0 || index > size()</code>). 145 */ 146 public Object remove(int index) throws IndexOutOfBoundsException { 147 return cells.remove(index); 148 } 149 150 /* 151 * -------------------------------------------------------------------------------------- 152 * --------------------------OVERRIDED METHODS---------------------------------- 153 * -------------------------------------------------------------------------------------- 154 */ 155 156 /** 157 * 158 * {@inheritDoc} 159 * 160 * @see java.lang.Object#toString() 161 */ 162 @Override 163 public String toString() { 164 StringBuffer res = new StringBuffer(); 165 res.append("[Row number: ").append(rowNumber).append(", Data: ") //$NON-NLS-1$//$NON-NLS-2$ 166 .append(cells.toString()).append("]"); //$NON-NLS-1$ 167 return res.toString(); 168 } 169 170 /* 171 * -------------------------------------------------------------------------------------- 172 * --------------------------HELPER METHODS--------------------------------------- 173 * -------------------------------------------------------------------------------------- 174 */ 175 176 private void notifyValueChanged(String oldValue, String newValue) { 177 for (IRawDataObserver observer : getObservers()) { 178 observer.notifyValueChanged(this, oldValue, newValue); 179 } 180 } 181 }