View Javadoc

1   package ca.uhn.hl7v2.testpanel.util;
2   
3   import java.util.Comparator;
4   
5   import org.apache.commons.lang.StringUtils;
6   
7   import ca.uhn.hl7v2.HL7Exception;
8   import ca.uhn.hl7v2.model.Message;
9   import ca.uhn.hl7v2.parser.PipeParser;
10  import ca.uhn.hl7v2.util.Terser;
11  
12  public class Hl7V2MessageStringComparatorByControlId implements Comparator<String> {
13  	private static final PipeParser ourParser = PipeParser.getInstanceWithNoValidation();
14  
15  	@Override
16  	public int compare(String theS1, String theS2) {
17  
18  		if (theS1 == null && theS2 == null) {
19  			return 0;
20  		}
21  		if (theS1 == null) {
22  			return 1;
23  		}
24  		if (theS2 == null) {
25  			return -1;
26  		}
27  
28  		Message o1;
29  		Message o2;
30  		try {
31  			o1 = ourParser.parse(theS1);
32  			o2 = ourParser.parse(theS2);
33  		} catch (HL7Exception e) {
34  			throw new Error(e);
35  		}
36  
37  		try {
38  			String c1 = new Terser(o1).get("/MSH-10");
39  			String c2 = new Terser(o2).get("/MSH-10");
40  
41  			c1 = StringUtils.defaultString(c1);
42  			c2 = StringUtils.defaultString(c2);
43  
44  			return c1.compareToIgnoreCase(c2);
45  
46  		} catch (HL7Exception e) {
47  			throw new Error(e);
48  		}
49  
50  	}
51  
52  }