/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.karry.dop.core;

import java.util.ArrayList;

/**
 *
 * @author karry
 */
public class Bus {
    protected ArrayList<BusListener> listeners = new ArrayList<BusListener>();
    private int value = 0; // state -1 is undefined...
    private String name;
    
    public Bus(String name){
        this.name = name; 
    }

    Bus(String name, int initValue) {
        this(name);
        setValue(initValue);
    }

    public String getName() {
        return name;
    }
    
    public int getValue() {
        return value;
    }

    public void removeBusListener(BusListener listener) {
        listeners.remove(listener);
    }
    
    public void setValue(int value){
        if (value == this.value)
            return;
        
        this.value = value;
        for (int i=0; i<listeners.size(); i++){
            listeners.get(i).busChangePerformed(this,value);
        }
    }
    
    public void addBusListener(BusListener listener){
        listeners.add(listener);
    }
    
    public ArrayList<BusListener> getBusListeners(){
        return this.listeners;
    }

}
