1 /*******************************************************************************
2  * Copyright (c) 2018 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *     Red Hat Inc. - copied from SemanticHighlightingManager and modified
14  *******************************************************************************/
15 package org.eclipse.jdt.internal.ui.javaeditor;
16 
17 import org.eclipse.jface.text.Position;
18 
19 /**
20  * Highlighted Positions.
21  *
22  * @since 1.11
23  */
24 public class HighlightedPositionCore extends Position {
25 
26 	/** Lock object */
27 	private Object fLock;
28 
29 	/** Highlighting object */
30 	private Object fHighlighting;
31 
32 	/**
33 	 * Initialize the styled positions with the given offset, length and foreground color.
34 	 *
35 	 * @param offset The position offset
36 	 * @param length The position length
37 	 * @param highlighting The highlighting object
38 	 * @param lock The lock object
39 	 */
HighlightedPositionCore(int offset, int length, Object highlighting, Object lock)40 	public HighlightedPositionCore(int offset, int length, Object highlighting, Object lock) {
41 		super(offset, length);
42 		fHighlighting = highlighting;
43 		fLock= lock;
44 	}
45 
46 
47 	/**
48 	 * Uses reference equality for the highlighting.
49 	 *
50 	 * @param off The offset
51 	 * @param len The length
52 	 * @param highlighting The highlighting
53 	 * @return <code>true</code> iff the given offset, length and highlighting are equal to the internal ones.
54 	 */
isEqual(int off, int len, Object highlighting)55 	public boolean isEqual(int off, int len, Object highlighting) {
56 		synchronized (fLock) {
57 			return !isDeleted() && getOffset() == off && getLength() == len && fHighlighting == highlighting;
58 		}
59 	}
60 
61 	/**
62 	 * Is this position contained in the given range (inclusive)? Synchronizes on position updater.
63 	 *
64 	 * @param off The range offset
65 	 * @param len The range length
66 	 * @return <code>true</code> iff this position is not delete and contained in the given range.
67 	 */
isContained(int off, int len)68 	public boolean isContained(int off, int len) {
69 		synchronized (fLock) {
70 			return !isDeleted() && off <= getOffset() && off + len >= getOffset() + getLength();
71 		}
72 	}
73 
update(int off, int len)74 	public void update(int off, int len) {
75 		synchronized (fLock) {
76 			super.setOffset(off);
77 			super.setLength(len);
78 		}
79 	}
80 
81 	/*
82 	 * @see org.eclipse.jface.text.Position#setLength(int)
83 	 */
84 	@Override
setLength(int length)85 	public void setLength(int length) {
86 		synchronized (fLock) {
87 			super.setLength(length);
88 		}
89 	}
90 
91 	/*
92 	 * @see org.eclipse.jface.text.Position#setOffset(int)
93 	 */
94 	@Override
setOffset(int offset)95 	public void setOffset(int offset) {
96 		synchronized (fLock) {
97 			super.setOffset(offset);
98 		}
99 	}
100 
101 	/*
102 	 * @see org.eclipse.jface.text.Position#delete()
103 	 */
104 	@Override
delete()105 	public void delete() {
106 		synchronized (fLock) {
107 			super.delete();
108 		}
109 	}
110 
111 	/*
112 	 * @see org.eclipse.jface.text.Position#undelete()
113 	 */
114 	@Override
undelete()115 	public void undelete() {
116 		synchronized (fLock) {
117 			super.undelete();
118 		}
119 	}
120 
121 	/**
122 	 * @return Returns the highlighting.
123 	 */
getHighlighting()124 	public Object getHighlighting() {
125 		return fHighlighting;
126 	}
127 }