com.datastax.data.dataset.DataRelation Maven / Gradle / Ivy
package com.datastax.data.dataset;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;
public class DataRelation {
private static final Logger LOG = Logger.getLogger(DataRelation.class.getName());
protected static final String DEFAULT_NAME_PREFIX = "DataRelation";
private static final NameGenerator NAMEGEN = new NameGenerator(DEFAULT_NAME_PREFIX);
private DataSet dataSet;
private String name;
private DataColumn parentColumn;
private DataColumn childColumn;
private PropertyChangeSupport pcs = new PropertyChangeSupport(this);
protected DataRelation(DataSet ds) {
assert ds != null;
this.dataSet = ds;
name = NAMEGEN.generateName(this);
}
protected DataRelation(DataSet ds, String name) {
this(ds);
if (name != null) {
setName(name);
}
}
public DataSet getDataSet() {
return dataSet;
}
void removeFromDataSet() {
dataSet = null;
}
public void setName(String name) {
if (this.name != name) {
assert DataSetUtils.isValidName(name);
assert !dataSet.hasElement(name);
String oldName = this.name;
this.name = name;
pcs.firePropertyChange("name", oldName, name);
}
}
public String getName() {
return name;
}
public DataColumn getParentColumn() {
return parentColumn;
}
public void setParentColumn(DataColumn parentColumn) {
if (this.parentColumn != parentColumn) {
assert parentColumn == null || parentColumn != this.childColumn;
DataColumn oldValue = this.parentColumn;
this.parentColumn = parentColumn;
pcs.firePropertyChange("parentColumn", oldValue, parentColumn);
}
}
public DataColumn getChildColumn() {
return childColumn;
}
public void setChildColumn(DataColumn childColumn) {
if (this.childColumn != childColumn) {
assert childColumn == null || childColumn != this.parentColumn;
DataColumn oldValue = this.childColumn;
this.childColumn = childColumn;
pcs.firePropertyChange("childColumn", oldValue, childColumn);
}
}
public List getRows(DataRow parentRow) {
if (parentColumn == null || childColumn == null || parentRow == null) {
return Collections.unmodifiableList(Collections.EMPTY_LIST);
}
assert parentRow.getTable().equals(parentColumn.getTable());
DataTable childTable = childColumn.getTable();
Object parentKey = parentColumn.getTable().getValue(parentRow, parentColumn);
List rows = new ArrayList();
for (DataRow childRow : childTable.getRows()) {
Object childKey = childTable.getValue(childRow, childColumn);
if (parentKey != null && childKey != null && parentKey.equals(childKey)) {
rows.add(childRow);
}
}
return Collections.unmodifiableList(rows);
}
public List getRows(int parentRowIndex) {
if (parentColumn == null || childColumn == null || parentRowIndex < 0) {
return Collections.unmodifiableList(Collections.EMPTY_LIST);
}
DataTable parentTable = parentColumn.getTable();
return getRows(parentTable.getRow(parentRowIndex));
}
public List getRows(DataRow[] parentRows) {
List rows = new ArrayList();
for (DataRow parentRow : parentRows) {
rows.addAll(getRows(parentRow));
}
return Collections.unmodifiableList(rows);
}
public List getRows(int[] parentRowIndices) {
if (parentColumn == null || childColumn == null || parentRowIndices == null) {
return Collections.unmodifiableList(Collections.EMPTY_LIST);
}
DataTable parentTable = parentColumn.getTable();
DataRow[] parentRows = new DataRow[parentRowIndices.length];
for (int i=0; i getRows(List parentRowIndices) {
if (parentColumn == null || childColumn == null || parentRowIndices == null) {
return Collections.unmodifiableList(Collections.EMPTY_LIST);
}
DataTable parentTable = parentColumn.getTable();
DataRow[] parentRows = new DataRow[parentRowIndices.size()];
for (int i=0; i