001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.hivemind.impl;
016    
017    import org.apache.hivemind.Location;
018    import org.apache.hivemind.Resource;
019    
020    /**
021     * Implementation of the {@link org.apache.hivemind.Location} interface.
022     * Uses a line and column based position.
023     *
024     * @author Howard Lewis Ship
025     */
026    public final class LocationImpl implements Location
027    {
028        private Resource _resource;
029        private int _lineNumber = -1;
030        private int _columnNumber = -1;
031    
032        public LocationImpl(Resource resource)
033        {
034            _resource = resource;
035        }
036    
037        public LocationImpl(Resource resource, int lineNumber)
038        {
039            this(resource);
040    
041            _lineNumber = lineNumber;
042        }
043    
044        public LocationImpl(Resource resource, int lineNumber, int columnNumber)
045        {
046            this(resource);
047    
048            _lineNumber = lineNumber;
049            _columnNumber = columnNumber;
050        }
051    
052        public Resource getResource()
053        {
054            return _resource;
055        }
056    
057        public int getLineNumber()
058        {
059            return _lineNumber;
060        }
061    
062        public int getColumnNumber()
063        {
064            return _columnNumber;
065        }
066    
067        public int hashCode()
068        {
069            return ((237 + _resource.hashCode()) << 3 + _lineNumber) << 3 + _columnNumber;
070        }
071    
072        public boolean equals(Object other)
073        {
074            if (!(other instanceof LocationImpl))
075                return false;
076    
077            LocationImpl l = (LocationImpl) other;
078    
079            if (_lineNumber != l.getLineNumber())
080                return false;
081    
082            if (_columnNumber != l.getColumnNumber())
083                return false;
084    
085            return _resource.equals(l.getResource());
086        }
087        
088        /**
089         * Returns the position in format "line x, column y"
090         * 
091         * @see org.apache.hivemind.Location#getPosition()
092         */
093        public String getPosition()
094        {
095            String result = "";
096            
097            if (_lineNumber > 0)
098            {
099                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 toString()
114        {
115            StringBuffer buffer = new StringBuffer(_resource.toString());
116            String position = getPosition();
117            if (position.length() > 0) {
118                buffer.append(", "); 
119                buffer.append(position); 
120            }
121    
122            return buffer.toString();
123        }
124    
125     }