bunti/src/main/java/de/ctdo/bunti/dmx/DMXChannels.java

113 lines
2.6 KiB
Java

package de.ctdo.bunti.dmx;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* DMXChannel container
* @author jCoder
*
*/
public class DMXChannels {
protected Map<Integer,DMXChannel> channelByNumber = new HashMap<Integer, DMXChannel>();
protected Map<String,DMXChannel> channelByName = new HashMap<String, DMXChannel>();
/**
* Returns the number of channels
* @return number of channels
*/
public int getCount() {
return this.channelByNumber.size();
}
/**
* Returns a channel by name
* @param name channel name
* @return channel or null if not found
*/
public DMXChannel getChannelByName(String name) {
if (name == null) {
return null;
}
return this.channelByName.get(name);
}
/**
* Returns a channel by offset
* @param number number
* @return channel or null if not found
*/
public DMXChannel getChannelByNumber(int number) {
return this.channelByNumber.get(number);
}
/**
* Adds a channel
* @param channel channel to add
* @return true on success, false on error
*/
public boolean addChannel(DMXChannel channel) {
// object cannot be null
if (channel == null) {
return false;
}
// description cannot be null
if (channel.getName() == null) {
return false;
}
// entry must not exist by offset
if (this.channelByNumber.containsKey(channel.getOffset())) {
return false;
}
// entry must not exist by name
if (this.channelByName.containsKey(channel.getName())) {
return false;
}
this.channelByNumber.put(channel.getOffset(), channel);
this.channelByName.put(channel.getName(), channel);
return true;
}
/**
* Removes a channel by offset
* @param offset offset
* @return removed channel or null if it does not exist
*/
public DMXChannel removeChannel(int offset) {
DMXChannel tmpChannel = this.channelByNumber.remove(offset);
if (tmpChannel != null) {
this.channelByName.remove(tmpChannel.getName());
}
return tmpChannel;
}
/**
* Removes a channel by name
* @param name channel name
* @return removed channel or null if it does not exist
*/
public DMXChannel removeChannel(String name) {
if (name == null) {
return null;
}
DMXChannel tmpChannel = this.channelByName.remove(name);
if (tmpChannel != null) {
this.channelByNumber.remove(tmpChannel.getOffset());
}
return tmpChannel;
}
/**
* Returns an (unmodifiable) collection of all channels
* @return unmodifiable collection of all channels
*/
public Collection<DMXChannel> getAllChannels() {
return Collections.unmodifiableCollection(this.channelByNumber.values());
}
}