All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.datastax.data.dataset.DataRow Maven / Gradle / Ivy

The newest version!


package com.datastax.data.dataset;

import com.datastax.data.dataset.event.RowChangeEvent;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class DataRow {
    
    public enum DataRowStatus {
        
        INSERTED,
        
        DELETED,
        
        UPDATED,
        
        UNCHANGED
    };
    
    
    
    
    private PropertyChangeSupport pcs = new PropertyChangeSupport(this);
    
    
      private DataTable table;
    
    
    private DataRowStatus status = DataRowStatus.INSERTED;
    
    
    private Map cells = new HashMap();
    
    
    protected DataRow(DataTable table) {
        assert table != null;
        this.table = table;
        
        for (DataColumn col : this.table.getColumns()) {
            addCell(col);
        }
    }
    
    
    public void setReferenceValue(String colName, Object value) {
        setReferenceValue(table.getColumn(colName), value);
    }

    public int getCellsCount(){
        return cells.size();
    }
    
    
    public void setReferenceValue(DataColumn col, Object value) {
        assert col != null;
        
        
        getCell(col).setReferenceValue(table, col, value);
        
        
        DataRowStatus newStatus = deriveRowStatus();
        if ( newStatus != status ) {
            setStatus(newStatus);
        }
    }
    
    
    public void setValue(String colName, Object value) {
        DataColumn col = table.getColumn(colName);
        setValue(col, value);
    }
    
    
    public void setValue(DataColumn col, Object value) {
        assert col != null;
        
        Object oldValue = getValue(col);
        DataCell cell = getCell(col);
        getCell(col).setValue(table, col, value);
        
        DataRowStatus oldStatus = status;
        DataRowStatus newStatus = deriveRowStatus();
        if ( newStatus != oldStatus ) {
            setStatus(newStatus);
        }
        
        
        if (!cell.isSame(getTable(), col, oldValue, value))
            fireDataRowChanged(RowChangeEvent.newCellChangedEvent(this, col, oldValue, oldStatus));
    }
    
    
    public void resetAllToReferenceValue() {
        List cols = table.getColumns();
        for ( DataColumn col : cols ) {
            resetToReferenceValue(col);
        }
    }  
    
    
    public void resetToReferenceValue(String colName) {
        resetToReferenceValue(table.getColumn(colName));
    }
    
    
    public void resetToReferenceValue(DataColumn col) {
        setValue(col, getCell(col).referenceValue);
        
    }
    
    
    public Object getReferenceValue(String colName) {
        return getReferenceValue(table.getColumn(colName));
    }
    
    
    public Object getValue(String colName) {
        return getValue(table.getColumn(colName));
    }

    
    public Object getReferenceValue(DataColumn col) {
        assert col != null;
        String exp = col.getExpression();
        if (exp == null || exp.equals("")) {
            DataCell cell = getCell(col);
            return cell.referenceValue;
        } else {
            return col.getValueForRow(this);
        }
    }
    
    
    public Object getValue(DataColumn col) {
        assert col != null;
        String exp = col.getExpression();
        if (exp == null || exp.equals("")) {
            DataCell cell = getCell(col);
            return cell.value;
        } else {
            return col.getValueForRow(this);
        }
    }
    
    
    protected DataCell getCell(DataColumn col) {
        DataCell cell = cells.get(col);
        if (cell == null && col.getTable() == table) {
            cell = addCell(col);
        }
        return cell;
    }
    
    
    public DataTable getTable() {
        return table;
    }
    
    
    void removeFromTable() {
        table = null;
    }
    
    
    public DataRowStatus getStatus() {
        return status;
    }
    
    
    public boolean isModified(String colName) {
        return isModified(table.getColumn(colName));
    }
    
    
    public boolean isModified(DataColumn col) {
        return cells.get(col).changed;
    }
    
    
    public Object getOriginalValue(String colName) {
        return getReferenceValue(colName);
    }
    
    
    public Object getOriginalValue(DataColumn col) {
        return getReferenceValue(col);
    }
    
    
    public void setStatus(DataRowStatus status) {
        if (this.status != status) {
            DataRowStatus priorStatus = this.status;
            this.status = status;
            
            pcs.firePropertyChange("status", priorStatus, status);
            
            if (this.status == DataRowStatus.UNCHANGED) {
                
                List cols = table.getColumns();
                for ( DataColumn col : cols ) {
                    
                    
                    DataCell cell = getCell(col);
                    if ( cell.changed ) cell.overwriteReference();
                }
            }
        }
    }
    
    
    public int getIndex() {
        return table.indexOfRow(this);
    }
    
    
    public void fireDataRowChanged(RowChangeEvent evt) {
        table.fireRowChanged(evt);
    }
    
    
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        pcs.addPropertyChangeListener(listener);
    }
    
    
    public void addPropertyChangeListener(String property, PropertyChangeListener listener) {
        pcs.addPropertyChangeListener(property,  listener);
    }
    
    
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        pcs.removePropertyChangeListener(listener);
    }
    
    
    public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
        pcs.removePropertyChangeListener(propertyName,  listener);
    }
    
    
    public String toString() {
        StringBuilder buffer = new StringBuilder();
        buffer.append("Row #");
        buffer.append(table.indexOfRow(this));
        buffer.append(" [ ");
        int i=0;
        for (DataCell c : cells.values()) {
            buffer.append(c.value);
            if (i < cells.size() -1) {
                buffer.append(", ");
            }
            i++;
        }
        buffer.append(" ]");
        return buffer.toString();
    }
    
    
    protected DataCell addCell(DataColumn col) {
        DataCell cell = cells.get(col);
        if ( cell == null ) {
            cell = new DataCell(col);
            cells.put(col, cell);
        }
        return cell;
    }
    
    
    protected DataRowStatus deriveRowStatus() {
        DataRowStatus derived;
        switch (status) {
            case INSERTED: 
            case DELETED:
                derived = status;
                break;
            case UPDATED: 
            case UNCHANGED:
                boolean any = false;
                for ( DataCell cell : cells.values()) {
                    if ( cell.changed ) {
                        any = true;
                        break;
                    }
                }
                derived = ( any ? DataRowStatus.UPDATED : DataRowStatus.UNCHANGED );
                break;
            default:
                throw new RuntimeException("deriveRowStatus() has no case for row status of " + status);
        }
        return derived;
    }
    
    
    private static final class DataCell {
        
        Object referenceValue;
        
        Object value;
        
        boolean changed;
        
        boolean valueSet;
        
        
        DataCell(DataColumn col){
            this.value = this.referenceValue = col.getDefaultValue();
        }
        
        
        public void setReferenceValue(DataTable table, DataColumn col, Object newRef) {
            referenceValue = newRef;
            changed = isSame(table, col, referenceValue, newRef);
        }
        
        
        public void setValue(DataTable table, DataColumn col, Object newValue) {
            
            if ( !valueSet ) {
                value = referenceValue = newValue;
                changed = true;
                valueSet = true;
                return;
            }
            
            
            
            if ( isSame(table, col, referenceValue, newValue )) {
                value = referenceValue;
                changed = false;
                return;
            } else {
                
                if ( ! isSame( table, col, value, newValue )) {
                    value = newValue;
                    changed = true;
                }
            }
        }
        
        
        public void overwriteReference() {
            referenceValue = value;
            changed = false;
        }

        
        private boolean isSame(DataTable table, DataColumn col, Object baseValue, Object newValue) {
            if ( table.isIdentityComparisonEnabled()) {
                return newValue == baseValue;
            } else {
                Comparator comp = null;
                if ( table.hasColumnComparator(col)) {
                    comp = table.getColumnComparator(col);
                } else {
                    
                    
                    comp = table.getClassComparator(col.getType());
                }
                return comp.compare(baseValue, newValue) == 0;
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy