View Javadoc

1   package net.sourceforge.argval.version;
2   
3   
4   import net.sourceforge.argval.collection.CollectionUtilConfig;
5   import net.sourceforge.argval.collection.CollectionUtilConfigFactory;
6   
7   
8   /**
9    * A text based implementation of the {@link VersionNumberVisitor} interface.
10   * <p>
11   * Use the {@link #getAsText()} for retrieving the result.
12   * 
13   * @author <a  href="http://sourceforge.net/users/verhagent/">T. Verhagen</a>
14   */
15  public class SimpleVersionNumberVisitor implements VersionNumberVisitor {
16      private StringBuilder builder = new StringBuilder();
17      private final String separator;
18      private final CollectionUtilConfig config;
19      
20      public SimpleVersionNumberVisitor() {
21          this(VersionHierarchy.VERSION_PART_SEPARATOR);
22      }
23  
24      public SimpleVersionNumberVisitor(final String separator) {
25          this(separator, null);
26      }
27  
28      public SimpleVersionNumberVisitor(final String separator, CollectionUtilConfig config) {
29          super();
30          this.separator = (separator != null) 
31                  ? separator : VersionHierarchy.VERSION_PART_SEPARATOR;
32          this.config = (config != null) ? config 
33                  : new CollectionUtilConfigFactory().getConfig(CollectionUtilConfigFactory.CONFIG_LIST);
34      }
35  
36      
37      public void visit(VersionNumber versionNumber) {
38          if (builder.length() > 0)
39          {
40              builder.append(config.getArrayValueSeparator());
41          }
42          builder.append(config.getArrayValuePrefix());
43          boolean isBeginOfVersion = true;
44          for (String part : versionNumber.getPartitionedVersion()) {
45              if (isBeginOfVersion) {
46                  isBeginOfVersion = false;
47              }
48              else {
49                  builder.append(this.separator);
50              }
51              builder.append(part);
52          }
53          builder.append(config.getArrayValuePostfix());
54      }
55      
56      public String getAsText() {
57          StringBuilder builder = new StringBuilder();
58          builder.append(config.getArrayPrefix());
59          builder.append(this.builder);
60          builder.append(config.getArrayPostfix());
61          return builder.toString();
62      }
63      
64  }