Omaha #3693 Optimize usage of Patterns in com.raytheon.edex.plugin.obs plugin

Change-Id: I1dc26f81d0a6f39b86eb1e832c7204d8b6ce3348

Former-commit-id: 475fa37d70d51e5e9538141730fbead4a6b5d88e
This commit is contained in:
Mark Peters 2014-10-02 15:36:22 -05:00
parent cddda47923
commit 955f21a85c
3 changed files with 71 additions and 62 deletions

View file

@ -84,6 +84,7 @@ import com.vividsolutions.jts.geom.impl.CoordinateArraySequence;
* May 12, 2014 DR 17151 D. Friedman Fix 6hr min/max temp decoding.
* May 14, 2014 2536 bclement moved WMO Header to common, removed TimeTools usage
* Jul 23, 2014 3410 bclement location changed to floats
* Oct 02, 2014 3693 mapeters Added Pattern constants.
*
* </pre>
*
@ -193,6 +194,14 @@ public class MetarDecoder extends AbstractDecoder {
public static final Pattern SUNSHINE = Pattern
.compile("(\\b)98(\\d{3}|///)");
private static final Pattern AUTO = Pattern.compile(" AUTO");
private static final Pattern D4_NDV = Pattern.compile("\\d{4}NDV");
private static final Pattern D4_NSEW = Pattern.compile("\\d{4}[NSEW]");
private static final Pattern D4 = Pattern.compile("\\d{4}");
private boolean useMockInfo = false;
private ObStation mockInfo = null;
@ -238,7 +247,6 @@ public class MetarDecoder extends AbstractDecoder {
while (sep.hasNext()) {
byte[] messageData = sep.next();
Pattern thePattern;
String message = new String(messageData);
StringBuilder sbm = new StringBuilder(message);
@ -367,8 +375,7 @@ public class MetarDecoder extends AbstractDecoder {
obsMsg.delete(0, cutPos);
// Gets the correction notifier
thePattern = Pattern.compile(" AUTO");
matcher = thePattern.matcher(obsMsg);
matcher = AUTO.matcher(obsMsg);
if (matcher.find()) {
obsMsg.delete(0, matcher.end());
}
@ -457,8 +464,7 @@ public class MetarDecoder extends AbstractDecoder {
}
}
if (!foundVis) {
thePattern = Pattern.compile("\\d{4}NDV");
matcher = thePattern.matcher(obsMsg);
matcher = D4_NDV.matcher(obsMsg);
if (matcher.find()) {
int start = matcher.start();
int end = matcher.end();
@ -472,8 +478,7 @@ public class MetarDecoder extends AbstractDecoder {
boolean sectorFound = true;
while (sectorFound) {
thePattern = Pattern.compile("\\d{4}[NSEW]");
matcher = thePattern.matcher(obsMsg);
matcher = D4_NSEW.matcher(obsMsg);
if (matcher.find()) {
int start = matcher.start();
int end = matcher.end();
@ -488,8 +493,7 @@ public class MetarDecoder extends AbstractDecoder {
}
}
thePattern = Pattern.compile("\\d{4}");
matcher = thePattern.matcher(obsMsg);
matcher = D4.matcher(obsMsg);
if (matcher.find()) {
int start = matcher.start();
if (start > 0) {

View file

@ -58,6 +58,7 @@ import com.raytheon.uf.common.wmo.WMOHeader;
* 20080418 1093 jkorman Added filter for Alaskan &quot;Airways&quot;
* observations.
* May 14, 2014 2536 bclement moved WMO Header to common, removed unused HEADERREGEX
* Oct 02, 2014 3693 mapeters Changed pattern String constants to Pattern constants.
* </pre>
*
* @author bphillip
@ -75,15 +76,17 @@ public class MetarSeparator extends AbstractRecordSeparator {
private final Log theLogger = LogFactory.getLog(getClass());
/** Regex used for separating multi-record files */
private static final String ICAODATEPAIR = "\\p{Alnum}{4} (\\d{6}Z |(RMK )?NIL)";
private static final Pattern ICAODATEPAIR = Pattern
.compile("\\p{Alnum}{4} (\\d{6}Z |(RMK )?NIL)");
/** Regex used for determining metar type */
private static final String METARTYPE = "METAR|SPECI";
private static final Pattern METARTYPE = Pattern.compile("METAR|SPECI");
private static final String AIRWAYS = "[A-Z][A-Z,0-9]{3} (SP|SA) \\d{4} AWOS";
private static final Pattern AIRWAYS = Pattern
.compile("[A-Z][A-Z,0-9]{3} (SP|SA) \\d{4} AWOS");
/** Regex used to search for NIL messages */
private static final String NILREGEX = "NIL";
private static final Pattern NILREGEX = Pattern.compile("NIL");
/** The WMO header */
private WMOHeader header;
@ -179,8 +182,7 @@ public class MetarSeparator extends AbstractRecordSeparator {
// header = matcher.group();
// }
// Determines the type
Pattern pattern = Pattern.compile(METARTYPE);
Matcher matcher = pattern.matcher(message);
Matcher matcher = METARTYPE.matcher(message);
if (matcher.find()) {
type = matcher.group();
@ -189,8 +191,7 @@ public class MetarSeparator extends AbstractRecordSeparator {
}
message = message.replaceAll(type, "");
pattern = Pattern.compile(ICAODATEPAIR);
matcher = pattern.matcher(message);
matcher = ICAODATEPAIR.matcher(message);
List<Integer> bodyIndex = new ArrayList<Integer>();
Map<String, String> bodyMap = new HashMap<String, String>();
@ -218,7 +219,7 @@ public class MetarSeparator extends AbstractRecordSeparator {
for (int i = 0; i < bodyIndex.size() - 1; i += 2) {
String observation = message.substring(bodyIndex.get(i),
bodyIndex.get(i + 1));
matcher = pattern.matcher(observation);
matcher = ICAODATEPAIR.matcher(observation);
if (matcher.find()) {
// Get the key i.e. {ICAO|Date}
String obsKey = matcher.group();
@ -240,15 +241,13 @@ public class MetarSeparator extends AbstractRecordSeparator {
// Check for old style AIRWAYS data from Alaskan stations. This
// data will be at the end of valid METAR/SPECI data so just
// remove it.
pattern = Pattern.compile(AIRWAYS);
matcher = pattern.matcher(observation);
matcher = AIRWAYS.matcher(observation);
if (matcher.find()) {
observation = observation.substring(0, matcher.start());
}
// Check for NIL observations and, if found, throw out
pattern = Pattern.compile(NILREGEX);
matcher = pattern.matcher(observation);
matcher = NILREGEX.matcher(observation);
if (!matcher.find()) {
String record = header.getWmoHeader() + "\n" + type + " " + observation;
records.add(record);

View file

@ -21,6 +21,7 @@ package com.raytheon.edex.plugin.obs.metar.util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.measure.converter.UnitConverter;
import javax.measure.unit.NonSI;
import javax.measure.unit.SI;
@ -35,6 +36,7 @@ import javax.measure.unit.SI;
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* Jun 24, 2009 brockwoo Initial creation
* Oct 02, 2014 3693 mapeters Made Patterns and UnitConverters constants.
*
* </pre>
*
@ -47,30 +49,34 @@ public class VisibilityParser {
private String visibility;
private double prevail_vsbySM;
private String vsby_Dir;
private Pattern digitsOnly;
private Pattern twoDigitDir;
private Pattern oneDigitDir;
private Pattern parseNumber;
private UnitConverter meters2Miles;
private UnitConverter km2Miles;
private UnitConverter miles2Meters;
private UnitConverter miles2Km;
private static final Pattern DIGITS_ONLY = Pattern.compile("^\\d+$");
private static final Pattern TWO_DIGIT_DIR = Pattern.compile("^[NS][EW]$");
private static final Pattern ONE_DIGIT_DIR = Pattern.compile("^[NSEW]$");
private static final Pattern PARSE_NUMBER = Pattern.compile("^(\\d+)\\D*$");
private static final UnitConverter METERS_TO_MILES = SI.METER
.getConverterTo(NonSI.MILE);
private static final UnitConverter KM_TO_MILES = SI.KILOMETER
.getConverterTo(NonSI.MILE);
private static final UnitConverter MILES_TO_METERS = NonSI.MILE
.getConverterTo(SI.METER);
private static final UnitConverter MILES_TO_KM = NonSI.MILE
.getConverterTo(SI.KILOMETER);
// MAX value taken from AWIPSI code
private static final double MAX = 2147483647;
public VisibilityParser() {
this.meters2Miles = SI.METER.getConverterTo(NonSI.MILE);
this.km2Miles = SI.KILOMETER.getConverterTo(NonSI.MILE);
this.miles2Meters = NonSI.MILE.getConverterTo(SI.METER);
this.miles2Km = NonSI.MILE.getConverterTo(SI.KILOMETER);
this.visibility = new String();
this.prevail_vsbySM = 0.0;
this.vsby_Dir = new String();
this.digitsOnly = Pattern.compile("^\\d+$");
this.twoDigitDir = Pattern.compile("^[NS][EW]$");
this.oneDigitDir = Pattern.compile("^[NSEW]$");
this.parseNumber = Pattern.compile("^(\\d+)\\D*$");
}
public String getVisibility() {
@ -82,11 +88,11 @@ public class VisibilityParser {
}
public double getPrevail_vsbyKM() {
return this.miles2Km.convert(prevail_vsbySM);
return MILES_TO_KM.convert(prevail_vsbySM);
}
public double getPrevail_vsbyM() {
return this.miles2Meters.convert(prevail_vsbySM);
return MILES_TO_METERS.convert(prevail_vsbySM);
}
public String getVsby_Dir() {
@ -116,10 +122,10 @@ public class VisibilityParser {
/* CHECK FOR VISIBILITY MEASURED IN KILOMETERS */
/***********************************************/
if((offset = vis[0].indexOf("KM")) != -1) {
Matcher km = digitsOnly.matcher(vis[0].substring(0, offset));
Matcher km = DIGITS_ONLY.matcher(vis[0].substring(0, offset));
if(km.matches()) {
this.prevail_vsbySM =
this.km2Miles.convert(prevailVSBY(vis[0]));
KM_TO_MILES.convert(prevailVSBY(vis[0]));
this.visibility = vis[0];
return true;
}
@ -133,9 +139,9 @@ public class VisibilityParser {
/***********************************/
else if( (charOffset = vis[0].indexOf('/')) != -1 &&
(offset = vis[0].indexOf("SM")) != -1 ) {
Matcher num = digitsOnly.matcher(vis[0].substring(0, charOffset));
Matcher num = DIGITS_ONLY.matcher(vis[0].substring(0, charOffset));
Matcher den =
digitsOnly.matcher(vis[0].substring(charOffset+1, offset));
DIGITS_ONLY.matcher(vis[0].substring(charOffset+1, offset));
if(num.matches() && den.matches()) {
this.prevail_vsbySM = prevailVSBY(vis[0]);
this.visibility = vis[0];
@ -149,7 +155,7 @@ public class VisibilityParser {
/* IN WHOLE STATUTE MILES */
/***********************************/
else if((offset = vis[0].indexOf("SM")) != -1) {
Matcher sm = digitsOnly.matcher(vis[0].substring(0, offset));
Matcher sm = DIGITS_ONLY.matcher(vis[0].substring(0, offset));
if(sm.matches()) {
prevail_vsbySM = prevailVSBY(vis[0]);
this.visibility = vis[0];
@ -165,7 +171,7 @@ public class VisibilityParser {
/* MILES */
/***********************************/
else if( vis[0].length() < 4 ) {
Matcher wholeNumber = digitsOnly.matcher(vis[0]);
Matcher wholeNumber = DIGITS_ONLY.matcher(vis[0]);
if(!wholeNumber.matches()) {
return false;
}
@ -177,9 +183,9 @@ public class VisibilityParser {
if( (charOffset = vis[1].indexOf('/')) != -1 &&
(offset = vis[1].indexOf("SM")) != -1 ) {
Matcher num =
digitsOnly.matcher(vis[1].substring(0, charOffset));
DIGITS_ONLY.matcher(vis[1].substring(0, charOffset));
Matcher den =
digitsOnly.matcher(vis[1].substring(charOffset+1, offset));
DIGITS_ONLY.matcher(vis[1].substring(charOffset+1, offset));
if( num.matches() && den.matches() )
{
prevail_vsbySM = prevailVSBY(vis[1]);
@ -200,20 +206,20 @@ public class VisibilityParser {
/* IN METERS WITH OR WITHOUT DI- */
/* RECTION OF OBSERVATION */
/***********************************/
Matcher firstFourChar = digitsOnly.matcher(vis[0].substring(0, 4));
Matcher firstFourChar = DIGITS_ONLY.matcher(vis[0].substring(0, 4));
if(!firstFourChar.matches()) {
return false;
}
if(vis[0].length() == 6) {
String dir = vis[0].substring(4, 6);
if(this.twoDigitDir.matcher(dir).matches()) {
if (TWO_DIGIT_DIR.matcher(dir).matches()) {
vsby_Dir = dir;
}
}
else if(vis[0].length() == 5) {
String dir = vis[0].substring(4, 5);
if(this.oneDigitDir.matcher(dir).matches()) {
if (ONE_DIGIT_DIR.matcher(dir).matches()) {
vsby_Dir = dir;
}
}
@ -223,7 +229,7 @@ public class VisibilityParser {
if( visValue >= 50f &&
visValue <= 500f &&
(visValue % 50) == 0 ) {
this.prevail_vsbySM = this.meters2Miles.convert(visValue);
this.prevail_vsbySM = METERS_TO_MILES.convert(visValue);
this.visibility = vis[0];
return true;
}
@ -231,7 +237,7 @@ public class VisibilityParser {
visValue <= 3000.0f &&
(visValue % 100) == 0 )
{
prevail_vsbySM = this.meters2Miles.convert(visValue);
prevail_vsbySM = METERS_TO_MILES.convert(visValue);
this.visibility = vis[0];
return true;
}
@ -239,7 +245,7 @@ public class VisibilityParser {
visValue <= 3000.0f &&
(visValue % 100) == 0 )
{
prevail_vsbySM = this.meters2Miles.convert(visValue);
prevail_vsbySM = METERS_TO_MILES.convert(visValue);
this.visibility = vis[0];
return true;
}
@ -247,7 +253,7 @@ public class VisibilityParser {
visValue <= 5000.0f &&
(visValue % 500) == 0 )
{
this.prevail_vsbySM = this.meters2Miles.convert(visValue);
this.prevail_vsbySM = METERS_TO_MILES.convert(visValue);
this.visibility = vis[0];
return true;
}
@ -256,7 +262,7 @@ public class VisibilityParser {
(visValue % 500) == 0 ||
visValue == 9999 )
{
this.prevail_vsbySM = this.meters2Miles.convert(visValue);
this.prevail_vsbySM = METERS_TO_MILES.convert(visValue);
this.visibility = vis[0];
return true;
}
@ -272,7 +278,7 @@ public class VisibilityParser {
if(vis.length() < 0) {
return MAX;
}
Matcher number = this.parseNumber.matcher(vis);
Matcher number = PARSE_NUMBER.matcher(vis);
if(number.matches()) {
return Double.valueOf(number.group(1));
}