I might have a use for this now...
Sunday, May 4, 2025
Sunday, March 29, 2020
Watching Covid-19 cases
This last week I had started watching the count of cases for my county, Middlesex in Massachusetts, hoping to see things leveling off soon. (I'm looking at this site: Coronavirus COVID-19 (2019-nCoV). It seems legit.) Unfortunately the numbers are still climbing. When I started they were below 200. Today I see the number of active cases is 837. Yesterday the active count was in the 500's. According to the site there are no recoveries yet, which is I assume because all the cases are less than a couple weeks old and it takes at least that long for the disease to play out.
While the schools and almost all businesses have closed their locations, I still see kids playing at the playgrounds when I walk my dog. Just a few, but I wish people wouldn't do that. Still, better than New York City, which looks like it's going to be disaster.
Anyway, I short update. I'm still working at my job, thanks to telecommuting. I'm going to try to keep updating during this special period.
While the schools and almost all businesses have closed their locations, I still see kids playing at the playgrounds when I walk my dog. Just a few, but I wish people wouldn't do that. Still, better than New York City, which looks like it's going to be disaster.
Anyway, I short update. I'm still working at my job, thanks to telecommuting. I'm going to try to keep updating during this special period.
Friday, July 14, 2017
Nasty problem using String.replaceAll and String.replaceFirst
A customer ran into a problem using my code yesterday. My code does a bunch of SQL stuff and years ago I started using tokens and the replace methods to make things more readable.
Instead of:
String query = "select value from synonymtable where domainid='" + domainid.toUpperCase() + "' and internalvalue='" + internalvalue.toUpperCase() + "' and defaults = 1" ;
I had:
String query = "select value from synonymtable where domainid='<domainid>' and internalvalue='<internalvalue>' and defaults = 1"
.replaceAll("<domainid>", domainid.toUpperCase())
.replaceAll("<internalvalue>", internalvalue.toUpperCase()) ;
Using the replacement methods made the code much clearer. And I could create a template string.
static final String queryTemplate = "select value from synonymtable where domainid='<domainid>' and internalvalue='<internalvalue>' and defaults = 1" ;
The problem comes in because these replace methods take regular expressions, but I thought I'm fine since I'm always just doing straight replacements. Unfortunately the client had added a column with a $ to their database. Something like COST$. In a regular expression the dollar sign ($) means something, so this breaks a bunch of code.
Caused by: Illegal group reference
at java.util.regex.Matcher(Matcher.java: 819)
at java.util.regex.Matcher(Matcher.java: 917)
at java.lang.String(String.java: 1718)
And I have 400 over places where I'm using the replace methods. Arrgh!
I've created a utility method to work around this.
public static String replaceAll(String template, String ... tokenValuePairs)
{
if (tokenValuePairs.length % 2 != 0)
{
throw new RuntimeException("Method expects pairs of tokens and replacement values") ;
}
String ret = template ;
for (int x = 0; x < tokenValuePairs.length; x = x + 2)
{
ret = ret.replaceAll(tokenValuePairs[x], Matcher.quoteReplacement(tokenValuePairs[x+1])) ;
}
return ret ;
}
The Matcher#quoteReplacement formats replacement string so I don't need to escape any characters.
Now the code will look like:
String query = StringUtility.replaceAll("select value from synonymtable where domainid='' and internalvalue='' and defaults = 1",
"", domainid.toUpperCase(),
"", internalvalue.toUpperCase()) ;
Unfortunately I need to fix probably hundreds of places where the replace methods are used.
Instead of:
String query = "select value from synonymtable where domainid='" + domainid.toUpperCase() + "' and internalvalue='" + internalvalue.toUpperCase() + "' and defaults = 1" ;
I had:
String query = "select value from synonymtable where domainid='<domainid>' and internalvalue='<internalvalue>' and defaults = 1"
.replaceAll("<domainid>", domainid.toUpperCase())
.replaceAll("<internalvalue>", internalvalue.toUpperCase()) ;
Using the replacement methods made the code much clearer. And I could create a template string.
static final String queryTemplate = "select value from synonymtable where domainid='<domainid>' and internalvalue='<internalvalue>' and defaults = 1" ;
The problem comes in because these replace methods take regular expressions, but I thought I'm fine since I'm always just doing straight replacements. Unfortunately the client had added a column with a $ to their database. Something like COST$. In a regular expression the dollar sign ($) means something, so this breaks a bunch of code.
Caused by: Illegal group reference
at java.util.regex.Matcher(Matcher.java: 819)
at java.util.regex.Matcher(Matcher.java: 917)
at java.lang.String(String.java: 1718)
And I have 400 over places where I'm using the replace methods. Arrgh!
I've created a utility method to work around this.
public static String replaceAll(String template, String ... tokenValuePairs)
{
if (tokenValuePairs.length % 2 != 0)
{
throw new RuntimeException("Method expects pairs of tokens and replacement values") ;
}
String ret = template ;
for (int x = 0; x < tokenValuePairs.length; x = x + 2)
{
ret = ret.replaceAll(tokenValuePairs[x], Matcher.quoteReplacement(tokenValuePairs[x+1])) ;
}
return ret ;
}
The Matcher#quoteReplacement formats replacement string so I don't need to escape any characters.
Now the code will look like:
String query = StringUtility.replaceAll("select value from synonymtable where domainid='
"
"
Unfortunately I need to fix probably hundreds of places where the replace methods are used.
I will beat the crap out of..
anyone who says it's okay to punch someone based on their free speech.
Wednesday, December 2, 2015
Sunday, November 15, 2015
Beer Review: Lava - Ölvisholt Brugghús
Color like Guinness, complex & creamy not crisp. Slight smoked aroma. Imperial Stout - 9.4% alcohol by volume. After a pint, I'm ready for a nap. But yummy. 8 out of 10.
Beer Advocate link.
Beer Advocate link.
Tuesday, May 19, 2015
Subscribe to:
Posts (Atom)