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