Mercurial > jhg
comparison src/org/tmatesoft/hg/internal/AdapterPlug.java @ 520:1ee452f31187
Experimental support for inverse direction history walking. Refactored/streamlined cancellation in HgLogCommand and down the stack
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Fri, 21 Dec 2012 21:20:26 +0100 |
parents | |
children | 90df078d6418 |
comparison
equal
deleted
inserted
replaced
519:934037edbea0 | 520:1ee452f31187 |
---|---|
1 /* | |
2 * Copyright (c) 2012 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.internal; | |
18 | |
19 import java.util.ArrayList; | |
20 import java.util.HashMap; | |
21 import java.util.List; | |
22 import java.util.Map; | |
23 | |
24 import org.tmatesoft.hg.util.Adaptable; | |
25 | |
26 /** | |
27 * Implementation of {@link Adaptable} that allows to add ("plug") | |
28 * adapters as needed | |
29 * | |
30 * @author Artem Tikhomirov | |
31 * @author TMate Software Ltd. | |
32 */ | |
33 public class AdapterPlug implements Adaptable { | |
34 private final Map<Class<?>, Object> adapters = new HashMap<Class<?>, Object>(); | |
35 private final List<Class<?>> adapterUses = new ArrayList<Class<?>>(); | |
36 | |
37 public <T> void attachAdapter(Class<T> adapterClass, T instance) { | |
38 adapters.put(adapterClass, instance); | |
39 } | |
40 | |
41 public <T> T getAdapter(Class<T> adapterClass) { | |
42 Object instance = adapters.get(adapterClass); | |
43 if (instance != null) { | |
44 adapterUses.add(adapterClass); | |
45 return adapterClass.cast(instance); | |
46 } | |
47 return null; | |
48 } | |
49 | |
50 public int getAdapterUse(Class<?> adapterClass) { | |
51 int uses = 0; | |
52 for (Class<?> c : adapterUses) { | |
53 if (c == adapterClass) { | |
54 uses++; | |
55 } | |
56 } | |
57 return uses; | |
58 } | |
59 } |