001    /*
002     * Copyright (C) 2001 Ciaran Treanor <ciaran@codeloop.com>
003     *
004     * Distributable under GPL license.
005     * See terms of license at gnu.org.
006     *
007     * $Id: DataSourceType.java,v 1.2 2006/12/21 18:02:42 tarus Exp $
008     */
009    package org.jrobin.core.jrrd;
010    
011    /**
012     * Class DataSourceType
013     *
014     * @author <a href="mailto:ciaran@codeloop.com">Ciaran Treanor</a>
015     * @version $Revision: 1.2 $
016     */
017    public class DataSourceType {
018    
019            private static final int _COUNTER = 0;
020            private static final String STR_COUNTER = "COUNTER";
021    
022            /**
023             * Field COUNTER
024             */
025            public static final DataSourceType COUNTER =
026                            new DataSourceType(_COUNTER);
027            private static final int _ABSOLUTE = 1;
028            private static final String STR_ABSOLUTE = "ABSOLUTE";
029    
030            /**
031             * Field ABSOLUTE
032             */
033            public static final DataSourceType ABSOLUTE =
034                            new DataSourceType(_ABSOLUTE);
035            private static final int _GAUGE = 2;
036            private static final String STR_GAUGE = "GAUGE";
037    
038            /**
039             * Field GAUGE
040             */
041            public static final DataSourceType GAUGE = new DataSourceType(_GAUGE);
042            private static final int _DERIVE = 3;
043            private static final String STR_DERIVE = "DERIVE";
044    
045            /**
046             * Field DERIVE
047             */
048            public static final DataSourceType DERIVE = new DataSourceType(_DERIVE);
049            private int type;
050    
051            private DataSourceType(int type) {
052                    this.type = type;
053            }
054    
055            /**
056             * Returns a <code>DataSourceType</code> with the given name.
057             *
058             * @param s name of the <code>DataSourceType</code> required.
059             * @return a <code>DataSourceType</code> with the given name.
060             */
061            public static DataSourceType get(String s) {
062    
063                    if (s.equalsIgnoreCase(STR_COUNTER)) {
064                            return COUNTER;
065                    }
066                    else if (s.equalsIgnoreCase(STR_ABSOLUTE)) {
067                            return ABSOLUTE;
068                    }
069                    else if (s.equalsIgnoreCase(STR_GAUGE)) {
070                            return GAUGE;
071                    }
072                    else if (s.equalsIgnoreCase(STR_DERIVE)) {
073                            return DERIVE;
074                    }
075                    else {
076                            throw new IllegalArgumentException("Invalid DataSourceType");
077                    }
078            }
079    
080            /**
081             * Compares this object against the specified object.
082             *
083             * @return <code>true</code> if the objects are the same,
084             *         <code>false</code> otherwise.
085             */
086            public boolean equals(Object obj) {
087    
088                    if (!(obj instanceof DataSourceType)) {
089                            throw new IllegalArgumentException("Not a DataSourceType");
090                    }
091    
092                    return (((DataSourceType) obj).type == type)
093                                    ? true
094                                    : false;
095            }
096    
097            /**
098             * Returns a string representation of this object.
099             *
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    }