Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Resolver |
|
| 2.5;2.5 |
1 | /******************************************************************************* | |
2 | * Copyright (c) 2008 Ketan Padegaonkar and others. | |
3 | * All rights reserved. This program and the accompanying materials | |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
7 | * | |
8 | * Contributors: | |
9 | * Ketan Padegaonkar - initial API and implementation | |
10 | *******************************************************************************/ | |
11 | package org.eclipse.swtbot.swt.finder.resolvers; | |
12 | ||
13 | import java.util.ArrayList; | |
14 | import java.util.Collection; | |
15 | import java.util.LinkedHashSet; | |
16 | import java.util.List; | |
17 | import java.util.Set; | |
18 | ||
19 | import org.eclipse.swtbot.swt.finder.utils.MultiValueMap; | |
20 | ||
21 | /** | |
22 | * A resolver that maps classes to the {@link IChildrenResolver}s that resolve the classes. | |
23 | * | |
24 | * @see IChildrenResolver#getResolvableClasses() | |
25 | * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com> | |
26 | * @version $Id$ | |
27 | */ | |
28 | 10110 | public class Resolver { |
29 | ||
30 | /** The map that maps classes to {@link IChildrenResolver}s */ | |
31 | 5055 | private final MultiValueMap<Class<?>, IResolvable> map = new MultiValueMap<Class<?>, IResolvable>(); |
32 | ||
33 | /** | |
34 | * Map all the classes that the resolver resolver to the resolver. | |
35 | * | |
36 | * @param resolver the resolver. | |
37 | * @see IChildrenResolver#getResolvableClasses() | |
38 | */ | |
39 | public void addResolver(IResolvable resolver) { | |
40 | 45479 | Class<?>[] resolvableClasses = resolver.getResolvableClasses(); |
41 | 45479 | if ((resolvableClasses != null) && (resolvableClasses.length > 0)) |
42 | 40426 | addResolver(resolver, resolvableClasses); |
43 | 45479 | } |
44 | ||
45 | /** | |
46 | * Gets the resolvers that match the given class. | |
47 | * | |
48 | * @param clazz the class that should be resolved using the resolvers. | |
49 | * @return the list of {@link Resolver}s that can resolve objects of type <code>clazz</code> | |
50 | */ | |
51 | public List<IResolvable> getResolvers(Class<?> clazz) { | |
52 | ||
53 | 290843 | LinkedHashSet<IResolvable> result = new LinkedHashSet<IResolvable>(); |
54 | ||
55 | 290843 | Collection<IResolvable> resolvers = map.getCollection(clazz); |
56 | ||
57 | 290843 | if ((resolvers != null) && !resolvers.isEmpty()) |
58 | 171520 | result.addAll(resolvers); |
59 | 119323 | else if (!Object.class.equals(clazz)) |
60 | 117344 | result.addAll(getResolvers(clazz.getSuperclass())); |
61 | ||
62 | 290843 | return new ArrayList<IResolvable>(result); |
63 | } | |
64 | ||
65 | /** | |
66 | * Adds a new resolver to the list. | |
67 | * | |
68 | * @param resolver The resolver to add. | |
69 | * @param resolvableClasses The classes supported by the resolver. | |
70 | */ | |
71 | private void addResolver(IResolvable resolver, Class<?>[] resolvableClasses) { | |
72 | 85907 | for (Class<?> clazz : resolvableClasses) { |
73 | 45481 | map.put(clazz, resolver); |
74 | } | |
75 | 40426 | } |
76 | ||
77 | Class<?>[] getResolvableClasses() { | |
78 | 0 | Set<Class<?>> keySet = map.keySet(); |
79 | 0 | return keySet.toArray(new Class[keySet.size()]); |
80 | } | |
81 | } |