Clover Coverage Report
Coverage timestamp: Sat Sep 18 2010 04:09:52 UTC
../../../../../img/srcFileCovDistChart5.png 77% of files have more coverage
68   248   32   4
26   141   0.47   17
17     1.88  
1    
 
  KernelModuleReferenceList       Line # 39 68 32 47.7% 0.4774775
 
  (58)
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2010, Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10    * This program is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13    * GNU General Public License for more details.
14    */
15   
16    package org.qedeq.kernel.bo.module;
17   
18    import java.util.ArrayList;
19    import java.util.HashMap;
20    import java.util.List;
21    import java.util.Map;
22   
23    import org.qedeq.base.trace.Trace;
24    import org.qedeq.base.utility.EqualsUtility;
25    import org.qedeq.kernel.bo.ModuleReferenceList;
26    import org.qedeq.kernel.bo.QedeqBo;
27    import org.qedeq.kernel.common.IllegalModuleDataException;
28    import org.qedeq.kernel.common.ModuleContext;
29   
30   
31    /**
32    * Represents a reference list of modules. Every entry has a symbolic name for one referenced QEDEQ
33    * module. This module label acts as a prefix for all references to that module. The module label
34    * must be an unique String.
35    *
36    * @version $Revision: 1.1 $
37    * @author Michael Meyling
38    */
 
39    public class KernelModuleReferenceList implements ModuleReferenceList {
40   
41    /** This class. */
42    private static final Class CLASS = KernelModuleReferenceList.class;
43   
44    /** Contains all labels. */
45    private final List labels;
46   
47    /** Contains all module props. */
48    private final List props;
49   
50    /** Contains all module import contexts. */
51    private final List contexts;
52   
53    /** Maps labels to context. */
54    private final Map label2Context;
55   
56    /**
57    * Constructs an empty list of module references.
58    */
 
59  509 toggle public KernelModuleReferenceList() {
60  509 labels = new ArrayList();
61  509 props = new ArrayList();
62  509 contexts = new ArrayList();
63  509 label2Context = new HashMap();
64    }
65   
66    /**
67    * Add module reference to list.
68    *
69    * @param context Within this context.
70    * @param label Referenced module gets this label. Must not be <code>null</code> or empty.
71    * @param prop Referenced module has this properties. Must not be <code>null</code>.
72    * @throws IllegalModuleDataException The <code>label</code> is empty or <code>null</code>.
73    */
 
74  257 toggle public void add(final ModuleContext context, final String label, final QedeqBo prop)
75    throws IllegalModuleDataException {
76  257 if (label == null || label.length() <= 0) {
77  0 throw new IllegalModuleDataException(10003, "An label was not defined.",
78    new ModuleContext(context), null,
79    null); // LATER mime 20071026: organize exception codes
80    }
81  257 final ModuleContext con = new ModuleContext(context);
82  257 labels.add(label);
83  257 label2Context.put(label, con);
84  257 contexts.add(con);
85  257 Trace.param(CLASS, "add(ModuleContext, String, QedeqBo)", "context", con);
86  257 props.add(prop);
87    }
88   
89    /**
90    * Add module reference to list.
91    *
92    * @param context Within this context.
93    * @param label Referenced module gets this label. Must not be <code>null</code> or empty.
94    * @param prop Referenced module has this properties. Must not be <code>null</code>.
95    * @throws IllegalModuleDataException The <code>id</code> already exists or is
96    * <code>null</code>. Also if <code>label</code> is empty or <code>null</code>.
97    */
 
98  158 toggle public void addLabelUnique(final ModuleContext context, final String label,
99    final QedeqBo prop) throws IllegalModuleDataException {
100  158 if (labels.contains(label)) {
101    // LATER mime 20071026: organize exception codes
102  0 throw new IllegalModuleDataException(10004, "Label \"" + label
103    + "\" defined more than once.", new ModuleContext(context), // use copy constructor!
104    (ModuleContext) label2Context.get(label), null);
105    }
106  158 add(new ModuleContext(context), label, prop);
107    }
108   
 
109  1996 toggle public int size() {
110  1996 return labels.size();
111    }
112   
 
113  257 toggle public String getLabel(final int index) {
114  257 return (String) labels.get(index);
115    }
116   
 
117  63 toggle public QedeqBo getQedeqBo(final int index) {
118  63 return (QedeqBo) props.get(index);
119    }
120   
121    /**
122    * Get {@link QedeqBo} of referenced module.
123    *
124    * @param index Entry index.
125    * @return Module properties for that module.
126    */
 
127  598 toggle public KernelQedeqBo getKernelQedeqBo(final int index) {
128  598 return (KernelQedeqBo) props.get(index);
129    }
130   
 
131  200 toggle public ModuleContext getModuleContext(final int index) {
132  200 return (ModuleContext) contexts.get(index);
133    }
134   
 
135  1737 toggle public QedeqBo getQedeqBo(final String label) {
136  1737 final int index = labels.indexOf(label);
137  1737 if (index < 0) {
138  243 return null;
139    }
140  1494 return (QedeqBo) props.get(index);
141    }
142   
143    /**
144    * Get KernelQedeqBo of referenced module via label. Might be <code>null</code>.
145    *
146    * @param label Label for referenced module or <code>null</code> if not found.
147    * @return QEQDEQ BO.
148    */
 
149  141 toggle public KernelQedeqBo getKernelQedeqBo(final String label) {
150  141 final int index = labels.indexOf(label);
151  141 if (index < 0) {
152  0 return null;
153    }
154  141 return (KernelQedeqBo) props.get(index);
155    }
156   
157    /**
158    * Is the given QEDEQ BO already in this list?
159    *
160    * @param bo QEDEQ BO.
161    * @return Already in list?
162    */
 
163  278 toggle public boolean contains(final KernelQedeqBo bo) {
164  278 return props.contains(bo);
165    }
166   
167    /**
168    * Delete a given QEDEQ BO already from list.
169    *
170    * @param bo QEDEQ BO.
171    */
 
172  0 toggle public void remove(final KernelQedeqBo bo) {
173  0 int index;
174  0 while (0 <= (index = props.indexOf(bo))) {
175  0 final String label = (String) labels.get(index);
176  0 label2Context.remove(label);
177  0 props.remove(index);
178  0 labels.remove(index);
179  0 contexts.remove(index);
180    }
181    }
182   
 
183  0 toggle public boolean equals(final Object obj) {
184  0 if (!(obj instanceof KernelModuleReferenceList)) {
185  0 return false;
186    }
187  0 final ModuleReferenceList otherList = (ModuleReferenceList) obj;
188  0 if (size() != otherList.size()) {
189  0 return false;
190    }
191  0 for (int i = 0; i < size(); i++) {
192  0 if (!EqualsUtility.equals(getLabel(i), otherList.getLabel(i))
193    || !EqualsUtility.equals(getQedeqBo(i),
194    otherList.getQedeqBo(i))) {
195  0 return false;
196    }
197    }
198  0 return true;
199    }
200   
201    /**
202    * Empty reference list.
203    */
 
204  1238 toggle public void clear() {
205  1238 labels.clear();
206  1238 props.clear();
207  1238 contexts.clear();
208  1238 label2Context.clear();
209    }
210   
211    /**
212    * Copy all list entry references of <code>list</code> to this instance.
213    *
214    * @param list Copy from here.
215    */
 
216  126 toggle public void set(final KernelModuleReferenceList list) {
217  126 clear();
218  126 this.labels.addAll(list.labels);
219  126 this.props.addAll(list.props);
220  126 this.contexts.addAll(list.contexts);
221  126 this.label2Context.putAll(list.label2Context);
222    }
223   
 
224  0 toggle public int hashCode() {
225  0 int hash = 0;
226  0 for (int i = 0; i < size(); i++) {
227  0 hash = hash ^ (i + 1);
228  0 if (getLabel(i) != null) {
229  0 hash = hash ^ getLabel(i).hashCode();
230  0 hash = hash ^ getQedeqBo(i).hashCode();
231    }
232    }
233  0 return hash;
234    }
235   
 
236  0 toggle public String toString() {
237  0 final StringBuffer buffer = new StringBuffer("module reference list:\n");
238  0 for (int i = 0; i < size(); i++) {
239  0 if (i != 0) {
240  0 buffer.append("\n");
241    }
242  0 buffer.append((i + 1) + ":\t");
243  0 buffer.append(getLabel(i)).append(": ").append(getQedeqBo(i)).append("\n");
244    }
245  0 return buffer.toString();
246    }
247   
248    }