View Javadoc
1   package org.vaadin.aceeditor.client;
2   
3   import org.vaadin.aceeditor.client.AceAnnotation.MarkerAnnotation;
4   import org.vaadin.aceeditor.client.AceAnnotation.RowAnnotation;
5   import org.vaadin.aceeditor.client.TransportDoc.TransportMarker;
6   import org.vaadin.aceeditor.client.TransportDoc.TransportMarkerAnnotation;
7   import org.vaadin.aceeditor.client.TransportDoc.TransportRowAnnotation;
8   
9   import java.util.*;
10  import java.util.Map.Entry;
11  
12  public class AceDoc {
13  
14  	private final String text;
15  	
16  	// key: markerId
17  	private final Map<String, AceMarker> markers;
18  
19  	// key: row
20  	private final Set<RowAnnotation> rowAnnotations;
21  
22  	// key: markerId
23  	private final Set<MarkerAnnotation> markerAnnotations;
24  
25  	public AceDoc() {
26  		this("");
27  	}
28  
29  	public AceDoc(String text) {
30          this(text,
31                  Collections.<String, AceMarker> emptyMap(),
32                  Collections.<RowAnnotation>emptySet(),
33                  Collections.<MarkerAnnotation>emptySet());
34  	}
35  	
36  	public AceDoc(String text, Map<String, AceMarker> markers) {
37          this(text, markers,
38                  Collections.<RowAnnotation>emptySet(),
39                  Collections.<MarkerAnnotation>emptySet());
40  	}
41  
42  	public AceDoc(String text, Map<String, AceMarker> markers,
43  			Set<RowAnnotation> rowAnnotations,
44  			Set<MarkerAnnotation> markerAnnotations) {
45          if (text == null) {
46              text = "";
47          }
48  
49  		this.text = text;
50  		this.markers = markers;
51  		this.rowAnnotations = rowAnnotations;
52  		this.markerAnnotations = markerAnnotations;
53  	}
54  	
55  	public String getText() {
56  		return text;
57  	}
58  	
59  	public Map<String, AceMarker> getMarkers() {
60  		return Collections.unmodifiableMap(markers);
61  	}
62  	
63  	public Set<RowAnnotation> getRowAnnotations() {
64  		if (rowAnnotations==null) {
65  			return Collections.emptySet();
66  		}
67  		return Collections.unmodifiableSet(rowAnnotations);
68  	}
69  	
70  	public Set<MarkerAnnotation> getMarkerAnnotations() {
71  		if (markerAnnotations==null) {
72  			return Collections.emptySet();
73  		}
74  		return Collections.unmodifiableSet(markerAnnotations);
75  	}
76  	
77  	public boolean hasRowAnnotations() {
78  		return rowAnnotations != null;
79  	}
80  	
81  	public boolean hasMarkerAnnotations() {
82  		return markerAnnotations != null;
83  	}
84  
85  	@Override
86  	public String toString() {
87  		return text + "\n/MARKERS: "+markers+"\nra:"+rowAnnotations+", ma:"+markerAnnotations;
88  	}
89  
90  	@Override
91  	public boolean equals(Object other) {
92  		if (other instanceof AceDoc) {
93  			AceDoc od = (AceDoc) other;
94  			return textEquals(text, od.text) &&
95  					Util.sameMaps(this.markers, od.markers) &&
96  					Util.sameSets(this.markerAnnotations, od.markerAnnotations) &&
97  					Util.sameSets(this.rowAnnotations, od.rowAnnotations);
98  		}
99  		return false;
100 	}
101 
102 	@Override
103 	public int hashCode() {
104 		return getText().hashCode();
105 	}
106 
107     public boolean textEquals(String a, String b) {
108         return a == null ? b == null : a.equals(b);
109     }
110 
111 	public AceDoc withText(String newText) {
112 		return new AceDoc(newText, markers, rowAnnotations, markerAnnotations);
113 	}
114 
115 	public TransportDoc asTransport() {
116 		TransportDoc td = new TransportDoc();
117 		td.text = text;
118 
119 		td.markers = getTransportMarkers();
120 		td.markerAnnotations = getTransportMarkerAnnotations();
121 		td.rowAnnotations = getTransportRowAnnotations();
122 
123 		return td;
124 	}
125 
126 	/* TODO private */ Map<String, TransportMarker> getTransportMarkers() {
127 		HashMap<String, TransportMarker> ms = new HashMap<String, TransportMarker>(
128 				markers.size());
129 		for (Entry<String, AceMarker> e : markers.entrySet()) {
130 			ms.put(e.getKey(), e.getValue().asTransport());
131 		}
132 		return ms;
133 	}
134 	
135 
136 	private Set<TransportRowAnnotation> getTransportRowAnnotations() {
137 		if (rowAnnotations==null) {
138 			return null;
139 		}
140 		HashSet<TransportRowAnnotation> anns = new HashSet<TransportRowAnnotation>(
141 				rowAnnotations.size());
142 		for (RowAnnotation ra : rowAnnotations) {
143 			anns.add(ra.asTransport());
144 		}
145 		return anns;
146 	}
147 
148 	private Set<TransportMarkerAnnotation> getTransportMarkerAnnotations() {
149 		if (markerAnnotations==null) {
150 			return null;
151 		}
152 		HashSet<TransportMarkerAnnotation> anns = new HashSet<TransportMarkerAnnotation>(
153 				markerAnnotations.size());
154 		for (MarkerAnnotation ma : markerAnnotations) {
155 			anns.add(ma.asTransport());
156 		}
157 		return anns;
158 	}
159 
160 	public static AceDoc fromTransport(TransportDoc doc) {
161 		String text = doc.text;
162 		Map<String, AceMarker> markers = markersFromTransport(doc.markers, doc.text);
163 		Set<RowAnnotation> rowAnnotations = rowAnnotationsFromTransport(doc.rowAnnotations);
164 		Set<MarkerAnnotation> markerAnnotations = markerAnnotationsFromTransport(doc.markerAnnotations);
165 		return new AceDoc(text, markers, rowAnnotations, markerAnnotations);
166 	}
167 
168 	private static Map<String, AceMarker> markersFromTransport(
169 			Map<String, TransportMarker> markers, String text) {
170 		HashMap<String, AceMarker> ms = new HashMap<String, AceMarker>();
171 		for (Entry<String, TransportMarker> e : markers.entrySet()) {
172 			ms.put(e.getKey(), AceMarker.fromTransport(e.getValue()));
173 		}
174 		return ms;
175 	}
176 
177 	private static Set<MarkerAnnotation> markerAnnotationsFromTransport(
178 			Set<TransportMarkerAnnotation> markerAnnotations) {
179 		if (markerAnnotations==null) {
180 			return null;
181 		}
182 		HashSet<MarkerAnnotation> anns = new HashSet<MarkerAnnotation>(markerAnnotations.size());
183 		for (TransportMarkerAnnotation ta : markerAnnotations) {
184 			anns.add(ta.fromTransport());
185 		}
186 		return anns;
187 	}
188 
189 	private static Set<RowAnnotation> rowAnnotationsFromTransport(
190 			Set<TransportRowAnnotation> rowAnnotations) {
191 		if (rowAnnotations==null) {
192 			return null;
193 		}
194 		HashSet<RowAnnotation> anns = new HashSet<RowAnnotation>(rowAnnotations.size());
195 		for (TransportRowAnnotation ta : rowAnnotations) {
196 			anns.add(ta.fromTransport());
197 		}
198 		return anns;
199 	}
200 	
201 	// TODO?
202 	public AceDoc withMarkers(Set<AceMarker> newMarkers) {
203 		HashMap<String, AceMarker> markers2 = new HashMap<String, AceMarker>(newMarkers.size());
204 		for (AceMarker m : newMarkers) {
205 			markers2.put(m.getMarkerId(), m);
206 		}
207 		return new AceDoc(text, markers2, rowAnnotations, markerAnnotations);
208 	}
209 	
210 	public AceDoc withMarkers(Map<String, AceMarker> newMarkers) {
211 		return new AceDoc(text, newMarkers, rowAnnotations, markerAnnotations);
212 	}
213 	public AceDoc withAdditionalMarker(AceMarker marker) {
214 		HashMap<String, AceMarker> markers2 = new HashMap<String, AceMarker>(markers);
215 		markers2.put(marker.getMarkerId(), marker);
216 		return new AceDoc(text, markers2, rowAnnotations, markerAnnotations);
217 	}
218 
219 	public AceDoc withoutMarker(String markerId) {
220 		HashMap<String, AceMarker> markers2 = new HashMap<String, AceMarker>(markers);
221 		markers2.remove(markerId);
222 		return new AceDoc(text, markers2, rowAnnotations, markerAnnotations);
223 	}
224 
225 	public AceDoc withoutMarkers() {
226 		Map<String, AceMarker> noMarkers = Collections.emptyMap();
227 		return new AceDoc(text, noMarkers, rowAnnotations, markerAnnotations);
228 	}
229 
230 	public AceDoc withRowAnnotations(Set<RowAnnotation> ranns) {
231 		return new AceDoc(text, markers, ranns, markerAnnotations);
232 	}
233 	
234 	public AceDoc withMarkerAnnotations(Set<MarkerAnnotation> manns) {
235 		return new AceDoc(text, markers, rowAnnotations, manns);
236 	}
237 
238 	public AceDoc withAdditionalMarkerAnnotation(MarkerAnnotation mann) {
239 		HashSet<MarkerAnnotation> manns = markerAnnotations==null?new HashSet<MarkerAnnotation>():new HashSet<MarkerAnnotation>(markerAnnotations);
240 		manns.add(mann);
241 		return new AceDoc(text, markers, rowAnnotations, manns);
242 	}
243 	
244 	public AceDoc withAdditionalRowAnnotation(RowAnnotation rann) {
245 		HashSet<RowAnnotation> ranns = rowAnnotations==null?new HashSet<RowAnnotation>():new HashSet<RowAnnotation>(rowAnnotations);
246 		ranns.add(rann);
247 		return new AceDoc(text, markers, ranns, markerAnnotations);
248 	}
249 }