View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: ConverterRegistry.java
13   * Created: 2009-08-04
14   * Author: tmilos
15   * $Id$
16   */
17  
18  package pl.edu.agh.cast.data.converter;
19  
20  import java.util.Collections;
21  import java.util.LinkedList;
22  import java.util.List;
23  
24  import pl.edu.agh.cast.Activator;
25  import pl.edu.agh.cast.data.model.Type;
26  
27  /**
28   * A locator and registry of data set converters.
29   *
30   * @author AGH CAST Team
31   */
32  public final class ConverterRegistry {
33  
34  	private List<ConverterReference> converterReferences;
35  
36  	// BEGIN Singleton Handling
37  
38  	/**
39  	 * Data set converter registry single instance holder.
40  	 *
41  	 * @author AGH CAST Team
42  	 */
43  	private static final class ConverterRegistryHolder {
44  		private static final ConverterRegistry INSTANCE = createInstance();
45  	}
46  
47  	private static ConverterRegistry createInstance() {
48  		return new ConverterRegistry();
49  	}
50  
51  	public static ConverterRegistry getInstance() {
52  		return ConverterRegistryHolder.INSTANCE;
53  	}
54  
55  	// END Singleton Handling
56  
57  	private ConverterRegistry() {
58  		converterReferences = Activator.getDataSetConverterReferences();
59  	}
60  
61  	/**
62  	 * Returns a list of converter references which match the given list of data set types.
63  	 *
64  	 * @param types
65  	 *            list of data set types
66  	 * @return list of matching converter references
67  	 */
68  	public List<ConverterReference> getMatchingConverters(List<Type> types) {
69  		List<ConverterReference> matchingReferences = new LinkedList<ConverterReference>();
70  
71  		for (ConverterReference converterReference : converterReferences) {
72  			if (isMatching(converterReference.getSpecification(), types)) {
73  				matchingReferences.add(converterReference);
74  			}
75  		}
76  
77  		return matchingReferences;
78  	}
79  
80  	/**
81  	 * Returns a list of converter references which match the given data set type.
82  	 *
83  	 * @param type
84  	 *            the data set type
85  	 * @return list of matching converter references
86  	 */
87  	public List<ConverterReference> getMatchingConverters(Type type) {
88  		return getMatchingConverters(Collections.singletonList(type));
89  	}
90  
91  	/**
92  	 * Checks if the given converter specification is matching the given list of data set types.
93  	 *
94  	 * @param spec
95  	 *            converter specification to check
96  	 * @param types
97  	 *            list of data set types
98  	 * @return <code>true</code> if given converter specification matches the given list of data set types
99  	 */
100 	public static boolean isMatching(ConverterSpecification spec, List<Type> types) {
101 		return types.size() == 1 && spec.getInputEntries().size() == 1
102 		        && types.get(0).equals(spec.getInputEntries().get(0).getType());
103 	}
104 
105 }