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
015package org.apache.tapestry5.ioc.internal.util;
016
017import org.apache.tapestry5.ioc.Location;
018import org.apache.tapestry5.ioc.Resource;
019
020import java.util.Formatter;
021
022/**
023 * Implementation class for {@link org.apache.tapestry5.ioc.Location}.
024 */
025public 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    @Override
053    public Resource getResource()
054    {
055        return resource;
056    }
057
058    @Override
059    public int getLine()
060    {
061        return line;
062    }
063
064    @Override
065    public int getColumn()
066    {
067        return column;
068    }
069
070    @Override
071    public String toString()
072    {
073        StringBuilder buffer = new StringBuilder(resource.toString());
074        Formatter formatter = new Formatter(buffer);
075
076        if (line != UNKNOWN) formatter.format(", line %d", line);
077
078        if (column != UNKNOWN) formatter.format(", column %d", column);
079
080        return buffer.toString();
081    }
082
083    @Override
084    public int hashCode()
085    {
086        final int PRIME = 31;
087        int result = 1;
088        result = PRIME * result + column;
089        result = PRIME * result + line;
090        result = PRIME * result + ((resource == null) ? 0 : resource.hashCode());
091        return result;
092    }
093
094    @Override
095    public boolean equals(Object obj)
096    {
097        if (this == obj) return true;
098        if (obj == null) return false;
099        if (getClass() != obj.getClass()) return false;
100        final LocationImpl other = (LocationImpl) obj;
101        if (column != other.column) return false;
102        if (line != other.line) return false;
103        if (resource == null)
104        {
105            if (other.resource != null) return false;
106        }
107        else if (!resource.equals(other.resource)) return false;
108        return true;
109    }
110
111}