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 join(String sep, String[] list) {
131    CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder(sep);
132    for (String s : list) {
133      b.append(s);
134    }
135    return b.toString();
136  }
137
138  public static String build(List<String> list) {
139    CommaSeparatedStringBuilder self = new CommaSeparatedStringBuilder();
140    for (String s : list) {
141      self.append(s);
142    }
143    return self.toString();
144  }
145
146
147  public static String buildObjects(List<? extends Object> list) {
148    CommaSeparatedStringBuilder self = new CommaSeparatedStringBuilder();
149    for (Object s : list) {
150      self.append(s.toString());
151    }
152    return self.toString();
153  }
154  
155  public static Set<String> toSet(String source) {
156    if (source == null) {
157      return null;
158    }
159    Set<String> res = new HashSet<>();
160    if (!Utilities.noString(source)) {
161      for (String s : source.split("\\,")) {
162        res.add(s.trim());
163      }
164    }
165    return res;
166  }
167
168  public static String joinWrapped(String sep, String leftWrap, String rightWrap, Collection<String> list) {
169    CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder(sep);
170    for (String s : list) {
171      if (s != null) {
172        b.append(leftWrap+s+rightWrap);
173      }
174    }
175    return b.toString();
176  }
177
178  public static String join(String sep, EnumSet<? extends Enum> set) {
179    CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder(sep);
180    for (Enum e : set) {
181      b.append(e.toString());
182    }
183    return b.toString();
184  }
185}