1 | /* |
---|
2 | * To change this license header, choose License Headers in Project Properties. |
---|
3 | * To change this template file, choose Tools | Templates |
---|
4 | * and open the template in the editor. |
---|
5 | */ |
---|
6 | package nl.mpi.annot.translate; |
---|
7 | |
---|
8 | import java.util.ArrayList; |
---|
9 | import java.util.HashMap; |
---|
10 | |
---|
11 | /** |
---|
12 | * |
---|
13 | * @author petbei |
---|
14 | */ |
---|
15 | public class TranslationTables { |
---|
16 | private ArrayList<TranslationTable> translationTableArray = null; |
---|
17 | private HashMap<String, Integer> translationTableMap = null; |
---|
18 | |
---|
19 | public TranslationTables() { |
---|
20 | //System.err.println("--- LOG --- created \"TranslationTables\" object"); |
---|
21 | translationTableArray = new ArrayList<TranslationTable>(); |
---|
22 | translationTableMap = new HashMap<String, Integer>(); |
---|
23 | } |
---|
24 | |
---|
25 | TranslationTable addTranslationTable(ResourceLayer fromRL, ResourceLayer toRL, TranslationType transType) throws ATException { |
---|
26 | //--- check validity of parameters |
---|
27 | if (fromRL == null) { |
---|
28 | System.out.println("*** ERROR *** TranslationTables: attempt to add NULL 'from' ResourceLayer name - skipped"); |
---|
29 | throw new ATException("definition of TranslationTable: null 'from' ResourceLayer"); |
---|
30 | } |
---|
31 | if (toRL == null) { |
---|
32 | System.out.println("*** ERROR *** TranslationTables: attempt to add NULL 'to' ResourceLayer name - skipped"); |
---|
33 | throw new ATException("definition of TranslationTable: null 'to' ResourceLayer"); |
---|
34 | } |
---|
35 | if (transType == null) { |
---|
36 | System.out.println("*** ERROR *** TranslationTables: attempt to add NULL or empty translation type object - skipped"); |
---|
37 | throw new ATException("definition of translationTable: null translation type"); |
---|
38 | } |
---|
39 | |
---|
40 | //--- check if Resource/Layer combination is already defined |
---|
41 | if (fromRL.getName().equals(toRL.getName())) { |
---|
42 | System.out.println("*** ERROR *** new TranslationTable: 'from' and 'to' Resource/Layers are the same - ignored"); |
---|
43 | throw new ATException("definition of translationTable: 'from' and 'to' Resource/Layers are the same"); |
---|
44 | } |
---|
45 | String transTabKey = fromRL.getName() + "-" + toRL.getName() + "=" + transType.getId(); |
---|
46 | if (translationTableMap.containsKey(transTabKey)) { |
---|
47 | System.out.println("*** ERROR *** TranslationTable \"" + transTabKey + "\" was already defined - ignored"); |
---|
48 | throw new ATException("definition of translationTable: '" + transTabKey + "' was already defined"); |
---|
49 | } |
---|
50 | //--- not defined; create new one |
---|
51 | TranslationTable nw = new TranslationTable(transTabKey, fromRL, toRL, transType); |
---|
52 | translationTableArray.add(nw); |
---|
53 | translationTableMap.put(transTabKey, translationTableArray.size() - 1); |
---|
54 | return nw; |
---|
55 | } |
---|
56 | |
---|
57 | TranslationTable find(String fromRes, String fromLay, String toRes, String toLay, TranslationType ty) { |
---|
58 | String fromResLayId = fromRes + "/" + fromLay; |
---|
59 | String toResLayId = toRes + "/" + toLay; |
---|
60 | String tableId = fromResLayId + "-" + toResLayId + "=" + ty.getId(); |
---|
61 | if (translationTableMap.containsKey(tableId)) { |
---|
62 | return translationTableArray.get(translationTableMap.get(tableId)); |
---|
63 | } |
---|
64 | else { |
---|
65 | System.err.println("*** ERROR *** failed to find translation table " + tableId); |
---|
66 | return null; |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | void display() { |
---|
71 | System.out.println("========== START list of TranslationTables =========="); |
---|
72 | for (TranslationTable tt: translationTableArray) { |
---|
73 | tt.display(); |
---|
74 | } |
---|
75 | System.out.println("========== END list of TranslationTables ==========\n"); |
---|
76 | } |
---|
77 | } |
---|