View Javadoc

1   package org.lsst.ccs.bus;
2   
3   import java.io.Serializable;
4   
5   /**
6    * Bus definitions.
7    *
8    * @ImplSpec It is important that this list can be extended (for instance by
9    * creating an Event bus)
10   * @ImplNote Since there is no such thing as a parameterized enum, this class
11   * emulates one: it lists the possible bus topics.
12   */
13  //TODO: why Serializable?
14  public class Bus<T extends BusPayload> implements Serializable {
15  
16  	public static final Bus<LogEvent> LOG = new Bus<LogEvent>("LOG", 0);
17  	public static final Bus<Status> STATUS = new Bus<Status>("STATUS", 1);
18  	public static final Bus<CommandBusMessage> COMMAND = new Bus<CommandBusMessage>("COMMAND", 2);
19  	private static final Bus[] vals = {LOG, STATUS, COMMAND};
20  	private static final long serialVersionUID = -5478704370712583354L;
21  
22  	private String name;
23  	private int ordinal;
24  
25  	private Bus(String name, int ordinal) {
26  		this.name = name;
27  		this.ordinal = ordinal;
28  	}
29  
30  	public String toString() {
31  		return this.name;
32  	}
33  
34  	public int ordinal() {
35  		return this.ordinal;
36  	}
37  
38  	/***
39  	 * emulates the <TT>valueOf</TT> method of enums
40  	 * @param str
41  	 * @return 
42  	 */
43  	public static Bus valueOf(String str) {
44  		// should be a switch but is realised as a 1.6 compatible code
45  		String upperStr = str != null ? str.toUpperCase() : str;
46  		switch (upperStr) {
47  			case "COMMAND":
48  				return COMMAND;
49  			case "STATUS":
50  				return STATUS;
51  			case "LOG":
52  				return LOG;
53  			default:
54  				throw new IllegalArgumentException(str);
55  
56  		}
57  	}
58  
59  	/***
60  	 * emulates the <TT>values</TT> method of enums
61  	 */
62  	public static Bus[] values() {
63  		return vals;
64  	}
65  }