001package jmri.jmrit.operations.rollingstock;
002
003import java.util.ArrayList;
004import java.util.List;
005import jmri.beans.PropertyChangeSupport;
006import org.slf4j.Logger;
007import org.slf4j.LoggerFactory;
008
009/**
010 * A group of rolling stock that is managed as one unit.
011 *
012 * @author Daniel Boudreau Copyright (C) 2010, 2013
013 * @param <T> the type of RollingStock in this group
014 */
015public abstract class RollingStockGroup<T extends RollingStock> extends PropertyChangeSupport {
016
017    protected String _name = "";
018    protected T _lead = null;
019    protected List<T> _group = new ArrayList<>();
020
021    public RollingStockGroup(String name) {
022        _name = name;
023    }
024
025    public String getName() {
026        return _name;
027    }
028
029    // for combo boxes
030    @Override
031    public String toString() {
032        return _name;
033    }
034
035    public void add(T rs) {
036        if (_group.contains(rs)) {
037            log.debug("rs ({}) already part of group ({})", rs.toString(), getName());
038            return;
039        }
040        if (_group.size() <= 0) {
041            _lead = rs;
042        }
043        int oldSize = _group.size();
044        _group.add(rs);
045        firePropertyChange("grouplistLength", oldSize, _group.size()); // NOI18N
046    }
047
048    public void delete(T rs) {
049        if (!_group.contains(rs)) {
050            log.debug("rs ({}) not part of group ({})", rs.getId(), getName());
051            return;
052        }
053        int oldSize = _group.size();
054        _group.remove(rs);
055        // need a new lead rs?
056        removeLead(rs);
057        firePropertyChange("grouplistLength", oldSize, _group.size()); // NOI18N
058    }
059
060    public List<T> getGroup() {
061        return _group;
062    }
063
064    public int getTotalLength() {
065        int length = 0;
066        for (T rs : _group) {
067            length = length + rs.getTotalLength();
068        }
069        return length;
070    }
071
072    /**
073     * Get a group's adjusted weight
074     *
075     * @return group's weight
076     */
077    public int getAdjustedWeightTons() {
078        int weightTons = 0;
079        for (T rs : _group) {
080            weightTons = weightTons + rs.getAdjustedWeightTons();
081        }
082        return weightTons;
083    }
084
085    public boolean isLead(T rs) {
086        if (rs == _lead) {
087            return true;
088        }
089        return false;
090    }
091
092    public T getLead() {
093        return _lead;
094    }
095
096    /**
097     * Gets the number of rolling stock in this group
098     *
099     * @return number of elements in this group
100     */
101    public int getSize() {
102        return _group.size();
103    }
104
105    /**
106     * Sets the lead for this group. RollingStock must be part of the group. The
107     * rolling stock that make up this group will have the attributes of the
108     * lead. However, the length attribute is the sum of all unit lengths plus
109     * the coupler lengths.
110     *
111     * @param rs lead for this group.
112     */
113    public void setLead(T rs) {
114        if (_group.contains(rs)) {
115            _lead = rs;
116        }
117    }
118
119    public void removeLead(T rs) {
120        if (isLead(rs) && _group.size() > 0) {
121            setLead(_group.get(0));
122        }
123    }
124
125    public void dispose() {
126
127    }
128
129    private final static Logger log = LoggerFactory.getLogger(RollingStockGroup.class);
130}