/plugins/JImporter/tags/JIMPORTER_0_4/jimporter/sorting/CaseInsensitiveComparator.java

# · Java · 46 lines · 16 code · 6 blank · 24 comment · 2 complexity · 705fefc130e5dcd45dc761282c1f90f5 MD5 · raw file

  1. /*
  2. * CaseInsensitiveComparitor.java - Compares Import Items without regard to
  3. * character case.
  4. * Copyright (C) 2002 Matthew Flower (MattFlower@yahoo.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. package jimporter.sorting;
  21. import java.util.Comparator;
  22. import jimporter.ImportItem;
  23. /**
  24. * Defines an object ordering class that doesn't care if a string value is upper
  25. * or lower case when sorting. This method is only designed to compare
  26. * <code>ImportItem</code> classes.
  27. */
  28. public class CaseInsensitiveComparator implements Comparator {
  29. public int compare(Object o1, Object o2) {
  30. if (!((o1 instanceof ImportItem) && (o2 instanceof ImportItem))) {
  31. throw new ClassCastException("CaseInsensitiveComparator is only designed to compare ImportItems.");
  32. }
  33. String string1 = ((ImportItem)o1).getImportStatement().toLowerCase();
  34. String string2 = ((ImportItem)o2).getImportStatement().toLowerCase();
  35. return string1.compareTo(string2);
  36. }
  37. public boolean equals(Object o) {
  38. return (o instanceof CaseInsensitiveComparator);
  39. }
  40. }