View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.monitoring;
19  
20  import org.apache.commons.monitoring.counters.Unit;
21  
22  import static org.apache.commons.monitoring.counters.Unit.Time.NANOSECOND;
23  
24  /**
25   * As a monitored resource may have multipe Metrics, each one has a dedicated 'role' that
26   * defines the type of data or the monitored aspect it handles.
27   *
28   * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
29   */
30  public class Role implements Comparable<Role> {
31      public static final Role WEB = new Role("web", NANOSECOND);
32      public static final Role JSP = new Role("jsp", NANOSECOND);
33      public static final Role JDBC = new Role("jdbc", NANOSECOND);
34      public static final Role PERFORMANCES = new Role("performances", NANOSECOND);
35      public static final Role FAILURES = new Role("failures", Unit.UNARY);
36  
37      private final String name;
38      private final Unit unit;
39  
40      public Role(String name, Unit unit) {
41          if (name == null) {
42              throw new IllegalArgumentException("A role name is required");
43          }
44          if (unit == null) {
45              throw new IllegalArgumentException("A role unit is required");
46          }
47          this.name = name;
48          this.unit = unit;
49      }
50  
51      /**
52       * @return the role
53       */
54      public String getName() {
55          return name;
56      }
57  
58      /**
59       * @return the unit
60       */
61      public Unit getUnit() {
62          return unit;
63      }
64  
65      /**
66       * @see java.lang.Object#hashCode()
67       */
68      @Override
69      public int hashCode() {
70          return name.hashCode();
71      }
72  
73      @Override
74      public boolean equals( Object o )
75      {
76          if ( this == o )
77          {
78              return true;
79          }
80          if ( o == null || getClass() != o.getClass() )
81          {
82              return false;
83          }
84  
85          Role role = (Role) o;
86  
87          if ( !name.equals( role.name ) )
88          {
89              return false;
90          }
91  
92          return true;
93      }
94  
95      /**
96       * {@inheritDoc}
97       */
98      public int compareTo(final Role o) {
99          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 }