summaryrefslogtreecommitdiffstats
path: root/jack-api/src/com/android/jack/api/v01/ChainedException.java
blob: 1efea1679487cb9d8016ff74d2cc52263500d974 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.jack.api.v01;

import java.util.ArrayList;
import java.util.Iterator;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;

/**
 * Abstract class to easily chain exceptions together.
 *
 * The exception can be managed like any other exception. In this case, the first one will be the
 * only one used.
 *
 * Special management can use the {@link #iterator()} or the {@link #getNextException()} to browse
 * all chained exceptions and dispatch them.
 *
 * See {@link ChainedExceptionBuilder} to build the chain of exceptions.
 */
public abstract class ChainedException extends Exception
    implements Iterable<ChainedException> {
  private static final long serialVersionUID = 1L;

  @Nonnull
  private String message;

  @Nonnegative
  private int count = 1;

  @Nonnull
  private ChainedException tail = this;

  @CheckForNull
  private ChainedException next = null;

  public ChainedException(@Nonnull String message) {
    super("");
    this.message = message;
  }

  public ChainedException(@Nonnull String message, @Nonnull Throwable cause) {
    super("", cause);
    this.message = message;
  }

  public ChainedException(@Nonnull Throwable cause) {
    super(cause);
    this.message = cause.getMessage();
  }

  @Override
  @Nonnull
  public String getMessage() {
    return message;
  }

  @Override
  @Nonnull
  public String getLocalizedMessage() {
    return message;
  }

  public void setMessage(@Nonnull String message) {
    this.message = message;
  }

  @Nonnull
  protected ChainedException putAsLastExceptionOf(
      @CheckForNull ChainedException head) {
    if (head == null) {
      this.tail  = this;
      this.next  = null;
      this.count = 1;

      return this;
    } else {
      head.tail.next = this;
      head.tail = this;
      head.count++;

      return head;
    }
  }

  @CheckForNull
  public ChainedException getNextException() {
    return next;
  }

  @Nonnegative
  public int getNextExceptionCount() {
    return count;
  }

  @Override
  @Nonnull
  public Iterator<ChainedException> iterator() {
    ArrayList<ChainedException> list = new ArrayList<ChainedException>(count);

    ChainedException exception = this;
    do {
      list.add(exception);
      exception = exception.next;
    } while (exception != null);

    return list.iterator();
  }

  /**
   * Builder to construct a chain of exceptions.
   */
  public static class ChainedExceptionBuilder<T extends ChainedException> {
    @CheckForNull
    private T head = null;

    @SuppressWarnings("unchecked")
    public void appendException(@Nonnull T exceptions) {
      for (ChainedException exception : exceptions) {
        head = (T) exception.putAsLastExceptionOf(head);
      }
    }

    public void throwIfNecessary() throws T {
      if (head != null) {
        throw head;
      }
    }

    @Nonnull
    public T getException() {
      assert head != null;
      return head;
    }
  }
}