source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSRule.java@ 8256

Last change on this file since 8256 was 7879, checked in by Don-vip, 9 years ago

optimize / ease debug the chek of assertions in MapCSSTagChecker

  • Property svn:eol-style set to native
File size: 3.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint.mapcss;
3
4import java.util.List;
5
6import org.openstreetmap.josm.gui.mappaint.Environment;
7import org.openstreetmap.josm.tools.Utils;
8
9/**
10 * A MapCSS rule.
11 *
12 * A MapCSS style is simply a list of MapCSS rules. Each rule has a selector
13 * and a declaration. Whenever the selector matches the primitive, the
14 * declaration block is executed for this primitive.
15 */
16public class MapCSSRule implements Comparable<MapCSSRule> {
17
18 public final Selector selector;
19 public final Declaration declaration;
20
21 public static class Declaration {
22 public final List<Instruction> instructions;
23 // declarations in the StyleSource are numbered consecutively
24 public final int idx;
25
26 public Declaration(List<Instruction> instructions, int idx) {
27 this.instructions = instructions;
28 this.idx = idx;
29 }
30
31 /**
32 * <p>Executes the instructions against the environment {@code env}</p>
33 *
34 * @param env the environment
35 */
36 public void execute(Environment env) {
37 for (Instruction i : instructions) {
38 i.execute(env);
39 }
40 }
41
42 @Override
43 public int hashCode() {
44 final int prime = 31;
45 int result = 1;
46 result = prime * result + idx;
47 result = prime * result + ((instructions == null) ? 0 : instructions.hashCode());
48 return result;
49 }
50
51 @Override
52 public boolean equals(Object obj) {
53 if (this == obj)
54 return true;
55 if (obj == null)
56 return false;
57 if (!(obj instanceof Declaration))
58 return false;
59 Declaration other = (Declaration) obj;
60 if (idx != other.idx)
61 return false;
62 if (instructions == null) {
63 if (other.instructions != null)
64 return false;
65 } else if (!instructions.equals(other.instructions))
66 return false;
67 return true;
68 }
69
70 @Override
71 public String toString() {
72 return "Declaration [instructions=" + instructions + ", idx=" + idx + "]";
73 }
74 }
75
76 /**
77 * Constructs a new {@code MapCSSRule}.
78 * @param selector The selector
79 * @param declaration The declaration
80 */
81 public MapCSSRule(Selector selector, Declaration declaration) {
82 this.selector = selector;
83 this.declaration = declaration;
84 }
85
86 /**
87 * <p>Executes the instructions against the environment {@code env}</p>
88 *
89 * @param env the environment
90 */
91 public void execute(Environment env) {
92 declaration.execute(env);
93 }
94
95 @Override
96 public int compareTo(MapCSSRule o) {
97 return declaration.idx - o.declaration.idx;
98 }
99
100 @Override
101 public String toString() {
102 return selector + " {\n " + Utils.join("\n ", declaration.instructions) + "\n}";
103 }
104}
105
Note: See TracBrowser for help on using the repository browser.