tikhomirov@148: /*
tikhomirov@148: * Copyright (c) 2011 TMate Software Ltd
tikhomirov@148: *
tikhomirov@148: * This program is free software; you can redistribute it and/or modify
tikhomirov@148: * it under the terms of the GNU General Public License as published by
tikhomirov@148: * the Free Software Foundation; version 2 of the License.
tikhomirov@148: *
tikhomirov@148: * This program is distributed in the hope that it will be useful,
tikhomirov@148: * but WITHOUT ANY WARRANTY; without even the implied warranty of
tikhomirov@148: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tikhomirov@148: * GNU General Public License for more details.
tikhomirov@148: *
tikhomirov@148: * For information on how to redistribute this software under
tikhomirov@148: * the terms of a license other than GNU General Public License
tikhomirov@148: * contact TMate Software at support@hg4j.com
tikhomirov@148: */
tikhomirov@148: package org.tmatesoft.hg.util;
tikhomirov@148:
tikhomirov@148: /**
tikhomirov@148: * Mix-in for objects that support cancellation.
tikhomirov@148: *
tikhomirov@148: * @author Artem Tikhomirov
tikhomirov@148: * @author TMate Software Ltd.
tikhomirov@148: */
tikhomirov@148: public interface CancelSupport {
tikhomirov@148:
tikhomirov@148: /**
tikhomirov@148: * This method is invoked to check if target had been brought to canceled state. Shall silently return if target is
tikhomirov@148: * in regular state.
tikhomirov@148: * @throws CancelledException when target internal state has been changed to canceled.
tikhomirov@148: */
tikhomirov@148: void checkCancelled() throws CancelledException;
tikhomirov@148:
tikhomirov@148:
tikhomirov@148: // Yeah, this factory class looks silly now, but perhaps in the future I'll need wrappers for other cancellation sources?
tikhomirov@148: // just don't want to have general Utils class with methods like get() below
tikhomirov@148: static class Factory {
tikhomirov@148:
tikhomirov@148: /**
tikhomirov@148: * Obtain non-null cancel support object.
tikhomirov@148: *
tikhomirov@215: * @param target any object (or null
) that might have cancel support. For null
, returns an instance than never cancels.
tikhomirov@148: * @return target if it's capable checking cancellation status or no-op implementation that never cancels.
tikhomirov@148: */
tikhomirov@148: public static CancelSupport get(Object target) {
tikhomirov@312: CancelSupport cs = get(target, null);
tikhomirov@312: if (cs != null) {
tikhomirov@312: return cs;
tikhomirov@312: }
tikhomirov@312: return new CancelSupport() {
tikhomirov@312: public void checkCancelled() {
tikhomirov@312: }
tikhomirov@312: };
tikhomirov@312: }
tikhomirov@312:
tikhomirov@312: public static CancelSupport get(Object target, CancelSupport defaultValue) {
tikhomirov@148: if (target instanceof CancelSupport) {
tikhomirov@148: return (CancelSupport) target;
tikhomirov@148: }
tikhomirov@148: if (target instanceof Adaptable) {
tikhomirov@148: CancelSupport cs = ((Adaptable) target).getAdapter(CancelSupport.class);
tikhomirov@148: if (cs != null) {
tikhomirov@148: return cs;
tikhomirov@148: }
tikhomirov@148: }
tikhomirov@312: return defaultValue;
tikhomirov@148: }
tikhomirov@148: }
tikhomirov@215:
tikhomirov@215: interface Target {
tikhomirov@215: T set(CancelSupport cs);
tikhomirov@215: }
tikhomirov@148: }