ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nicolas Lalevée <nicolas.lale...@hibnet.org>
Subject Re: svn commit: r714053 - /ant/core/trunk/src/main/org/apache/tools/ant/types/resources/MappedResource.java
Date Fri, 14 Nov 2008 19:01:52 GMT
I am not sure of how are used the "Resource" in the Ant code, but it  
makes me think of the Eclipse adapters (probably a design pattern ?):
http://www.jeffreyricker.com/papers/Eclipse-Adapters.pdf

Though it changes the tests of the appendability, the touchability,  
etc... of a Resource in the ant code, the "instanceof+cast" would be  
replaced by some "getAdapter". This solution is not very self- 
contained...

But I see another solution, quite different. Probably most of the work  
should be done in the Resource class:

interface NameMapper {
   String getName();
}

public class Resource extends DataType implements .....
   NameMapper nameMapper = new NameMapper() {
     return isReference() ? ((Resource) getCheckedRef()).getName() :  
name;
   }
   public String getName() {
     return nameMapper.getName();
   }
}

And then:

public static Resource map(Resource r) {
   r. nameMapper = new NameMapper() {
     return doMapName();
   }
}


hoping it helps,
Nicolas



Le 14 nov. 08 à 17:29, Stefan Bodewig a écrit :

> I need help!
>
> I'm trying to write a mapped-resource that is a decorator for a
> different resource and changes the name of the decorated resource
> using a mapper.
>
> Resource itself is a class, so my base class is set.  There are
> several "optional" behaviors available via interfaces, namely
> Touchable, Appendable and FileProvider (I hope I didn't miss one).
>
> Our tasks use instanceof checks in several places, so if the mapped
> resource wraps a FileResource it should also implement all three of
> those interfaces.  OTOH it must not implement any of these interfaces
> if wrapping a StringResource.
>
> Any attempt of using anonymous subclasses of MappedResource or
> reflection proxies leads to objects that either no longer extend
> Resource or contain the methods required by an interface but don't
> implement it.
>
> The current solution is really really ugly (seven subclasses with many
> of them identical except for the class they extend):
>
> On 2008-11-14, <bodewig@apache.org> wrote:
>
>>>   public static MappedResource map(Resource r) {
>>>       if (r instanceof FileProvider) {
>>>           if (r instanceof Touchable) {
>>>               if (r instanceof Appendable) {
>>>                   // most probably FileResource
>>>                   return new AppendableTouchableFileProviderMR(r);
>>>               }
>>>               return new TouchableFileProviderMR(r);
>>>           }
>>>           if (r instanceof Appendable) {
>>>               return new AppendableFileProviderMR(r);
>>>           }
>>>           return new FileProviderMR(r);
>>>       }
>>>       if (r instanceof Touchable) {
>>>           if (r instanceof Appendable) {
>>>               return new AppendableTouchableMR(r);
>>>           }
>>>           return new TouchableMR(r);
>>>       }
>>>       if (r instanceof Appendable) {
>>>           return new AppendableMR(r);
>>>       }
>>>       // no special interface
>>>       return new MappedResource(r);
>>>   }
>
> The other approach I could take is to clone the wrapped resource (all
> our resources are cloneable) and change the clone's name - and not
> have a decorator at all.
>
> The main problem with this approach is that some resources are
> immutable and will throw an exception in setName (StringResource for
> example) while others will not do what I want - FileResource will
> simply ignore setName().
>
> Any other ideas (apart from not using Java?)
>
> Stefan
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> For additional commands, e-mail: dev-help@ant.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Mime
View raw message