KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > TypedPosition


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.text;
12
13
14
15 /**
16  * Convenience class for positions that have a type, similar to
17  * {@link org.eclipse.jface.text.ITypedRegion}.
18  * <p>
19  * As {@link org.eclipse.jface.text.Position},<code>TypedPosition</code> can
20  * not be used as key in hash tables as it overrides <code>equals</code> and
21  * <code>hashCode</code> as it would be a value object.
22  */

23 public class TypedPosition extends Position {
24
25     /** The type of the region described by this position */
26     private String JavaDoc fType;
27
28     /**
29      * Creates a position along the given specification.
30      *
31      * @param offset the offset of this position
32      * @param length the length of this position
33      * @param type the type of this position
34      */

35     public TypedPosition(int offset, int length, String JavaDoc type) {
36         super(offset, length);
37         fType= type;
38     }
39
40     /**
41      * Creates a position based on the typed region.
42      *
43      * @param region the typed region
44      */

45     public TypedPosition(ITypedRegion region) {
46         super(region.getOffset(), region.getLength());
47         fType= region.getType();
48     }
49
50     /**
51      * Returns the type of the position
52      *
53      * @return the type of this position
54      */

55     public String JavaDoc getType() {
56         return fType;
57     }
58
59     /*
60      * @see java.lang.Object#equals(java.lang.Object)
61      */

62     public boolean equals(Object JavaDoc o) {
63         if (o instanceof TypedPosition) {
64             if (super.equals(o)) {
65                 TypedPosition p= (TypedPosition) o;
66                 return (fType == null && p.getType() == null) || fType.equals(p.getType());
67             }
68         }
69         return false;
70     }
71
72      /*
73      * @see java.lang.Object#hashCode()
74      */

75     public int hashCode() {
76         int type= fType == null ? 0 : fType.hashCode();
77         return super.hashCode() | type;
78      }
79 }
80
Popular Tags