001    // Copyright 2006, 2007 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.tapestry5.ioc.internal.util;
016    
017    import org.apache.tapestry5.ioc.Location;
018    import org.apache.tapestry5.ioc.Resource;
019    
020    import java.util.Formatter;
021    
022    /**
023     * Implementation class for {@link org.apache.tapestry5.ioc.Location}.
024     */
025    public final class LocationImpl implements Location
026    {
027        private final Resource resource;
028    
029        private final int line;
030    
031        private final int column;
032    
033        private static final int UNKNOWN = -1;
034    
035        public LocationImpl(Resource resource)
036        {
037            this(resource, UNKNOWN);
038        }
039    
040        public LocationImpl(Resource resource, int line)
041        {
042            this(resource, line, UNKNOWN);
043        }
044    
045        public LocationImpl(Resource resource, int line, int column)
046        {
047            this.resource = resource;
048            this.line = line;
049            this.column = column;
050        }
051    
052        public Resource getResource()
053        {
054            return resource;
055        }
056    
057        public int getLine()
058        {
059            return line;
060        }
061    
062        public int getColumn()
063        {
064            return column;
065        }
066    
067        @Override
068        public String toString()
069        {
070            StringBuilder buffer = new StringBuilder(resource.toString());
071            Formatter formatter = new Formatter(buffer);
072    
073            if (line != UNKNOWN) formatter.format(", line %d", line);
074    
075            if (column != UNKNOWN) formatter.format(", column %d", column);
076    
077            return buffer.toString();
078        }
079    
080        @Override
081        public int hashCode()
082        {
083            final int PRIME = 31;
084            int result = 1;
085            result = PRIME * result + column;
086            result = PRIME * result + line;
087            result = PRIME * result + ((resource == null) ? 0 : resource.hashCode());
088            return result;
089        }
090    
091        @Override
092        public boolean equals(Object obj)
093        {
094            if (this == obj) return true;
095            if (obj == null) return false;
096            if (getClass() != obj.getClass()) return false;
097            final LocationImpl other = (LocationImpl) obj;
098            if (column != other.column) return false;
099            if (line != other.line) return false;
100            if (resource == null)
101            {
102                if (other.resource != null) return false;
103            }
104            else if (!resource.equals(other.resource)) return false;
105            return true;
106        }
107    
108    }