Δευτέρα, 29 Σεπτεμβρίου 2008

Tips and tricks

  1. Tricks on immutability
a. Always make a copy of mutable objects inside your methods, e.g.

void myMethod(Date date) {
Date myCopy = new Date(date.getTime());
}
b. How do I create an immutable Java Bean or POJO? Make it read-only, and pass any parameters during initialisation (in the constructor):

public class MyClass {
private int param1;
private String param2;
private Date date;

public MyClass (int param1, String param2, Date d) {
this.param1 = param1;
this.param2 = param2;
this.date = new Date(d.getTime());
}

public int getParam1() {
return param1;
}

public String getParam2() {
return param2;
}

public Date getDate() {
return new Date(date.getTime());
}
}

c. Immutable objects and Hibernate
  • set the access type to “field” in order to use reflection to set the fields instead of property access, which uses setters.
  • set mutable to false (Hibernate will perform small optimizations, too).

Κυριακή, 7 Σεπτεμβρίου 2008

Compare and sort Greek Strings

Inspired from the article Compare and sort Strings, I hereby present an example of Collators and Comparators with Strings that contain Greek letters.

import java.text.Collator;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

public class GreekCollator {
private static final Locale GREEK_LOCALE = new Locale("el", "GR");
private static final int NO_COLLATOR = -1;

private enum CollatorEnum {
NoCollator(NO_COLLATOR),
Primary(Collator.PRIMARY),
Secondary(Collator.SECONDARY),
Tertiary(Collator.TERTIARY),
Identical(Collator.IDENTICAL);

private int collatorStrength;
CollatorEnum(int collatorStrength) {
this.collatorStrength = collatorStrength;
}
}

/**
* @param args
*/
public static void main(String[] args) {
List<String> words = Arrays.asList("άλλος", "αλάνι", "βασικός", "ελεύθερος", "Ελεύθερος", "Άλλος", "Αλάνι");

System.out.println(words + " - Original Data\n");
sort(words, CollatorEnum.NoCollator); // don't use a collator
sort(words, CollatorEnum.Primary);
sort(words, CollatorEnum.Secondary);
sort(words, CollatorEnum.Tertiary);
sort(words, CollatorEnum.Identical);

System.out.println();
compare("αβγ", "ΆΒΓ", CollatorEnum.NoCollator);
compare("αβγ", "ΆΒΓ", CollatorEnum.Primary);
compare("αβγ", "ΆΒΓ", CollatorEnum.Secondary);
compare("αβγ", "ΆΒΓ", CollatorEnum.Tertiary);
compare("αβγ", "ΆΒΓ", CollatorEnum.Identical);

System.out.println();
compare("αβγ", "ΑΒΓ", CollatorEnum.NoCollator);
compare("αβγ", "ΑΒΓ", CollatorEnum.Primary);
compare("αβγ", "ΑΒΓ", CollatorEnum.Secondary);
compare("αβγ", "ΑΒΓ", CollatorEnum.Tertiary);
compare("αβγ", "ΆΒΓ", CollatorEnum.Identical);
}

private static void sort(List<String> aWords, CollatorEnum collatorEnum) {
if (collatorEnum.collatorStrength < 0) {
Collections.sort(aWords);
} else {
Collator collator = Collator.getInstance(GREEK_LOCALE);
collator.setStrength(collatorEnum.collatorStrength);
Collections.sort(aWords, collator);
}
System.out.println(aWords.toString() + " " + collatorEnum);
}

private static void compare(String aThis, String aThat, CollatorEnum collatorEnum) {
int comparison = 999;
if (collatorEnum.collatorStrength < 0) {
comparison = aThis.compareTo(aThat);
} else {
Collator collator = Collator.getInstance(GREEK_LOCALE);
collator.setStrength(collatorEnum.collatorStrength);
comparison = collator.compare(aThis, aThat);
}
if (comparison == 0) {
System.out.println("Collator sees them as the same : " + aThis
+ ", " + aThat + " - " + collatorEnum);
} else {
System.out.println("Collator sees them as DIFFERENT : " + aThis
+ ", " + aThat + " - " + collatorEnum);
}
}

}

Here is the output of the program, for your comments.

[άλλος, αλάνι, βασικός, ελεύθερος, Ελεύθερος, Άλλος, Αλάνι] - Original Data

[Άλλος, Αλάνι, Ελεύθερος, άλλος, αλάνι, βασικός, ελεύθερος] NoCollator
[Αλάνι, αλάνι, Άλλος, άλλος, βασικός, Ελεύθερος, ελεύθερος] Primary
[Αλάνι, αλάνι, Άλλος, άλλος, βασικός, Ελεύθερος, ελεύθερος] Secondary
[αλάνι, Αλάνι, άλλος, Άλλος, βασικός, ελεύθερος, Ελεύθερος] Tertiary
[αλάνι, Αλάνι, άλλος, Άλλος, βασικός, ελεύθερος, Ελεύθερος] Identical

Collator sees them as DIFFERENT : αβγ, ΆΒΓ - NoCollator
Collator sees them as the same : αβγ, ΆΒΓ - Primary
Collator sees them as DIFFERENT : αβγ, ΆΒΓ - Secondary
Collator sees them as DIFFERENT : αβγ, ΆΒΓ - Tertiary
Collator sees them as DIFFERENT : αβγ, ΆΒΓ - Identical

Collator sees them as DIFFERENT : αβγ, ΑΒΓ - NoCollator
Collator sees them as the same : αβγ, ΑΒΓ - Primary
Collator sees them as the same : αβγ, ΑΒΓ - Secondary
Collator sees them as DIFFERENT : αβγ, ΑΒΓ - Tertiary
Collator sees them as DIFFERENT : αβγ, ΆΒΓ - Identical

References
  1. Java Practices, Compare and sort Strings.