Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

guava-libraries - Is the Ordering class thread safe?

The guava-libraries have a class Ordering. I'm wondering if it's thread safe.

For example, can it be used as a static variable?

public static Ordering<String> BY_LENGTH_ORDERING = new Ordering<String>() {
   public int compare(String left, String right) {
      return Ints.compare(left.length(), right.length());
   }
};
like image 689
Soundlink Avatar asked Aug 26 '10 16:08

Soundlink


2 Answers

It's as thread-safe as your compare method.

Default implementation of Ordering does not have any instance data, so the only thing that matters is how you define your compare method.

like image 183
Alexander Pogrebnyak Avatar answered Sep 18 '22 22:09

Alexander Pogrebnyak


Yes, Ordering objects are all immutable unless you do something to make them mutable, such as extending Ordering and adding mutable fields, or providing a mutable Comparator in the from(Comparator) method or a mutable Function in onResultOf(Function).

But typically, you'd really have to go out of your way to make one that isn't thread safe.

like image 28
ColinD Avatar answered Sep 17 '22 22:09

ColinD