KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > event > IvyEvent


1 /*
2  * This file is subject to the licence found in LICENCE.TXT in the root directory of the project.
3  * Copyright Jayasoft 2005 - All rights reserved
4  *
5  * #SNAPSHOT#
6  */

7 package fr.jayasoft.ivy.event;
8
9 import java.util.HashMap JavaDoc;
10 import java.util.Map JavaDoc;
11
12 import fr.jayasoft.ivy.Ivy;
13 import fr.jayasoft.ivy.ModuleDescriptor;
14 import fr.jayasoft.ivy.ModuleId;
15 import fr.jayasoft.ivy.ModuleRevisionId;
16 import fr.jayasoft.ivy.util.StringUtils;
17
18 /**
19  * The root of all ivy events
20  *
21  * Any ivy event knows which ivy instance triggered the event (the source)
22  * and also has a name and a map of attributes.
23  *
24  * The name of the event represents the event type, usually there is a one - one
25  * mapping between event names and IvyEvent subclass, even if this is not mandatory.
26  * Example:
27  * pre-resolve
28  * pre-resolve-dependency
29  * post-download
30  *
31  * The map of attributes is a Map from String keys to String values.
32  * It is especially useful to filter events, and to get some of their essential data in
33  * some context where access to Java types is not easy (in an ant build file, for example),
34  * Example:
35  * pre-resolve (organisation=foo, module=bar, revision=1.0, conf=default)
36  * post-download (organisation=foo, module=bar, revision=1.0, artifact=foo-test, type=jar, ext=jar)
37  *
38  * @author Xavier Hanin
39  *
40  */

41 public class IvyEvent {
42     private Ivy _source;
43     private String JavaDoc _name;
44     private Map JavaDoc _attributes = new HashMap JavaDoc();
45
46     protected IvyEvent(Ivy source, String JavaDoc name) {
47         _source = source;
48         _name = name;
49     }
50     
51     /**
52      * Should only be called during event object construction, since events should be immutable
53      * @param key
54      * @param value
55      */

56     protected void addAttribute(String JavaDoc key, String JavaDoc value) {
57         _attributes.put(key, value);
58     }
59     protected void addMDAttributes(ModuleDescriptor md) {
60         addMridAttributes(md.getResolvedModuleRevisionId());
61     }
62
63     protected void addMridAttributes(ModuleRevisionId mrid) {
64         addModuleIdAttributes(mrid.getModuleId());
65         addAttribute("revision", mrid.getRevision());
66         addAttributes(mrid.getExtraAttributes());
67     }
68
69     protected void addModuleIdAttributes(ModuleId moduleId) {
70         addAttribute("organisation", moduleId.getOrganisation());
71         addAttribute("module", moduleId.getName());
72     }
73
74     protected void addConfsAttribute(String JavaDoc[] confs) {
75         addAttribute("conf", StringUtils.join(confs, ", "));
76     }
77
78     protected void addAttributes(Map JavaDoc attributes) {
79         _attributes.putAll(attributes);
80     }
81     
82
83     public Ivy getSource() {
84         return _source;
85     }
86
87     public String JavaDoc getName() {
88         return _name;
89     }
90
91     /**
92      * Returns the attributes of this event, as a Map(String->String)
93      * @return the attributes of this event, as a Map(String->String)
94      */

95     public Map JavaDoc getAttributes() {
96         return new HashMap JavaDoc(_attributes);
97     }
98     
99     public String JavaDoc toString() {
100         return getName()+" "+getAttributes();
101     }
102     
103     public boolean equals(Object JavaDoc obj) {
104         if (! (obj instanceof IvyEvent)) {
105             return false;
106         }
107         IvyEvent e = (IvyEvent) obj;
108         
109         return getSource().equals(e.getSource())
110             && getName().equals(e.getName())
111             && _attributes.equals(e._attributes);
112     }
113     
114     public int hashCode() {
115         int hash = 37;
116         hash = 13 * hash + getSource().hashCode();
117         hash = 13 * hash + getName().hashCode();
118         hash = 13 * hash + _attributes.hashCode();
119         return hash;
120     }
121 }
122
Popular Tags