-1

On occasion, as much as I try to avoid it, I do generate a fair amount of boilerplate code in my programming. I've been told that the Java Class object can be used to get around boilerplate code, but I don't currently see how.

When I say boilerplate code, I'm referring to the term used to refer to text that is reused again and again, with slight modification.

public Map<String, Boolean> loadBooleanTags(File in)
{
// Code that extracts boolean tags
}

Now, suppose, you then want to load int tags, where the files are in the exact same format, but you want the data structure to be a Map<String, Integer>. The only way I can think of handling this is like so:

public Map<String, Integer> loadIntegerTags(File in)
{
// Code that extracts integer tags
}

Essentially, I copy and past the boolean method, but I make it parse an Integer instead. What are some better ways to handle this? Ideally, I'd like to have one method that outputs a map with the correct generics.

Sal
  • 3,179
  • 7
  • 31
  • 41
  • 4
    sounds very puzzling... you do program in Java but you find yourself googling for "Java Class"? assuming it's me who is missing something, can you post a few code snippets to illustrate the problem you're dealing with? – Pavel Veller Apr 19 '12 at 02:55
  • 4
    Please define "boilerplate code" – Jim Garrison Apr 19 '12 at 02:55
  • Are you asking about reflection? – SLaks Apr 19 '12 at 03:05
  • 2
    Ask whoever told you that for clarification and references. You will never find anything useful googling for "Java Class" :) – David Apr 19 '12 at 03:35
  • The Class object is used to represent classes and interfaces in a running Java Application. It was added in Java 5, and I've honestly never used it too much. So, I wanted to know if it could help in boilerplate code, where boilerplate code is defined as text that is reused with slight modifications. I think I asked the wrong question. Feel free to downvote this question if you guys feel it's appropriate. – Sal Apr 19 '12 at 03:38
  • No. The Class object was added in Java 1.0, not Java 5. – user207421 Apr 19 '12 at 05:29

1 Answers1

3

Interesting question. First of all, Class has been around since Java 1. Second, I think this is the pattern you're looking for:

public abstract class GenericMapFactory<T>
{
    public Map<String,T> makeMap(File in) throws InstantiationException, IllegalAccessException, IOException
    {
        Map<String,T> result = new HashMap<String,T>();
        BufferedReader rdr = new BufferedReader(new FileReader(in));
        String line = null;
        while ((line=rdr.readLine()) != null)
        {
            String key  = "" /* something meaningful for your application */; 
            T      item = parse(line);
            result.put(key, item);
        }
        return result;
    }

    protected abstract T parse(String line);
}

For every variation you need you provide a specialization, for example:

public static class IntMapFactory extends GenericMapFactory<Integer>
{
    @Override
    protected Integer parse(String line)
    {
        Integer result = null; 
        // parse the line, setting the value of result
        return result;
    }
}

All the 'boilerplate' code is factored out into a generic superclass and only the type-specific code needs to be written. You use it as follows:

File in = ...
Map<String,Integer> msi = new IntMapFactory().makeMap(in);
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190