Browse Source

use adapter for Touchable instead of instanceof checks

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@718210 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 17 years ago
parent
commit
31e1bc0694
3 changed files with 19 additions and 59 deletions
  1. +3
    -2
      src/main/org/apache/tools/ant/taskdefs/Touch.java
  2. +11
    -55
      src/main/org/apache/tools/ant/types/resources/MappedResource.java
  3. +5
    -2
      src/main/org/apache/tools/ant/util/ResourceUtils.java

+ 3
- 2
src/main/org/apache/tools/ant/taskdefs/Touch.java View File

@@ -299,7 +299,8 @@ public class Touch extends Task {
Iterator iter = resources.iterator();
while (iter.hasNext()) {
Resource r = (Resource) iter.next();
if (!(r instanceof Touchable)) {
Touchable t = (Touchable) r.as(Touchable.class);
if (t == null) {
throw new BuildException("Can't touch " + r);
}
touch(r, defaultTimestamp);
@@ -343,7 +344,7 @@ public class Touch extends Task {
// use this to create file and deal with non-writable files
touch(((FileProvider) r).getFile(), defaultTimestamp);
} else {
((Touchable) r).touch(defaultTimestamp);
((Touchable) r.as(Touchable.class)).touch(defaultTimestamp);
}
} else {
String[] mapped = fileNameMapper.mapFileName(r.getName());


+ 11
- 55
src/main/org/apache/tools/ant/types/resources/MappedResource.java View File

@@ -38,6 +38,7 @@ import org.apache.tools.ant.types.Resource;
public class MappedResource extends Resource {
private final Resource wrapped;
private final boolean isAppendable;
private final boolean isTouchable;

// should only be instantiated via factory, this also means we
// don't have to think about being a reference to a different
@@ -49,6 +50,7 @@ public class MappedResource extends Resource {
protected MappedResource(Resource r) {
wrapped = r;
isAppendable = wrapped.as(Appendable.class) != null;
isTouchable = wrapped.as(Touchable.class) != null;
}

/**
@@ -162,21 +164,19 @@ public class MappedResource extends Resource {
}
};
}
if (clazz == Touchable.class && isTouchable) {
return new Touchable() {
public void touch(long modTime) {
((Touchable) wrapped.as(Touchable.class)).touch(modTime);
}
};
}
return super.as(clazz);
}

public static MappedResource map(Resource r) {
if (r instanceof FileProvider) {
if (r instanceof Touchable) {
return new TouchableFileProviderMR(r);
}
return new FileProviderMR(r);
}
if (r instanceof Touchable) {
return new TouchableMR(r);
}
// no special interface
return new MappedResource(r);
return r instanceof FileProvider
? new FileProviderMR(r) : new MappedResource(r);
}

private static class FileProviderMR extends MappedResource
@@ -201,48 +201,4 @@ public class MappedResource extends Resource {
}
}

private static class TouchableMR extends MappedResource
implements Touchable {
private final Touchable t;

protected TouchableMR(Resource r) {
super(r);
if (!(r instanceof Touchable)) {
throw new IllegalArgumentException("trying to wrap something "
+ "that is not a "
+ " Touchable");
}
t = (Touchable) r;
}

/**
* delegated to the wrapped resource.
*/
public void touch(long m) {
t.touch(m);
}
}

private static class TouchableFileProviderMR extends FileProviderMR
implements Touchable {
private final Touchable t;

protected TouchableFileProviderMR(Resource r) {
super(r);
if (!(r instanceof Touchable)) {
throw new IllegalArgumentException("trying to wrap something "
+ "that is not a "
+ " Touchable");
}
t = (Touchable) r;
}

/**
* delegated to the wrapped resource.
*/
public void touch(long m) {
t.touch(m);
}
}

}

+ 5
- 2
src/main/org/apache/tools/ant/util/ResourceUtils.java View File

@@ -421,8 +421,11 @@ public class ResourceUtils {
FileUtils.close(in);
}
}
if (preserveLastModified && dest instanceof Touchable) {
setLastModified((Touchable) dest, source.getLastModified());
if (preserveLastModified) {
Touchable t = (Touchable) dest.as(Touchable.class);
if (t != null) {
setLastModified(t, source.getLastModified());
}
}
}
// CheckStyle:ParameterNumberCheck ON


Loading…
Cancel
Save