View Javadoc

1   /*
2    * Copyright (C) 2001 Ciaran Treanor <ciaran@codeloop.com>
3    *
4    * Distributable under GPL license.
5    * See terms of license at gnu.org.
6    *
7    * $Id: DataSourceType.java,v 1.2 2006/12/21 18:02:42 tarus Exp $
8    */
9   package org.jrobin.core.jrrd;
10  
11  /**
12   * Class DataSourceType
13   *
14   * @author <a href="mailto:ciaran@codeloop.com">Ciaran Treanor</a>
15   * @version $Revision: 1.2 $
16   */
17  public class DataSourceType {
18  
19  	private static final int _COUNTER = 0;
20  	private static final String STR_COUNTER = "COUNTER";
21  
22  	/**
23  	 * Field COUNTER
24  	 */
25  	public static final DataSourceType COUNTER =
26  			new DataSourceType(_COUNTER);
27  	private static final int _ABSOLUTE = 1;
28  	private static final String STR_ABSOLUTE = "ABSOLUTE";
29  
30  	/**
31  	 * Field ABSOLUTE
32  	 */
33  	public static final DataSourceType ABSOLUTE =
34  			new DataSourceType(_ABSOLUTE);
35  	private static final int _GAUGE = 2;
36  	private static final String STR_GAUGE = "GAUGE";
37  
38  	/**
39  	 * Field GAUGE
40  	 */
41  	public static final DataSourceType GAUGE = new DataSourceType(_GAUGE);
42  	private static final int _DERIVE = 3;
43  	private static final String STR_DERIVE = "DERIVE";
44  
45  	/**
46  	 * Field DERIVE
47  	 */
48  	public static final DataSourceType DERIVE = new DataSourceType(_DERIVE);
49  	private int type;
50  
51  	private DataSourceType(int type) {
52  		this.type = type;
53  	}
54  
55  	/**
56  	 * Returns a <code>DataSourceType</code> with the given name.
57  	 *
58  	 * @param s name of the <code>DataSourceType</code> required.
59  	 * @return a <code>DataSourceType</code> with the given name.
60  	 */
61  	public static DataSourceType get(String s) {
62  
63  		if (s.equalsIgnoreCase(STR_COUNTER)) {
64  			return COUNTER;
65  		}
66  		else if (s.equalsIgnoreCase(STR_ABSOLUTE)) {
67  			return ABSOLUTE;
68  		}
69  		else if (s.equalsIgnoreCase(STR_GAUGE)) {
70  			return GAUGE;
71  		}
72  		else if (s.equalsIgnoreCase(STR_DERIVE)) {
73  			return DERIVE;
74  		}
75  		else {
76  			throw new IllegalArgumentException("Invalid DataSourceType");
77  		}
78  	}
79  
80  	/**
81  	 * Compares this object against the specified object.
82  	 *
83  	 * @return <code>true</code> if the objects are the same,
84  	 *         <code>false</code> otherwise.
85  	 */
86  	public boolean equals(Object obj) {
87  
88  		if (!(obj instanceof DataSourceType)) {
89  			throw new IllegalArgumentException("Not a DataSourceType");
90  		}
91  
92  		return (((DataSourceType) obj).type == type)
93  				? true
94  				: false;
95  	}
96  
97  	/**
98  	 * Returns a string representation of this object.
99  	 *
100 	 * @return a string representation of this object.
101 	 */
102 	public String toString() {
103 
104 		String strType;
105 
106 		switch (type) {
107 
108 			case _COUNTER:
109 				strType = STR_COUNTER;
110 				break;
111 
112 			case _ABSOLUTE:
113 				strType = STR_ABSOLUTE;
114 				break;
115 
116 			case _GAUGE:
117 				strType = STR_GAUGE;
118 				break;
119 
120 			case _DERIVE:
121 				strType = STR_DERIVE;
122 				break;
123 
124 			default :
125 				// Don't you just hate it when you see a line like this?
126 				throw new RuntimeException("This should never happen");
127 		}
128 
129 		return strType;
130 	}
131 }