Lines 70-77
Link Here
|
70 |
* @author Vladimir Kvashin |
70 |
* @author Vladimir Kvashin |
71 |
*/ |
71 |
*/ |
72 |
public abstract class OffsetableBase implements CsmOffsetable, Disposable, CsmValidable { |
72 |
public abstract class OffsetableBase implements CsmOffsetable, Disposable, CsmValidable { |
73 |
private /*final*/ CsmFile fileRef; // set in onDispose, used as marker of invalid object |
73 |
private final CsmFile fileRef;// we keep ref to file, because it owns disposing flag from project |
74 |
private final CsmUID<CsmFile> fileUID; |
74 |
private boolean isValid = true; |
75 |
|
75 |
|
76 |
private final int startPosition; |
76 |
private final int startPosition; |
77 |
private final int endPosition; |
77 |
private final int endPosition; |
Lines 84-119
Link Here
|
84 |
|
84 |
|
85 |
protected OffsetableBase(CsmFile file, int start, int end) { |
85 |
protected OffsetableBase(CsmFile file, int start, int end) { |
86 |
// Parameters.notNull("file can not be null", file); // NOI18N |
86 |
// Parameters.notNull("file can not be null", file); // NOI18N |
87 |
this.fileUID = UIDCsmConverter.fileToUID(file); |
87 |
this.fileRef = file; |
88 |
this.fileRef = null;// to prevent error with "final" |
|
|
89 |
if(end < start) { |
88 |
if(end < start) { |
90 |
if(CndUtils.isDebugMode()) { |
89 |
if(CndUtils.isDebugMode()) { |
91 |
CndUtils.assertTrueInConsole(false, "end < start for " + ((file != null)?file.getAbsolutePath():"null file") + ":[" + start + "-" + end + "]"); // NOI18N |
90 |
CndUtils.assertTrueInConsole(false, "end < start for " + ((file != null)?file.getAbsolutePath():"null file") + ":[" + start + "-" + end + "]"); // NOI18N |
92 |
} |
91 |
} |
93 |
end = start; |
92 |
end = start; |
94 |
} |
93 |
} |
|
|
94 |
CsmUID<CsmFile> fileUID = UIDCsmConverter.fileToUID(file); |
95 |
this.startPosition = PositionManager.createPositionID(fileUID, start, PositionManager.Position.Bias.FOWARD); |
95 |
this.startPosition = PositionManager.createPositionID(fileUID, start, PositionManager.Position.Bias.FOWARD); |
96 |
this.endPosition = PositionManager.createPositionID(fileUID, end, PositionManager.Position.Bias.BACKWARD); |
96 |
this.endPosition = PositionManager.createPositionID(fileUID, end, PositionManager.Position.Bias.BACKWARD); |
97 |
} |
97 |
} |
98 |
|
98 |
|
99 |
@Override |
99 |
@Override |
100 |
final public int getStartOffset() { |
100 |
final public int getStartOffset() { |
101 |
return PositionManager.getOffset(fileUID, startPosition); |
101 |
return PositionManager.getOffset(fileRef, startPosition); |
102 |
} |
102 |
} |
103 |
|
103 |
|
104 |
@Override |
104 |
@Override |
105 |
final public int getEndOffset() { |
105 |
final public int getEndOffset() { |
106 |
return endPosition != 0 ? PositionManager.getOffset(fileUID, endPosition) : PositionManager.getOffset(fileUID, startPosition); |
106 |
return endPosition != 0 ? PositionManager.getOffset(fileRef, endPosition) : PositionManager.getOffset(fileRef, startPosition); |
107 |
} |
107 |
} |
108 |
|
108 |
|
109 |
@Override |
109 |
@Override |
110 |
public final Position getStartPosition() { |
110 |
public final Position getStartPosition() { |
111 |
return PositionManager.getPosition(fileUID, startPosition); |
111 |
return PositionManager.getPosition(fileRef, startPosition); |
112 |
} |
112 |
} |
113 |
|
113 |
|
114 |
@Override |
114 |
@Override |
115 |
public final Position getEndPosition() { |
115 |
public final Position getEndPosition() { |
116 |
return PositionManager.getPosition(fileUID, endPosition); |
116 |
return PositionManager.getPosition(fileRef, endPosition); |
117 |
} |
117 |
} |
118 |
|
118 |
|
119 |
public static int getStartOffset(AST node) { |
119 |
public static int getStartOffset(AST node) { |
Lines 155-166
Link Here
|
155 |
|
155 |
|
156 |
@Override |
156 |
@Override |
157 |
public CsmFile getContainingFile() { |
157 |
public CsmFile getContainingFile() { |
158 |
return _getFile(true); |
158 |
return this.fileRef; |
159 |
} |
159 |
} |
160 |
|
160 |
|
161 |
@Override |
161 |
@Override |
162 |
public boolean isValid() { |
162 |
public boolean isValid() { |
163 |
return CsmBaseUtilities.isValid(_getFile(false)) && this.fileRef == null; |
163 |
return isValid && CsmBaseUtilities.isValid(this.fileRef); |
164 |
} |
164 |
} |
165 |
|
165 |
|
166 |
@Override |
166 |
@Override |
Lines 179-216
Link Here
|
179 |
} |
179 |
} |
180 |
|
180 |
|
181 |
private synchronized void onDispose() { |
181 |
private synchronized void onDispose() { |
182 |
if (fileRef == null) { |
182 |
this.isValid = false; |
183 |
// restore container from it's UID |
183 |
} |
184 |
this.fileRef = UIDCsmConverter.UIDtoFile(fileUID); |
|
|
185 |
assert this.fileRef != null : "no object for UID " + fileUID; |
186 |
} |
187 |
} |
188 |
|
189 |
private synchronized CsmFile _getFile(boolean checkNull) { |
190 |
CsmFile file = this.fileRef; |
191 |
if (file == null) { |
192 |
file = UIDCsmConverter.UIDtoFile(fileUID); |
193 |
assert file != null || !checkNull: "no object for UID " + fileUID + " in object " + getClass() + ":" + getOffsetString(); |
194 |
} |
195 |
return file; |
196 |
} |
197 |
|
184 |
|
198 |
public void write(RepositoryDataOutput output) throws IOException { |
185 |
public void write(RepositoryDataOutput output) throws IOException { |
199 |
output.writeInt(startPosition); |
186 |
output.writeInt(startPosition); |
200 |
output.writeInt(endPosition); |
187 |
output.writeInt(endPosition); |
|
|
188 |
CsmUID<CsmFile> fileUID = UIDCsmConverter.fileToUID(fileRef); |
201 |
// not null UID |
189 |
// not null UID |
202 |
assert this.fileUID != null; |
190 |
assert fileUID != null; |
203 |
UIDObjectFactory.getDefaultFactory().writeUID(this.fileUID, output); |
191 |
UIDObjectFactory.getDefaultFactory().writeUID(fileUID, output); |
204 |
} |
192 |
} |
205 |
|
193 |
|
206 |
protected OffsetableBase(RepositoryDataInput input) throws IOException { |
194 |
protected OffsetableBase(RepositoryDataInput input) throws IOException { |
207 |
startPosition = input.readInt(); |
195 |
startPosition = input.readInt(); |
208 |
endPosition = input.readInt(); |
196 |
endPosition = input.readInt(); |
209 |
|
197 |
|
210 |
this.fileUID = UIDObjectFactory.getDefaultFactory().readUID(input); |
198 |
CsmUID<CsmFile> fileUID = UIDObjectFactory.getDefaultFactory().readUID(input); |
211 |
// not null UID |
199 |
// not null UID |
212 |
assert this.fileUID != null; |
200 |
assert fileUID != null; |
213 |
this.fileRef = null; |
201 |
this.fileRef = UIDCsmConverter.UIDtoFile(fileUID); |
|
|
202 |
this.isValid = (this.fileRef != null); |
203 |
} |
204 |
|
205 |
protected OffsetableBase(CsmFile containingFile, RepositoryDataInput input) throws IOException { |
206 |
// must be in sync with the above constructor |
207 |
startPosition = input.readInt(); |
208 |
endPosition = input.readInt(); |
209 |
|
210 |
CsmUID<CsmFile> fileUID = UIDObjectFactory.getDefaultFactory().readUID(input); |
211 |
// not null UID |
212 |
assert fileUID != null; |
213 |
this.fileRef = containingFile; |
214 |
this.isValid = (this.fileRef != null); |
214 |
} |
215 |
} |
215 |
|
216 |
|
216 |
// test trace method |
217 |
// test trace method |
Lines 221-229
Link Here
|
221 |
protected CharSequence getPositionString() { |
222 |
protected CharSequence getPositionString() { |
222 |
StringBuilder sb = new StringBuilder(); |
223 |
StringBuilder sb = new StringBuilder(); |
223 |
sb.append('['); |
224 |
sb.append('['); |
224 |
CsmFile containingFile = _getFile(false); |
225 |
CsmFile containingFile = this.fileRef; |
225 |
if (containingFile == null) { |
226 |
if (containingFile == null) { |
226 |
sb.append(" NO CONTAINER ").append(fileUID);// NOI18N |
227 |
sb.append(" NO CONTAINER ");// NOI18N |
227 |
} else { |
228 |
} else { |
228 |
sb.append(containingFile.getName()); |
229 |
sb.append(containingFile.getName()); |
229 |
} |
230 |
} |
Lines 256-262
Link Here
|
256 |
return false; |
257 |
return false; |
257 |
} |
258 |
} |
258 |
final OffsetableBase other = (OffsetableBase) obj; |
259 |
final OffsetableBase other = (OffsetableBase) obj; |
259 |
if (this.fileUID != other.fileUID && (this.fileUID == null || !this.fileUID.equals(other.fileUID))) { |
260 |
if ((this.fileRef == null || !this.fileRef.equals(other.fileRef))) { |
260 |
return false; |
261 |
return false; |
261 |
} |
262 |
} |
262 |
if (this.startPosition != other.startPosition) { |
263 |
if (this.startPosition != other.startPosition) { |
Lines 271-277
Link Here
|
271 |
@Override |
272 |
@Override |
272 |
public int hashCode() { |
273 |
public int hashCode() { |
273 |
int hash = 7; |
274 |
int hash = 7; |
274 |
hash = 47 * hash + (this.fileUID != null ? this.fileUID.hashCode() : 0); |
275 |
hash = 47 * hash + (this.fileRef != null ? this.fileRef.hashCode() : 0); |
275 |
hash = 47 * hash + this.startPosition; |
276 |
hash = 47 * hash + this.startPosition; |
276 |
hash = 47 * hash + this.endPosition; |
277 |
hash = 47 * hash + this.endPosition; |
277 |
return hash; |
278 |
return hash; |