001/*
002 * Copyright 2009-2020 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2009-2020 Ping Identity Corporation
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *    http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020/*
021 * Copyright (C) 2009-2020 Ping Identity Corporation
022 *
023 * This program is free software; you can redistribute it and/or modify
024 * it under the terms of the GNU General Public License (GPLv2 only)
025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
026 * as published by the Free Software Foundation.
027 *
028 * This program is distributed in the hope that it will be useful,
029 * but WITHOUT ANY WARRANTY; without even the implied warranty of
030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
031 * GNU General Public License for more details.
032 *
033 * You should have received a copy of the GNU General Public License
034 * along with this program; if not, see <http://www.gnu.org/licenses>.
035 */
036package com.unboundid.util;
037
038
039
040import java.io.Serializable;
041
042
043
044/**
045 * This class provides a data structure with information about a column to use
046 * with the {@link ColumnFormatter}.
047 */
048@NotMutable()
049@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
050public final class FormattableColumn
051       implements Serializable
052{
053  /**
054   * The serial version UID for this serializable class.
055   */
056  private static final long serialVersionUID = -67186391702592665L;
057
058
059
060  // The alignment for this column.
061  private final HorizontalAlignment alignment;
062
063  // The width for this column.
064  private final int width;
065
066  // The lines that comprise the heading label for this column.
067  private final String[] labelLines;
068
069
070
071  /**
072   * Creates a new formattable column with the provided information.
073   *
074   * @param  width       The width to use for this column.  It must be greater
075   *                     than or equal to 1.
076   * @param  alignment   The alignment to use for this column.  It must not be
077   *                     {@code null}.
078   * @param  labelLines  The lines to use as the label for this column.  It must
079   *                     not be {@code null}.
080   */
081  public FormattableColumn(final int width, final HorizontalAlignment alignment,
082                           final String... labelLines)
083  {
084    Validator.ensureTrue(width >= 1);
085    Validator.ensureNotNull(alignment, labelLines);
086
087    this.width      = width;
088    this.alignment  = alignment;
089    this.labelLines = labelLines;
090  }
091
092
093
094  /**
095   * Retrieves the width for this column.
096   *
097   * @return  The width for this column.
098   */
099  public int getWidth()
100  {
101    return width;
102  }
103
104
105
106  /**
107   * Retrieves the alignment for this column.
108   *
109   * @return  The alignment for this column.
110   */
111  public HorizontalAlignment getAlignment()
112  {
113    return alignment;
114  }
115
116
117
118  /**
119   * Retrieves the lines to use as the label for this column.
120   *
121   * @return  The lines to use as the label for this column.
122   */
123  public String[] getLabelLines()
124  {
125    return labelLines;
126  }
127
128
129
130  /**
131   * Retrieves a single-line representation of the label.  If there are multiple
132   * header lines, then they will be concatenated and separated by a space.
133   *
134   * @return  A single-line representation of the label.
135   */
136  public String getSingleLabelLine()
137  {
138    switch (labelLines.length)
139    {
140      case 0:
141        return "";
142      case 1:
143        return labelLines[0];
144      default:
145        final StringBuilder buffer = new StringBuilder();
146        buffer.append(labelLines[0]);
147        for (int i=1; i < labelLines.length; i++)
148        {
149          buffer.append(' ');
150          buffer.append(labelLines[i]);
151        }
152        return buffer.toString();
153    }
154  }
155
156
157
158  /**
159   * Appends a formatted representation of the provided text to the given
160   * buffer.
161   *
162   * @param  buffer  The buffer to which the text should be appended.  It must
163   *                 not be {@code null}.
164   * @param  text    The text to append to the buffer.  It must not be
165   *                 {@code null}.
166   * @param  format  The format to use for the text.  It must not be
167   *                 {@code null}.
168   */
169  public void format(final StringBuilder buffer, final String text,
170                     final OutputFormat format)
171  {
172    switch (format)
173    {
174      case TAB_DELIMITED_TEXT:
175        buffer.append(text);
176        break;
177
178      case CSV:
179        boolean quotesNeeded = false;
180        final int length = text.length();
181        final int startPos = buffer.length();
182        for (int i=0; i < length; i++)
183        {
184          final char c = text.charAt(i);
185          if (c == ',')
186          {
187            buffer.append(',');
188            quotesNeeded = true;
189          }
190          else if (c == '"')
191          {
192            buffer.append("\"\"");
193            quotesNeeded = true;
194          }
195          else if ((c >= ' ') && (c <= '~'))
196          {
197            buffer.append(c);
198          }
199        }
200
201        if (quotesNeeded)
202        {
203          buffer.insert(startPos, '"');
204          buffer.append('"');
205        }
206        break;
207
208      case COLUMNS:
209        alignment.format(buffer, text, width);
210        break;
211    }
212  }
213
214
215
216  /**
217   * Retrieves a string representation of this formattable column.
218   *
219   * @return  A string representation of this formattable column.
220   */
221  @Override()
222  public String toString()
223  {
224    final StringBuilder buffer = new StringBuilder();
225    toString(buffer);
226    return buffer.toString();
227  }
228
229
230
231  /**
232   * Appends a string representation of this formattable column to the provided
233   * buffer.
234   *
235   * @param  buffer  The buffer to which the string representation should be
236   *                 appended.
237   */
238  public void toString(final StringBuilder buffer)
239  {
240    buffer.append("FormattableColumn(width=");
241    buffer.append(width);
242    buffer.append(", alignment=");
243    buffer.append(alignment);
244    buffer.append(", label=\"");
245    buffer.append(getSingleLabelLine());
246    buffer.append("\")");
247  }
248}