001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.monitoring;
019    
020    import org.apache.commons.monitoring.counters.Unit;
021    
022    import static org.apache.commons.monitoring.counters.Unit.Time.NANOSECOND;
023    
024    /**
025     * As a monitored resource may have multipe Metrics, each one has a dedicated 'role' that
026     * defines the type of data or the monitored aspect it handles.
027     *
028     * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
029     */
030    public class Role implements Comparable<Role> {
031        public static final Role WEB = new Role("web", NANOSECOND);
032        public static final Role JSP = new Role("jsp", NANOSECOND);
033        public static final Role JDBC = new Role("jdbc", NANOSECOND);
034        public static final Role PERFORMANCES = new Role("performances", NANOSECOND);
035        public static final Role FAILURES = new Role("failures", Unit.UNARY);
036    
037        private final String name;
038        private final Unit unit;
039    
040        public Role(String name, Unit unit) {
041            if (name == null) {
042                throw new IllegalArgumentException("A role name is required");
043            }
044            if (unit == null) {
045                throw new IllegalArgumentException("A role unit is required");
046            }
047            this.name = name;
048            this.unit = unit;
049        }
050    
051        /**
052         * @return the role
053         */
054        public String getName() {
055            return name;
056        }
057    
058        /**
059         * @return the unit
060         */
061        public Unit getUnit() {
062            return unit;
063        }
064    
065        /**
066         * @see java.lang.Object#hashCode()
067         */
068        @Override
069        public int hashCode() {
070            return name.hashCode();
071        }
072    
073        @Override
074        public boolean equals( Object o )
075        {
076            if ( this == o )
077            {
078                return true;
079            }
080            if ( o == null || getClass() != o.getClass() )
081            {
082                return false;
083            }
084    
085            Role role = (Role) o;
086    
087            if ( !name.equals( role.name ) )
088            {
089                return false;
090            }
091    
092            return true;
093        }
094    
095        /**
096         * {@inheritDoc}
097         */
098        public int compareTo(final Role o) {
099            return name.compareTo(o.name);
100        }
101    
102        @Override
103        public String toString()
104        {
105            final StringBuilder sb = new StringBuilder( "Role{" );
106            sb.append( "name='" ).append( name ).append( '\'' );
107            sb.append( ", unit=" ).append( unit );
108            sb.append( '}' );
109            return sb.toString();
110        }
111    }