PageRenderTime 51ms CodeModel.GetById 24ms app.highlight 20ms RepoModel.GetById 2ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/buffer/PositionManager.java

#
Java | 209 lines | 125 code | 30 blank | 54 comment | 14 complexity | 837a963b48b368f25defa0026a0bc217 MD5 | raw file
  1/*
  2 * PositionManager.java - Manages positions
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2001, 2005 Slava Pestov
  7 *
  8 * This program is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU General Public License
 10 * as published by the Free Software Foundation; either version 2
 11 * of the License, or any later version.
 12 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program; if not, write to the Free Software
 20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 21 */
 22
 23package org.gjt.sp.jedit.buffer;
 24
 25//{{{ Imports
 26import javax.swing.text.Position;
 27import java.util.*;
 28import org.gjt.sp.util.Log;
 29//}}}
 30
 31/**
 32 * A class internal to jEdit's document model. You should not use it
 33 * directly.
 34 *
 35 * @author Slava Pestov
 36 * @version $Id: PositionManager.java 17813 2010-05-12 14:20:37Z k_satoda $
 37 * @since jEdit 4.2pre3
 38 */
 39class PositionManager
 40{
 41	//{{{ PositionManager constructor
 42	public PositionManager(JEditBuffer buffer)
 43	{
 44		this.buffer = buffer;
 45	} //}}}
 46	
 47	//{{{ createPosition() method
 48	public synchronized Position createPosition(int offset)
 49	{
 50		PosBottomHalf bh = new PosBottomHalf(offset);
 51		PosBottomHalf existing = positions.get(bh);
 52		if(existing == null)
 53		{
 54			positions.put(bh,bh);
 55			existing = bh;
 56		}
 57
 58		return new PosTopHalf(existing);
 59	} //}}}
 60
 61	//{{{ contentInserted() method
 62	public synchronized void contentInserted(int offset, int length)
 63	{
 64		if(positions.isEmpty())
 65			return;
 66
 67		/* get all positions from offset to the end, inclusive */
 68		Iterator<PosBottomHalf> iter = positions.tailMap(new PosBottomHalf(offset))
 69			.keySet().iterator();
 70
 71		iteration = true;
 72		while(iter.hasNext())
 73		{
 74			iter.next().contentInserted(offset,length);
 75		}
 76		iteration = false;
 77	} //}}}
 78
 79	//{{{ contentRemoved() method
 80	public synchronized void contentRemoved(int offset, int length)
 81	{
 82		if(positions.isEmpty())
 83			return;
 84
 85		/* get all positions from offset to the end, inclusive */
 86		Iterator<PosBottomHalf> iter = positions.tailMap(new PosBottomHalf(offset))
 87			.keySet().iterator();
 88
 89		iteration = true;
 90		while(iter.hasNext())
 91		{
 92			iter.next().contentRemoved(offset,length);
 93		}
 94		iteration = false;
 95
 96	} //}}}
 97
 98	boolean iteration;
 99
100	//{{{ Private members
101	private JEditBuffer buffer;
102	private SortedMap<PosBottomHalf, PosBottomHalf> positions = new TreeMap<PosBottomHalf, PosBottomHalf>();
103	//}}}
104
105	//{{{ Inner classes
106
107	//{{{ PosTopHalf class
108	class PosTopHalf implements Position
109	{
110		final PosBottomHalf bh;
111
112		//{{{ PosTopHalf constructor
113		PosTopHalf(PosBottomHalf bh)
114		{
115			this.bh = bh;
116			bh.ref();
117		} //}}}
118
119		//{{{ getOffset() method
120		public int getOffset()
121		{
122			return bh.offset;
123		} //}}}
124
125		//{{{ finalize() method
126		@Override
127		protected void finalize()
128		{
129			synchronized(PositionManager.this)
130			{
131				bh.unref();
132			}
133		} //}}}
134	} //}}}
135
136	//{{{ PosBottomHalf class
137	class PosBottomHalf implements Comparable<PosBottomHalf>
138	{
139		int offset;
140		int ref;
141
142		//{{{ PosBottomHalf constructor
143		PosBottomHalf(int offset)
144		{
145			this.offset = offset;
146		} //}}}
147
148		//{{{ ref() method
149		void ref()
150		{
151			ref++;
152		} //}}}
153
154		//{{{ unref() method
155		void unref()
156		{
157			if(--ref == 0)
158				positions.remove(this);
159		} //}}}
160
161		//{{{ contentInserted() method
162		void contentInserted(int offset, int length)
163		{
164			if(offset > this.offset)
165				throw new ArrayIndexOutOfBoundsException();
166			this.offset += length;
167			checkInvariants();
168		} //}}}
169
170		//{{{ contentRemoved() method
171		void contentRemoved(int offset, int length)
172		{
173			if(offset > this.offset)
174				throw new ArrayIndexOutOfBoundsException();
175			if(this.offset <= offset + length)
176				this.offset = offset;
177			else
178				this.offset -= length;
179			checkInvariants();
180		} //}}}
181
182		//{{{ equals() method
183		@Override
184		public boolean equals(Object o)
185		{
186			if(!(o instanceof PosBottomHalf))
187				return false;
188
189			return ((PosBottomHalf)o).offset == offset;
190		} //}}}
191
192		//{{{ compareTo() method
193		public int compareTo(PosBottomHalf posBottomHalf)
194		{
195			if(iteration)
196				Log.log(Log.ERROR,this,"Consistency failure");
197			return offset - posBottomHalf.offset;
198		} //}}}
199		
200		//{{{ checkInvariants() method
201		private void checkInvariants()
202		{
203			if(offset < 0 || offset > buffer.getLength())
204				throw new ArrayIndexOutOfBoundsException();
205		} //}}}
206	} //}}}
207
208	//}}}
209}