001: /* DataTableDecorator.java
002: *
003: * DDSteps - Data Driven JUnit Test Steps
004: * Copyright (C) 2005 Jayway AB
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License version 2.1 as published by the Free Software Foundation.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, visit
017: * http://www.opensource.org/licenses/lgpl-license.php
018: */
019: package org.ddsteps.dataset.decorator;
020:
021: import java.util.Iterator;
022:
023: import org.apache.commons.collections.Transformer;
024: import org.apache.commons.collections.functors.NOPTransformer;
025: import org.apache.commons.collections.iterators.TransformIterator;
026: import org.apache.commons.lang.Validate;
027: import org.ddsteps.dataset.DataRow;
028: import org.ddsteps.dataset.DataTable;
029:
030: /**
031: * Decorates a DataTable with a transformer for DataRows.
032: *
033: * @author Adam
034: * @version $Id: DataTableDecorator.java,v 1.1 2005/08/25 08:32:36 adamskogman
035: * Exp $
036: */
037: public class DataTableDecorator implements DataTable {
038:
039: final DataTable dataTable;
040:
041: Transformer dataValueTransformer = NOPTransformer.INSTANCE;
042:
043: /**
044: * Dependency injection constructor
045: *
046: * @param dataTable
047: * The decorated dataTable
048: */
049: public DataTableDecorator(DataTable dataTable) {
050: super ();
051: Validate.notNull(dataTable,
052: "Argument dataTable must not be null");
053: this .dataTable = dataTable;
054: }
055:
056: /**
057: * @param dataValueTransformer
058: * The dataValueTransformer to set.
059: */
060: public void setDataValueTransformer(Transformer dataValueTransformer) {
061: this .dataValueTransformer = dataValueTransformer;
062: }
063:
064: /**
065: * @see DataTable#getName()
066: */
067: public String getName() {
068: return dataTable.getName();
069: }
070:
071: /**
072: * @see DataTable#getRowCount()
073: */
074: public int getRowCount() {
075: return dataTable.getRowCount();
076: }
077:
078: /**
079: * Iterate over transformed data rows.
080: *
081: * @see org.ddsteps.dataset.DataTable#rowIterator()
082: */
083: public Iterator rowIterator() {
084: return new TransformIterator(dataTable.rowIterator(),
085: new DataRowDecoratorTransformer(dataValueTransformer));
086: }
087:
088: /**
089: * @see org.ddsteps.dataset.DataTable#findRowById(java.lang.String)
090: */
091: public DataRow findRowById(String rowId) {
092: // TODO Improve performance
093: return (DataRow) new DataRowDecoratorTransformer(
094: dataValueTransformer).transform(dataTable
095: .findRowById(rowId));
096: }
097:
098: /**
099: * Testing accessor.
100: *
101: * @return The data value transformer
102: */
103: public Transformer getDataValueTransformer() {
104: return dataValueTransformer;
105: }
106:
107: /**
108: * Testing accessor.
109: *
110: * @return The data table that this decorator decorates.
111: */
112: public DataTable getDataTable() {
113: return dataTable;
114: }
115: }
|