Author: nicolas
Date: Thu Feb 19 13:17:55 2009
New Revision: 745850
URL: http://svn.apache.org/viewvc?rev=745850&view=rev
Log:
fix pseudo-unicity of roles
Modified:
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/Role.java
Modified:
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/Role.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/Role.java?rev=745850&r1=745849&r2=745850&view=diff
==============================================================================
---
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/Role.java
(original)
+++
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/Role.java
Thu Feb 19 13:17:55 2009
@@ -19,8 +19,8 @@
import java.util.Collection;
import java.util.Collections;
-import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
/**
* As a monitored resource may have multipe Metrics, each one has a dedicated
'role' that
@@ -29,7 +29,6 @@
* @author <a href="mailto:[email protected]">Nicolas De Loof</a>
* @param <M> The metric this role relates to
*/
-...@suppresswarnings("unchecked")
public class Role
implements Comparable<Role>
{
@@ -39,7 +38,7 @@
private Metric.Type type;
- private static final Map<String, Role> ROLES = new
ConcurrentHashMap<String, Role>();
+ private static final ConcurrentMap<String, Role> ROLES = new
ConcurrentHashMap<String, Role>();
public static Role getRole( String name )
{
@@ -70,7 +69,18 @@
this.name = name;
this.unit = unit;
this.type = type;
- ROLES.put( name, this );
+ Role old = ROLES.putIfAbsent( name, this );
+ if ( old != null )
+ {
+ if ( !type.equals( old.type ) )
+ {
+ throw new IllegalStateException( "A role already exists with
this name but distinct type" );
+ }
+ if ( !unit.equals( old.unit ) )
+ {
+ throw new IllegalStateException( "A role already exists with
this name but distinct unit" );
+ }
+ }
}
/**