001package org.hl7.fhir.utilities;
002
003import java.util.Collection;
004import java.util.EnumSet;
005import java.util.List;
006import java.util.Set;
007
008
009import java.util.ArrayList;
010import java.util.HashSet;
011
012/*
013  Copyright (c) 2011+, HL7, Inc.
014  All rights reserved.
015  
016  Redistribution and use in source and binary forms, with or without modification, 
017  are permitted provided that the following conditions are met:
018    
019   * Redistributions of source code must retain the above copyright notice, this 
020     list of conditions and the following disclaimer.
021   * Redistributions in binary form must reproduce the above copyright notice, 
022     this list of conditions and the following disclaimer in the documentation 
023     and/or other materials provided with the distribution.
024   * Neither the name of HL7 nor the names of its contributors may be used to 
025     endorse or promote products derived from this software without specific 
026     prior written permission.
027  
028  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
029  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
030  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
031  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
032  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
033  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
034  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
035  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
036  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
037  POSSIBILITY OF SUCH DAMAGE.
038  
039 */
040
041
042
043/**
044 * Encapsulates StringBuilder to build strings of values separated by comma
045 * @author Ewout
046 */
047
048public class CommaSeparatedStringBuilder {
049
050  boolean first = true;
051  String sep = ", ";
052  StringBuilder b = new StringBuilder();
053  int count = 0;
054  String pending = null;
055  private String lastSep;
056
057  public CommaSeparatedStringBuilder() {
058    this.sep = ", ";
059    this.lastSep = ", ";
060  }
061  
062  public CommaSeparatedStringBuilder(String sep) {
063    this.sep = sep;
064    this.lastSep = sep;
065  }
066
067  public CommaSeparatedStringBuilder(String sep, String lastSep) {
068    this.sep = sep;
069    this.lastSep = lastSep;
070  }
071
072  private void commit(boolean last) {
073    if (pending != null) {
074      if (!first) {
075        if (last) {
076          b.append(lastSep);
077        } else {
078          b.append(sep);
079        }
080      }
081      b.append(pending);
082      pending = null;
083      first = false;  
084    }
085  }
086  public void append(String value) {
087    commit(false);
088    pending = value;
089    count++;    
090  }
091  
092  public int length() {
093    commit(false);
094    return b.length();
095  }
096  
097  public int count() {
098    return count;
099  }
100
101  @Override
102  public String toString() {
103    commit(true);
104    return b.toString();
105  }
106
107  public void appendIfNotNull(String s) {
108   if (!Utilities.noString(s))
109     append(s);
110    
111  }
112
113  public void addAll(List<String> list) {
114    for (String s : list) {
115      appendIfNotNull(s);
116    }
117    
118  }
119
120  public static String join(String sep, Collection<String> list) {
121    CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder(sep);
122    for (String s : list) {
123      if (s != null) {
124        b.append(s);
125      }
126    }
127    return b.toString();
128  }
129  
130  public static String join2(String sep, String finalSep, Collection<String> list) {
131    CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder(sep, finalSep);
132    for (String s : list) {
133      if (s != null) {
134        b.append(s);
135      }
136    }
137    return b.toString();
138  }
139
140  public static String joinToLimit(String sep, int limit, String overflow, Collection<String> list) {
141    CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder(sep);
142    int i = 0;
143    for (String s : list) {
144      if (s != null) {
145        i++;
146        if (i == limit) {
147          b.append(overflow);
148          break;
149        } else {
150          b.append(s);
151        }
152      }
153    }
154    return b.toString();
155  }
156
157  public static String join(String sep, String[] list) {
158    CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder(sep);
159    for (String s : list) {
160      b.append(s);
161    }
162    return b.toString();
163  }
164
165  public static String build(List<String> list) {
166    CommaSeparatedStringBuilder self = new CommaSeparatedStringBuilder();
167    for (String s : list) {
168      self.append(s);
169    }
170    return self.toString();
171  }
172
173
174  public static String buildObjects(List<? extends Object> list) {
175    CommaSeparatedStringBuilder self = new CommaSeparatedStringBuilder();
176    for (Object s : list) {
177      self.append(s.toString());
178    }
179    return self.toString();
180  }
181  
182  public static Set<String> toSet(String source) {
183    if (source == null) {
184      return null;
185    }
186    Set<String> res = new HashSet<>();
187    if (!Utilities.noString(source)) {
188      for (String s : source.split("\\,")) {
189        res.add(s.trim());
190      }
191    }
192    return res;
193  }
194
195  public static String joinWrapped(String sep, String leftWrap, String rightWrap, Collection<String> list) {
196    CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder(sep);
197    for (String s : list) {
198      if (s != null) {
199        b.append(leftWrap+s+rightWrap);
200      }
201    }
202    return b.toString();
203  }
204
205  public static String join(String sep, EnumSet<? extends Enum> set) {
206    CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder(sep);
207    for (Enum e : set) {
208      b.append(e.toString());
209    }
210    return b.toString();
211  }
212}