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