2012-03-03 01:29:06 +00:00
|
|
|
package de.ctdo.bunti.dmx;
|
|
|
|
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
/**
|
2012-03-03 17:29:31 +00:00
|
|
|
* DMXChannel container
|
|
|
|
* @author jCoder
|
|
|
|
*
|
|
|
|
*/
|
2012-03-03 01:29:06 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-03-03 17:29:31 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
2012-03-03 01:29:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2012-03-03 17:29:31 +00:00
|
|
|
if (channel.getName() == null) {
|
2012-03-03 01:29:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// entry must not exist by offset
|
2012-03-04 00:21:00 +00:00
|
|
|
if (this.channelByNumber.containsKey(channel.getOffset())) {
|
2012-03-03 01:29:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// entry must not exist by name
|
2012-03-04 00:21:00 +00:00
|
|
|
if (this.channelByName.containsKey(channel.getName())) {
|
2012-03-03 01:29:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-03-03 17:29:31 +00:00
|
|
|
this.channelByNumber.put(channel.getOffset(), channel);
|
|
|
|
this.channelByName.put(channel.getName(), channel);
|
2012-03-03 01:29:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-03 17:29:31 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2012-03-03 01:29:06 +00:00
|
|
|
|
2012-03-03 17:29:31 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2012-03-03 01:29:06 +00:00
|
|
|
|
2012-03-03 17:29:31 +00:00
|
|
|
|
2012-03-03 01:29:06 +00:00
|
|
|
/**
|
|
|
|
* Returns an (unmodifiable) collection of all channels
|
|
|
|
* @return unmodifiable collection of all channels
|
|
|
|
*/
|
|
|
|
public Collection<DMXChannel> getAllChannels() {
|
|
|
|
return Collections.unmodifiableCollection(this.channelByNumber.values());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|