Pattern for default string comparison

M

Marco

Hi all,

Sorry for my possibly silly question...

I have an application that reads several (CSV) files from which it
expects certain headers, such as "Person Name", or "Order Number". We
use some common features for recognizing such headers, for instance:
trim() before equals(), configurable case-sensitivity, etc. My first
idea on how to deal with it, is simply to have a static
equals( string1, string2 ) function in some utility class. I wonder if
some convention/design pattern exists for such a case, if certain
names are used as best practice, etc.

Thanks in advance for any advice.
 
D

Daniel Pitts

Marco said:
Hi all,

Sorry for my possibly silly question...

I have an application that reads several (CSV) files from which it
expects certain headers, such as "Person Name", or "Order Number". We
use some common features for recognizing such headers, for instance:
trim() before equals(), configurable case-sensitivity, etc. My first
idea on how to deal with it, is simply to have a static
equals( string1, string2 ) function in some utility class. I wonder if
some convention/design pattern exists for such a case, if certain
names are used as best practice, etc.

Thanks in advance for any advice.
Look at apache commons lang, they have a lot of useful String utility
methods in a single StringUtils class.

They may not support your exact use-case, but you can write a static
trimmedEqual(...) method that does what you want.

If you're use-case is "configurable", then I suggest using non-static
methods on a "Strategy" class.

public class HeaderRecognitionStrategy {
private final boolean trimFirst;
private final boolean caseSensitive;
private final boolean whatever;

public boolean matchesHeaderName(String headerName, String value) {
...
}
}
 
M

Mark Space

Marco said:
Hi all,

Sorry for my possibly silly question...

I have an application that reads several (CSV) files from which it
expects certain headers, such as "Person Name", or "Order Number". We
use some common features for recognizing such headers, for instance:
trim() before equals(), configurable case-sensitivity, etc. My first
idea on how to deal with it, is simply to have a static
equals( string1, string2 ) function in some utility class. I wonder if
some convention/design pattern exists for such a case, if certain
names are used as best practice, etc.

Thanks in advance for any advice.

The standard "utility class" is a Comparator, I think.

public class HeaderComparator implements Comparator<String> {

public int compare( String s1, String s2 ) {
boolean result;
// trim, toLower, etc.
return result;
}
}

The API takes comparators in various places, such as Collections.sort
and Arrays.sort, so they're generally useful, and you can re-use the
class for your own purposes.

Since you only need one, it's normal to implement this class as a
singleton. If you have different ones (trim, don't trim, etc.) you can
just make one each that you need.


public class HeaderComparator implements Comparator<String> {

private enum Trim { NO_TRIM, TRIM }
private enum Case { LEAVE_CASE, UPPER, LOWER }

public final static TRIM_LOWER = HeaderComparator( TRIM, LOWER );
public final static NO_TRIM = HeaderComparator( NO_TRIM, LEAVE_CASE );

private Trim trim;
private Case case;

private HeaderComparator( Trim trim, Case case ) {
this.trim = trim;
this.case = case;
}

public int compare( String s1, String s2 ) {
if( trim == TRIM ) {
s1 = s1.trim();
s2 = s2.trim();
}
if( case == LOWER ) {
s1 = s1.toLowerCase();
s2 = s2.toLowerCase();
}
return s1.compareTo( s2 );
}
}

NOTE: not compiled or tested. Also note that Comparator and Comparable
are two different interfaces.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top