You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Event.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. /* $Id$ */
  18. package org.apache.fop.events;
  19. import java.util.Collections;
  20. import java.util.EventObject;
  21. import java.util.Map;
  22. import org.apache.fop.events.model.EventSeverity;
  23. /**
  24. * This is the default event class used by this package. Each event has a unique event identifier
  25. * (a String), a severity indicator and a map of name/value pairs.
  26. */
  27. public class Event extends EventObject {
  28. private static final long serialVersionUID = -1310594422868258083L;
  29. private String eventID;
  30. private EventSeverity severity;
  31. private Map params;
  32. /**
  33. * Creates a new Event.
  34. * @param source the object that creates the event
  35. * @param eventID the unique identifier of the event
  36. * @param severity the severity level
  37. * @param params the event parameters (a map of name/value pairs)
  38. */
  39. public Event(Object source, String eventID, EventSeverity severity, Map params) {
  40. super(source);
  41. this.eventID = eventID;
  42. setSeverity(severity);
  43. this.params = params;
  44. }
  45. /**
  46. * Returns the event identifier.
  47. * @return the event identifier
  48. */
  49. public String getEventID() {
  50. return this.eventID;
  51. }
  52. /**
  53. * Returns the event group identifier.
  54. * @return the event group identifier (or null if there is no group identifier)
  55. */
  56. public String getEventGroupID() {
  57. int pos = this.eventID.lastIndexOf('.');
  58. if (pos > 0) {
  59. return this.eventID.substring(0, pos);
  60. } else {
  61. return null;
  62. }
  63. }
  64. /**
  65. * Returns the severity level.
  66. * @return the severity level
  67. */
  68. public EventSeverity getSeverity() {
  69. return this.severity;
  70. }
  71. /**
  72. * Sets the event's severity level. This method can be used to increase or decrease the
  73. * severity level in a listener.
  74. * @param severity the new event severity
  75. */
  76. public void setSeverity(EventSeverity severity) {
  77. this.severity = severity;
  78. }
  79. /**
  80. * Returns a parameter.
  81. * @param key the key to the parameter
  82. * @return the parameter value or null if no value with this key is found
  83. */
  84. public Object getParam(String key) {
  85. if (this.params != null) {
  86. return this.params.get(key);
  87. } else {
  88. return null;
  89. }
  90. }
  91. /**
  92. * Returns an unmodifiable {@link java.util.Map} with all event parameters.
  93. * @return the parameter map
  94. */
  95. public Map getParams() {
  96. return Collections.unmodifiableMap(this.params);
  97. }
  98. /**
  99. * Creates and returns a fluent builder object for building up the parameter map.
  100. * @return the parameter builder
  101. */
  102. public static ParamsBuilder paramsBuilder() {
  103. return new ParamsBuilder();
  104. }
  105. /**
  106. * This class is a fluent builder class for building up the parameter map.
  107. */
  108. public static class ParamsBuilder {
  109. private Map params;
  110. /**
  111. * Adds a new parameter (a name/value pair).
  112. * @param name the name of the parameter
  113. * @param value the value of the parameter
  114. * @return this instance
  115. */
  116. public ParamsBuilder param(String name, Object value) {
  117. if (this.params == null) {
  118. this.params = new java.util.HashMap();
  119. }
  120. this.params.put(name, value);
  121. return this;
  122. }
  123. /**
  124. * Returns the accumulated parameter map.
  125. * @return the accumulated parameter map
  126. */
  127. public Map build() {
  128. return this.params;
  129. }
  130. }
  131. }