KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > storage > api > AbstractEvent


1 /*
2 * CLIF is a Load Injection Framework
3 * Copyright (C) 2003-2004 France Telecom R&D
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * CLIF $Name: $
20 *
21 * Contact: clif@objectweb.org
22 */

23
24 package org.objectweb.clif.storage.api;
25
26 import org.objectweb.clif.storage.api.BladeEvent;
27 import java.util.Map JavaDoc;
28 import java.util.HashMap JavaDoc;
29
30 /**
31  * Abstract implementation of a blade event.
32  * All events managed by CLIF shall derive from this class. Each event type shall have a unique
33  * label, which must be registered using the static registerEventFieldLabels method
34  * (typically in the a static section of the event class).
35  * Events are "Comparable" on their dates.
36  * @see #registerEventFieldLabels(String, String[])
37  * @author Bruno Dillenseger
38  */

39 abstract public class AbstractEvent implements BladeEvent
40 {
41     static private Map JavaDoc eventFieldLabels = new HashMap JavaDoc();
42
43
44     /**
45      * Registers the given event type label (which must be unique) and associates the fields
46      * labels for this event type. Enforces event type label unicity.
47      * @param event_label the unique label designating this event type
48      * @param field_labels the array of field labels, describing the content of each field
49      * this type of event holds. These labels must be in the same order as the fields are
50      * printed when the toString() method is called on this type of event.
51      * @throws Error Attempt to register several times the same type label
52      * @see BladeEvent#toString(long, String)
53      */

54     static protected synchronized void registerEventFieldLabels(
55         String JavaDoc event_label,
56         String JavaDoc[] field_labels)
57     {
58         if (! eventFieldLabels.containsKey(event_label))
59         {
60             eventFieldLabels.put(event_label, field_labels);
61         }
62         else
63         {
64             throw new Error JavaDoc("Duplicate event type label declaration for " + event_label);
65         }
66     }
67
68
69     /**
70      * Get field labels of a given event type.
71      * @param event_label label of the event type
72      * @return the field labels of the given event type.
73      */

74     static public String JavaDoc[] getEventFieldLabels(String JavaDoc event_label)
75     {
76         return (String JavaDoc[])eventFieldLabels.get(event_label);
77     }
78
79
80     protected long date;
81     protected String JavaDoc bladeId;
82
83
84     public AbstractEvent()
85     {
86         this(System.currentTimeMillis(), null);
87     }
88
89
90     public AbstractEvent(long date, String JavaDoc bladeId)
91     {
92         this.date = date;
93         this.bladeId = bladeId;
94     }
95
96
97     public long getDate()
98     {
99         return date;
100     }
101
102
103     public void setDate(long date)
104     {
105         this.date = date;
106     }
107
108
109     public String JavaDoc getBladeId()
110     {
111         return bladeId;
112     }
113
114
115     public void setBladeId(String JavaDoc newId)
116     {
117         this.bladeId = newId;
118     }
119
120
121     //////////////////////////
122
// interface Comparable //
123
//////////////////////////
124

125
126     /**
127      * The order is based on dates. In case dates are the same, an arbitrary order is applied based
128      * on hashcodes.
129      */

130     public int compareTo(Object JavaDoc obj)
131         throws ClassCastException JavaDoc
132     {
133         int diff = (int)(date - ((BladeEvent)obj).getDate());
134         return diff == 0 ? hashCode() - obj.hashCode() : diff;
135     }
136 }
137
Popular Tags