0
\$\begingroup\$

I am working on a Java project that currently depends on 500+ configuration files in the form of XML, XSLT and YML files. The vast majority (~400) are XSLT files, and the purpose of the application is to transform a variety of XML structures using XSLT, and currently the files are deployed as part of each deploy package we make, which makes modifying the files cumbersome. For this reason, we are in the middle of making it so that the contents of the configurations will be in-memory via a ConcurrentHashMap that lives during the entire lifetime of the application. More specifically, the contents are put into a ConcurrentHashMap<PathKey, byte[]> where PathKey is a class given by

Public class PathKey {
    private Path path;

    public PathKey(String key) {
        path = Paths.get(key).normalize();
    }

    public Path getPath() {
        return this.path;
    }

    @Override
    public boolean equals(Object pathKey) {
        if (pathKey instanceof PathKey) {
            return this.path.equals((key as PathKey).getPath());
        }
        return false;
    }
}

I discovered that the java Path API is good at converting different strings like path/to/file and path\\to/some/..\\file into the same objects (using the class construct I specified above). That is, the library both detects that I may be using wrong file separators and that I am using file system constructs like the double dot notation. Hence, we can use this ConcurrentHashMap to essentially mimic the notation and behavior of a filesystem without actually using an actual file system, and we can preserve all the existing path references that we have stored in some of our YML configuration (and which the application depends on). My question is now the following: Is the above approach considered a misuse of the Path API? In one way it feels like a convenient way of obtaining precisely what we need in an easy and low cost way; on the other hand, the Paths.get() method constructs Path objects based on the underlying file system on which the application runs - and my feeling is that it may look strange to instantiate a lot of objects representing a structure in my filesystem that may or may not exist, and which I under all circumstances do not care about; even if the PathKey led to an actual file, I would not care about the file, but rather whether it is also a key for my ConcurrentHashMap. Does anyone have anything to add to my consideration?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.