Hello,

i defined the following interface:

@ImplementedBy(FileSystem.class)
public interface IFileSystem {
public String getFileContent(String filepath) throws IOException;
public long getLastModified(String filepath) throws IOException;
}

and implementing class:

@Singleton
public class FileReader implements IFileReader {    
@Override
public String getFileContent(String filepath) throws IOException {
return FileUtils.readFileToString(new File(filepath));
}

@Override
public String getLastModified(String filepath) throws IOException {
return (new File(filepath)).lastModified();
}
}

This should encapsulate these file operations on the harddisk. And I'm also 
able to mock this interface while testing my classes.
Now i would like to provide a CachedFileReader implementing which should 
cache any file *(I keep the example as small as possible without take note 
on file changes or anything else)*:

@Singleton
public class CachedFileReader implements IFileReader {
private final HashMap<String, String> cache = new HashMap<String, String> 
();
private final IFileReader filereader;
    
@Override
public String getFileContent(String filepath) throws IOException {
String key = filepath + String.valueOf(getLastModified(filepath)); // I 
know this is a crap solution, its just for this example

if (!(cache.containsKey(filepath)) {
cache.put(filepath, filereader.getFileContent(filepath));
}
return cache.get(filepath);
}

@Override
public String getLastModified(String filepath) throws IOException {
return filereader.getLastModified(filepath);
}

@Inject
public CachedFileReader(IFileReader filereader) {
this.filereader = filereader;
}
}

I haven't inherited from FileReader because I want later be able to test 
the CachedFileReader itself with a IFileReader mock object passed to the 
constructor.
Also I changed the @ImplementedBy Annotation of my IFileReader interface to 
CachedFileReader. So every class which needs a IFileReader now gets the 
CachedFileReader instance.

But how can I tell Google Juice tell that it should inject the normal 
FileReader to the constructor of the CachedFileReader instead of itself? Is 
there an annotation for that? I have read about multibindings but it didn't 
helped me. Or is there a better solution for my concern?

Thank you in advance,
Dirk

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/google-guice/-/Q0mkVSYi3RsJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en.

Reply via email to