View Javadoc

1   /*
2    * LICENSE
3    *
4    * "THE BEER-WARE LICENSE" (Revision 42):
5    * "Sven Strittmatter" <ich@weltraumschaf.de> wrote this file.
6    * As long as you retain this notice you can do whatever you want with
7    * this stuff. If we meet some day, and you think this stuff is worth it,
8    * you can buy me a beer in return.
9    */
10  package org.jenkinsci.plugins.darcs.browsers;
11  
12  /**
13   * Helper class to build URL queries.
14   *
15   * Queries are constructed by partial strings combined by separator characters.
16   *
17   * @author Sven Strittmatter <ich@weltraumschaf.de>
18   */
19  final class DarcsQueryBuilder {
20  
21      /**
22       * Types for queries.
23       */
24      enum SeparatorType {
25          /**
26           * Separates everything with slash, REST like.
27           */
28          SLASHES,
29          /**
30           * Starts wit {@literal ?} and then separates with {@literal ;}.
31           */
32          SEMICOLONS,
33          /**
34           * Starts wit {@literal ?} and then separates with {@literal &}.
35           */
36          AMPERSANDS;
37      }
38  
39      /**
40       * Buffers the builded query string.
41       */
42      private final StringBuilder buf = new StringBuilder();
43      /**
44       * The separator type for the query.
45       */
46      private final SeparatorType type;
47  
48      /**
49       * Does not add a first string.
50       *
51       * @param t separator type
52       */
53      DarcsQueryBuilder(final SeparatorType t) {
54          this(t, null);
55      }
56  
57      /**
58       * Dedicated constructor.
59       *
60       * @param t separator type
61       * @param s first string of query
62       */
63      DarcsQueryBuilder(final SeparatorType t, final String s) {
64          super();
65          type = t;
66          add(s);
67      }
68  
69      /**
70       * Get the separator type.
71       *
72       * @return type of separation
73       */
74      public SeparatorType getType() {
75          return this.type;
76      }
77  
78      /**
79       * Add a string part.
80       *
81       * @param s partial string
82       * @return return itself
83       */
84      public DarcsQueryBuilder add(final String s) {
85          if (null == s) {
86              // nothing to add
87              return this;
88          }
89  
90          switch (this.type) {
91              case SLASHES:
92                  buf.append('/');
93  
94                  break;
95              case SEMICOLONS:
96                  if (buf.length() == 0) {
97                      buf.append('?');
98                  } else {
99                      buf.append(';');
100                 }
101 
102                 break;
103             case AMPERSANDS:
104                 if (buf.length() == 0) {
105                     buf.append('?');
106                 } else {
107                     buf.append('&');
108                 }
109 
110                 break;
111             default:
112                 throw new IllegalStateException(String.format("Unsupported separator type %s", type));
113         }
114 
115         buf.append(s);
116 
117         return this;
118     }
119 
120     @Override
121     public String toString() {
122         return buf.toString();
123     }
124 
125 }