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