comparison src/org/tmatesoft/hg/util/CancelSupport.java @ 148:1a7a9a20e1f9

Exceptions, javadoc. Initial cancel and progress support
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 23 Feb 2011 22:36:28 +0100
parents
children 41a778e3fd31
comparison
equal deleted inserted replaced
147:a05145db4d0c 148:1a7a9a20e1f9
1 /*
2 * Copyright (c) 2011 TMate Software Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com
16 */
17 package org.tmatesoft.hg.util;
18
19 /**
20 * Mix-in for objects that support cancellation.
21 *
22 * @author Artem Tikhomirov
23 * @author TMate Software Ltd.
24 */
25 public interface CancelSupport {
26
27 /**
28 * This method is invoked to check if target had been brought to canceled state. Shall silently return if target is
29 * in regular state.
30 * @throws CancelledException when target internal state has been changed to canceled.
31 */
32 void checkCancelled() throws CancelledException;
33
34
35 // Yeah, this factory class looks silly now, but perhaps in the future I'll need wrappers for other cancellation sources?
36 // just don't want to have general Utils class with methods like get() below
37 static class Factory {
38
39 /**
40 * Obtain non-null cancel support object.
41 *
42 * @param target any object (or <code>null</code>) that might have cancel support
43 * @return target if it's capable checking cancellation status or no-op implementation that never cancels.
44 */
45 public static CancelSupport get(Object target) {
46 if (target instanceof CancelSupport) {
47 return (CancelSupport) target;
48 }
49 if (target instanceof Adaptable) {
50 CancelSupport cs = ((Adaptable) target).getAdapter(CancelSupport.class);
51 if (cs != null) {
52 return cs;
53 }
54 }
55 return new CancelSupport() {
56 public void checkCancelled() {
57 }
58 };
59 }
60 }
61 }