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

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

The newest version!

package com.datastax.data.dataset;

import com.datastax.data.dataset.provider.LoadTask;
import com.datastax.data.dataset.provider.SaveTask;
import com.datastax.data.dataset.provider.Task;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;


public abstract class DataProvider {
    
    private static final Executor EX = Executors.newCachedThreadPool();

    
    private PropertyChangeSupport pcs = new PropertyChangeSupport(this);
    
    private DataCommand command;
    
    private DataConnection connection;
    
    public void load(DataTable[] tables) {
        Task task = createLoadTask(tables);
        runTask(task);
    }
    
    public void loadAndWait(DataTable[] tables) {
        LoadTask task = createLoadTask(tables);
        task.setLoadOnEDT(false);
        runTaskAndWait(task);
    }
    
    public void load(DataTable t) {
        load(new DataTable[]{t});
    }
    
    public void loadAndWait(DataTable t) {
        loadAndWait(new DataTable[]{t});
    }
    
    public void save(DataTable t) {
        save(new DataTable[]{t});
    }
    
    public void saveAndWait(DataTable t) {
        saveAndWait(new DataTable[]{t});
    }
    
    public void save(DataTable[] tables) {
        Task task = createSaveTask(tables);
        runTask(task);
    }
    
    public void saveAndWait(DataTable[] tables) {
        Task task = createSaveTask(tables);
        runTaskAndWait(task);
    }
    
    
    protected abstract SaveTask createSaveTask(DataTable[] tables);
    
    
    protected abstract LoadTask createLoadTask(DataTable[] tables);
    
    
    protected void runTask(Task runner) {

        EX.execute(runner);
    }
    
    protected void runTaskAndWait(Task runner) {
        runner.run();
    }
    
    
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        pcs.addPropertyChangeListener(listener);
    }

   
    public void addPropertyChangeListener(String propertyName,
            PropertyChangeListener listener) {
        pcs.addPropertyChangeListener(propertyName, listener);
    }
    
    
    public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
    	pcs.removePropertyChangeListener(propertyName, listener);
    }

    
    public void setCommand(DataCommand cmd) {
        if (this.command != cmd) {
            DataCommand oldCommand = this.command;
            this.command = cmd;
            pcs.firePropertyChange("command", oldCommand, cmd);
        }
    }
    
    
    public DataCommand getCommand() {
        return command;
    }
    
    public void setConnection(DataConnection conn) {
        if (this.connection != conn) {
            DataConnection oldConn = this.connection;
            this.connection = conn;
            pcs.firePropertyChange("connection", oldConn, conn);
        }
    }
    
    public DataConnection getConnection() {
        return connection;
    }    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy