KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > impl > LocationImpl


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.impl;
16
17 import org.apache.hivemind.Location;
18 import org.apache.hivemind.Resource;
19
20 /**
21  * Implementation of the {@link org.apache.hivemind.Location} interface.
22  * Uses a line and column based position.
23  *
24  * @author Howard Lewis Ship
25  */

26 public final class LocationImpl implements Location
27 {
28     private Resource _resource;
29     private int _lineNumber = -1;
30     private int _columnNumber = -1;
31
32     public LocationImpl(Resource resource)
33     {
34         _resource = resource;
35     }
36
37     public LocationImpl(Resource resource, int lineNumber)
38     {
39         this(resource);
40
41         _lineNumber = lineNumber;
42     }
43
44     public LocationImpl(Resource resource, int lineNumber, int columnNumber)
45     {
46         this(resource);
47
48         _lineNumber = lineNumber;
49         _columnNumber = columnNumber;
50     }
51
52     public Resource getResource()
53     {
54         return _resource;
55     }
56
57     public int getLineNumber()
58     {
59         return _lineNumber;
60     }
61
62     public int getColumnNumber()
63     {
64         return _columnNumber;
65     }
66
67     public int hashCode()
68     {
69         return ((237 + _resource.hashCode()) << 3 + _lineNumber) << 3 + _columnNumber;
70     }
71
72     public boolean equals(Object JavaDoc other)
73     {
74         if (!(other instanceof LocationImpl))
75             return false;
76
77         LocationImpl l = (LocationImpl) other;
78
79         if (_lineNumber != l.getLineNumber())
80             return false;
81
82         if (_columnNumber != l.getColumnNumber())
83             return false;
84
85         return _resource.equals(l.getResource());
86     }
87     
88     /**
89      * Returns the position in format "line x, column y"
90      *
91      * @see org.apache.hivemind.Location#getPosition()
92      */

93     public String JavaDoc getPosition()
94     {
95         String JavaDoc result = "";
96         
97         if (_lineNumber > 0)
98         {
99             result += "line " + _lineNumber;
100         }
101
102         if (_columnNumber > 0)
103         {
104             if (result.length() > 0) {
105                 result += ", ";
106             }
107             result += "column " + _columnNumber;
108         }
109         
110         return result;
111     }
112     
113     public String JavaDoc toString()
114     {
115         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(_resource.toString());
116         String JavaDoc position = getPosition();
117         if (position.length() > 0) {
118             buffer.append(", ");
119             buffer.append(position);
120         }
121
122         return buffer.toString();
123     }
124
125  }
126
Popular Tags