001: /*
002: * Copyright 2003-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: */
017: package net.sourceforge.groboutils.codecoverage.v2.ant.zip;
018:
019: /**
020: * describes a File or a ZipEntry
021: *
022: * this class is meant to be used by classes needing to record path
023: * and date/time information about a file, a zip entry or some similar
024: * resource (URL, archive in a version control repository, ...)
025: *
026: * @author <a href="mailto:levylambert@tiscali-dsl.de">Antoine Levy-Lambert</a>
027: * @since Ant 1.5.2
028: */
029: public class Resource implements Cloneable, Comparable {
030: private String name = null;
031: private boolean exists = true;
032: private long lastmodified = 0;
033: private boolean directory = false;
034:
035: /**
036: * default constructor
037: */
038: public Resource() {
039: }
040:
041: /**
042: * only sets the name.
043: *
044: * <p>This is a dummy, used for not existing resources.</p>
045: *
046: * @param name relative path of the resource. Expects
047: * "/" to be used as the directory separator.
048: */
049: public Resource(String name) {
050: this (name, false, 0, false);
051: }
052:
053: /**
054: * sets the name, lastmodified flag, and exists flag
055: *
056: * @param name relative path of the resource. Expects
057: * "/" to be used as the directory separator.
058: */
059: public Resource(String name, boolean exists, long lastmodified) {
060: this (name, exists, lastmodified, false);
061: }
062:
063: /**
064: * @param name relative path of the resource. Expects
065: * "/" to be used as the directory separator.
066: */
067: public Resource(String name, boolean exists, long lastmodified,
068: boolean directory) {
069: this .name = name;
070: this .exists = exists;
071: this .lastmodified = lastmodified;
072: this .directory = directory;
073: }
074:
075: /**
076: * name attribute will contain the path of a file relative to the
077: * root directory of its fileset or the recorded path of a zip
078: * entry.
079: *
080: * <p>example for a file with fullpath /var/opt/adm/resource.txt
081: * in a file set with root dir /var/opt it will be
082: * adm/resource.txt.</p>
083: *
084: * <p>"/" will be used as the directory separator.</p>
085: */
086: public String getName() {
087: return name;
088: }
089:
090: /**
091: * @param name relative path of the resource. Expects
092: * "/" to be used as the directory separator.
093: */
094: public void setName(String name) {
095: this .name = name;
096: }
097:
098: /**
099: * the exists attribute tells whether a file exists
100: */
101: public boolean isExists() {
102: return exists;
103: }
104:
105: public void setExists(boolean exists) {
106: this .exists = exists;
107: }
108:
109: /**
110: * tells the modification time in milliseconds since 01.01.1970 of
111: *
112: * @return 0 if the resource does not exist to mirror the behavior
113: * of {@link java.io.File File}.
114: */
115: public long getLastModified() {
116: return !exists || lastmodified < 0 ? 0 : lastmodified;
117: }
118:
119: public void setLastModified(long lastmodified) {
120: this .lastmodified = lastmodified;
121: }
122:
123: /**
124: * tells if the resource is a directory
125: * @return boolean flag indicating if the resource is a directory
126: */
127: public boolean isDirectory() {
128: return directory;
129: }
130:
131: public void setDirectory(boolean directory) {
132: this .directory = directory;
133: }
134:
135: /**
136: * @return copy of this
137: */
138: public Object clone() {
139: try {
140: return super .clone();
141: } catch (CloneNotSupportedException e) {
142: throw new Error("CloneNotSupportedException for a "
143: + "Clonable Resource caught?");
144: }
145: }
146:
147: /**
148: * delegates to a comparison of names.
149: *
150: * @since Ant 1.6
151: */
152: public int compareTo(Object other) {
153: if (!(other instanceof Resource)) {
154: throw new IllegalArgumentException(
155: "Can only be compared with " + "Resources");
156: }
157: Resource r = (Resource) other;
158: return getName().compareTo(r.getName());
159: }
160: }
|