Commit d6e4783e authored by R.W.Majeed's avatar R.W.Majeed
Browse files

Refactored Observation, use Iterator instead of Enumeration

parent 3bb1d2b4
......@@ -27,8 +27,8 @@ import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
......@@ -369,9 +369,9 @@ public class I2b2Inserter extends AbstractObservationHandler implements Observat
}else if( m == null ){
// insert all modifiers
// loop through modifiers
Enumeration<Modifier> e = o.getModifiers();
while( e.hasMoreElements() ){
Modifier mod = e.nextElement();
Iterator<Modifier> e = o.getModifiers();
while( e.hasNext() ){
Modifier mod = e.next();
insertFact(o, mod, instanceNum);
}
}else{
......
......@@ -21,7 +21,7 @@ package de.sekmi.histream;
*/
import java.util.Enumeration;
import java.util.Iterator;
import de.sekmi.histream.ext.ExternalSourceType;
......@@ -60,13 +60,17 @@ public interface Observation extends ConceptValuePair, ExternalSourceType{
/**
* Whether this observation contains sub-concepts (=modifiers).
*
* @see #getModifier(String)
* @return true if modifiers are present, false otherwise
*/
boolean hasModifiers();
Modifier getModifier(String modifierId);
Enumeration<Modifier> getModifiers();
/**
* Get all modifiers
* @return modifiers
*/
Iterator<Modifier> getModifiers();
/**
* Add a sub concept to this observation.
......@@ -75,8 +79,9 @@ public interface Observation extends ConceptValuePair, ExternalSourceType{
* to the modifier.
*
* @param modifierId concept id for the new modifier
* @param value modifier value
* @return new modifier
* @throws IllegalArgumentException if the given modifierId is already used.
*/
Modifier addModifier(String modifierId)throws IllegalArgumentException;
Modifier addModifier(String modifierId, Value value)throws IllegalArgumentException;
}
......@@ -22,8 +22,8 @@ package de.sekmi.histream.impl;
import java.time.Instant;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import de.sekmi.histream.DateTimeAccuracy;
import de.sekmi.histream.Modifier;
......@@ -33,7 +33,7 @@ import de.sekmi.histream.Value;
/**
* Implementation of {@link Observation}.
* TODO add jaxb annotations to produce/load xml
* TODO DOM view of observation
* @author Raphael
*
*/
......@@ -55,7 +55,7 @@ public class ObservationImpl implements Observation{
/**
* Modifiers
*/
protected Hashtable<String, Modifier> modifiers;
protected Hashtable<String, ModifierImpl> modifiers;
/**
* Array of extensions, managed by the ObservationFactory
......@@ -117,21 +117,21 @@ public class ObservationImpl implements Observation{
}
@Override
public Enumeration<Modifier> getModifiers(){
return modifiers.elements();
}
/**
* Assign a modifier to the concept. This will overwrite any existing modifier
* with the same modifierId.
*
* @param modifier modifier
*/
public void setModifier(Modifier modifier){
// lazy create hashtable only, when a modifier is set
if( modifiers == null )modifiers = new Hashtable<>();
// potentially overwriting existing modifier
modifiers.put(modifier.getConceptId(), modifier);
public Iterator<Modifier> getModifiers(){
final Iterator<ModifierImpl> iter = modifiers.values().iterator();
return new Iterator<Modifier>(){
@Override
public boolean hasNext() {
return iter.hasNext();
}
@Override
public Modifier next() {
return iter.next();
}
};
//return (Iterator<Modifier>)modifiers.values().iterator();
}
@Override
......@@ -170,7 +170,7 @@ public class ObservationImpl implements Observation{
}
@Override
public Modifier addModifier(String modifierId) {
public Modifier addModifier(String modifierId, Value value) {
ModifierImpl m = new ModifierImpl(modifierId);
// lazy allocate modifiers
if( modifiers == null ){
......@@ -180,6 +180,8 @@ public class ObservationImpl implements Observation{
if( modifiers.containsKey(modifierId) )
throw new IllegalArgumentException("Duplicate modifier key");
m.setValue(value);
// add to modifier list
modifiers.put(modifierId, m);
return m;
......
......@@ -351,7 +351,7 @@ public class FlatObservationSupplier extends AbstractObservationParser implement
// parse and add value
Value value = parseValue(record);
try{
fact.addModifier(record.getConcept()).setValue(value);
fact.addModifier(record.getConcept(),value);
}catch( IllegalArgumentException e ){
log.severe("Unable to add modifier in line "+lineNo+": "+record.getConcept());
// TODO FIX line numbers not incremented correctly (line 220000 lines but should be 220006)
......
......@@ -168,10 +168,9 @@ public class XMLObservationSupplier extends XMLObservationParser implements Obse
while( reader.isStartElement() ){
if( !reader.getLocalName().equals("value") )throw new XMLStreamException("Only element 'value' allowed in eav-group",reader.getLocation());
modifier = fact.addModifier(atts.getValue("modifier"));
parseValueAttributes(atts);
modifier.setValue(parseValue(reader.getElementText()));
modifier = fact.addModifier(atts.getValue("modifier"), parseValue(reader.getElementText()));
reader.nextTag();
}
......
......@@ -24,7 +24,7 @@ package de.sekmi.histream.impl;
import java.io.FileInputStream;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.stream.StreamSupport;
import java.math.BigDecimal;
......@@ -123,18 +123,18 @@ public class FileObservationProviderTest {
Assert.assertEquals(Value.Type.Text, o.getValue().getType());
Assert.assertEquals("groupvalue", o.getValue().getValue());
Assert.assertTrue(o.hasModifiers());
Enumeration<Modifier> e = o.getModifiers();
Iterator<? extends Modifier> e = o.getModifiers();
// TODO check modifier count
Modifier m = o.getModifier("T:mod:1");
Assert.assertNotNull(m);
Assert.assertEquals("T:mod:1", m.getConceptId());
Assert.assertEquals(Value.Type.None, m.getValue().getType());
Assert.assertTrue(e.hasMoreElements());
Assert.assertTrue(e.hasNext());
m = o.getModifier("T:mod:2");
Assert.assertNotNull(m);
Assert.assertEquals(Value.Type.Text, m.getValue().getType());
Assert.assertEquals("def456", m.getValue().getValue());
Assert.assertTrue(e.hasMoreElements());
Assert.assertTrue(e.hasNext());
m = o.getModifier("T:mod:3");
Assert.assertNotNull(m);
Assert.assertEquals(Value.Type.Numeric, m.getValue().getType());
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment