CSSSelector.java 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package de.tu_darmstadt.informatik.tk.scopviz.ui.css;
  2. import java.util.HashSet;
  3. import java.util.Iterator;
  4. /**
  5. * Stores a single CSSSelector consisting of a type and set of classes. Stores
  6. * its value. The value is calculated by multiplying the amount of classes by
  7. * Two and adding one if the selector has a type.
  8. *
  9. * @author Matthias Wilhelm
  10. */
  11. class CSSSelector {
  12. /**
  13. * the stored CSS type
  14. */
  15. String type;
  16. /**
  17. * the stored CSS classes
  18. */
  19. HashSet<String> classes;
  20. /**
  21. * the stored selector value.<br/>
  22. * The value is calculated by multiplying the amount of classes by Two and
  23. * adding one if the selector has a type
  24. */
  25. int value;
  26. /**
  27. * Creates a new CSSSelector. Calculates its value.<br/>
  28. * The value is calculated by multiplying the amount of classes by Two and
  29. * adding one if the selector has a type
  30. *
  31. * @param type
  32. * CSS type
  33. * @param classes
  34. * a Set CSS classes
  35. */
  36. CSSSelector(String type, HashSet<String> classes) {
  37. if (type != null && type.trim().length() > 0)
  38. this.type = type;
  39. this.classes = classes;
  40. value = (type != null ? 1 : 0) + classes.size() << 1;
  41. }
  42. /**
  43. * Compares the suspect to its conditions.
  44. *
  45. * @param suspect
  46. * the CSSable to check
  47. * @return true if the CSSable contains all classes of the selector and the
  48. * type matches.
  49. */
  50. boolean ConditionsMetBy(CSSable suspect) {
  51. if (type != null && !type.equals(suspect.getType()))
  52. return false;
  53. Iterator<String> i = classes.iterator();
  54. HashSet<String> sC = suspect.getClasses();
  55. while (i.hasNext()) {
  56. if (sC == null || !sC.contains(i.next()))
  57. return false;
  58. }
  59. return true;
  60. }
  61. /**
  62. * The value is calculated by multiplying the amount of classes by Two and
  63. * adding one if the selector has a type
  64. *
  65. * @return the value of this CSS selector
  66. */
  67. int getValue() {
  68. return value;
  69. }
  70. @Override
  71. public String toString() {
  72. String ret = "";
  73. for (String c : classes) {
  74. ret = ret.concat(".").concat(c);
  75. }
  76. if (type == null)
  77. return ret;
  78. return type.concat(ret);
  79. }
  80. }