2
|
1 |
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
|
|
2 |
// Use of this source code is governed by a BSD-style license that can be
|
|
3 |
// found in the LICENSE file.
|
|
4 |
|
|
5 |
package org.chromium.debug.core.model;
|
|
6 |
|
|
7 |
import java.util.ArrayList;
|
|
8 |
import java.util.List;
|
|
9 |
|
|
10 |
/**
|
|
11 |
* Helper class that destructs unfinished objects. It is needed when Java GC is not enough.
|
|
12 |
* It requires to be explicitly discharged if all went OK and destruction should be cancelled.
|
|
13 |
* Using this class may be more convenient that try/finally in Java.
|
|
14 |
*/
|
|
15 |
public class DestructingGuard {
|
|
16 |
/**
|
|
17 |
* Confirms that constructing has finished OKAY and no destruction is needed from now.
|
|
18 |
*/
|
|
19 |
public void discharge() {
|
|
20 |
discharged = true;
|
|
21 |
}
|
|
22 |
|
|
23 |
/**
|
|
24 |
* This method is supposed to be called from finally clause. It performs destructing
|
|
25 |
* unless {@link #discharge()} has been called.
|
|
26 |
*/
|
|
27 |
public void doFinally() {
|
|
28 |
if (discharged) {
|
|
29 |
return;
|
|
30 |
}
|
|
31 |
for (int i = destructables.size() - 1; i >= 0; i--) {
|
|
32 |
destructables.get(i).destruct();
|
|
33 |
}
|
|
34 |
discharged = true;
|
|
35 |
}
|
|
36 |
|
|
37 |
/**
|
|
38 |
* Adds another value that should be destructed. Added values are destructed in reversed order.
|
|
39 |
*/
|
|
40 |
public void addValue(Destructable destructable) {
|
|
41 |
this.destructables.add(destructable);
|
|
42 |
}
|
|
43 |
|
|
44 |
private List<Destructable> destructables = new ArrayList<Destructable>(1);
|
|
45 |
private boolean discharged = false;
|
|
46 |
}
|