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  package org.apache.commons.lang3.exception;
18  
19  import java.util.List;
20  import java.util.Set;
21  
22  import org.apache.commons.lang3.tuple.Pair;
23  
24  /**
25   * Allows the storage and retrieval of contextual information based on label-value
26   * pairs for exceptions.
27   * <p>
28   * Implementations are expected to manage the pairs in a list-style collection
29   * that keeps the pairs in the sequence of their addition.
30   * </p>
31   * 
32   * @see ContextedException
33   * @see ContextedRuntimeException
34   * @since 3.0
35   */
36  public interface ExceptionContext {
37  
38      /**
39       * Adds a contextual label-value pair into this context.
40       * <p>
41       * The pair will be added to the context, independently of an already
42       * existing pair with the same label.
43       * </p>
44       * 
45       * @param label  the label of the item to add, {@code null} not recommended
46       * @param value  the value of item to add, may be {@code null}
47       * @return {@code this}, for method chaining, not {@code null}
48       */
49      ExceptionContext addContextValue(String label, Object value);
50  
51      /**
52       * Sets a contextual label-value pair into this context.
53       * <p>
54       * The pair will be added normally, but any existing label-value pair with
55       * the same label is removed from the context.
56       * </p>
57       * 
58       * @param label  the label of the item to add, {@code null} not recommended
59       * @param value  the value of item to add, may be {@code null}
60       * @return {@code this}, for method chaining, not {@code null}
61       */
62      ExceptionContext setContextValue(String label, Object value);
63  
64      /**
65       * Retrieves all the contextual data values associated with the label.
66       * 
67       * @param label  the label to get the contextual values for, may be {@code null}
68       * @return the contextual values associated with the label, never {@code null}
69       */
70      List<Object> getContextValues(String label);
71  
72      /**
73       * Retrieves the first available contextual data value associated with the label.
74       * 
75       * @param label  the label to get the contextual value for, may be {@code null}
76       * @return the first contextual value associated with the label, may be {@code null}
77       */
78      Object getFirstContextValue(String label);
79  
80      /**
81       * Retrieves the full set of labels defined in the contextual data.
82       * 
83       * @return the set of labels, not {@code null}
84       */
85      Set<String> getContextLabels();
86  
87      /**
88       * Retrieves the full list of label-value pairs defined in the contextual data.
89       * 
90       * @return the list of pairs, not {@code null}
91       */
92      List<Pair<String, Object>> getContextEntries();
93  
94      /**
95       * Gets the contextualized error message based on a base message.
96       * This will add the context label-value pairs to the message.
97       * 
98       * @param baseMessage  the base exception message <b>without</b> context information appended
99       * @return the exception message <b>with</b> context information appended, not {@code null}
100      */
101     String getFormattedExceptionMessage(String baseMessage);
102 
103 }