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     *
023     * @author Howard Lewis Ship
024     */
025    public final class LocationImpl implements Location
026    {
027        private Resource _resource;
028        private int _lineNumber = -1;
029        private int _columnNumber = -1;
030    
031        public LocationImpl(Resource resource)
032        {
033            _resource = resource;
034        }
035    
036        public LocationImpl(Resource resource, int lineNumber)
037        {
038            this(resource);
039    
040            _lineNumber = lineNumber;
041        }
042    
043        public LocationImpl(Resource resource, int lineNumber, int columnNumber)
044        {
045            this(resource);
046    
047            _lineNumber = lineNumber;
048            _columnNumber = columnNumber;
049        }
050    
051        public Resource getResource()
052        {
053            return _resource;
054        }
055    
056        public int getLineNumber()
057        {
058            return _lineNumber;
059        }
060    
061        public int getColumnNumber()
062        {
063            return _columnNumber;
064        }
065    
066        public int hashCode()
067        {
068            return ((237 + _resource.hashCode()) << 3 + _lineNumber) << 3 + _columnNumber;
069        }
070    
071        public boolean equals(Object other)
072        {
073            if (!(other instanceof Location))
074                return false;
075    
076            Location l = (Location) other;
077    
078            if (_lineNumber != l.getLineNumber())
079                return false;
080    
081            if (_columnNumber != l.getColumnNumber())
082                return false;
083    
084            return _resource.equals(l.getResource());
085        }
086    
087        public String toString()
088        {
089            if (_lineNumber <= 0 && _columnNumber <= 0)
090                return _resource.toString();
091    
092            StringBuffer buffer = new StringBuffer(_resource.toString());
093    
094            if (_lineNumber > 0)
095            {
096                buffer.append(", line ");
097                buffer.append(_lineNumber);
098            }
099    
100            if (_columnNumber > 0)
101            {
102                buffer.append(", column ");
103                buffer.append(_columnNumber);
104            }
105    
106            return buffer.toString();
107        }
108    }