001/*
002 * Copyright 2009-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2009-2024 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-2024 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
040/**
041 * This enumeration defines a set of values that may indicate how text should be
042 * horizontally aligned.
043 */
044@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
045public enum HorizontalAlignment
046{
047  /**
048   * Indicates that items should be aligned along their left edges.
049   */
050  LEFT(),
051
052
053
054  /**
055   * Indicates that text should be aligned along their centers.
056   */
057  CENTER(),
058
059
060
061  /**
062   * Indicates that text should be aligned along right edges.
063   */
064  RIGHT();
065
066
067
068  /**
069   * Appends the provided string to the given buffer, aligned properly within
070   * the specified width.  Spaces will be inserted before and/or after the text
071   * as necessary to achieve the desired alignment.  This method will always
072   * append exactly {@code width} characters (including spaces added to achieve
073   * the desired alignment) to the provided buffer.  If the given text is longer
074   * than {@code width}, then only the first {@code width} characters of the
075   * provided text will be appended.
076   *
077   * @param  buffer  The buffer to which the formatted text should be appended.
078   *                 It must not be {@code null}.
079   * @param  text    The text to be added to the provided buffer, with
080   *                 additional spaces as necessary to achieve the desired
081   *                 width.  It must not be {@code null}.
082   * @param  width   The number of characters to append to the provided buffer.
083   *                 It must be greater than or equal to 1.
084   */
085  public void format(@NotNull final StringBuilder buffer,
086                     @NotNull final String text, final int width)
087  {
088    final int length = text.length();
089    if (length >= width)
090    {
091      buffer.append(text.substring(0, width));
092      return;
093    }
094
095    final int spacesBefore;
096    final int spacesAfter;
097    switch (this)
098    {
099      case LEFT:
100        spacesBefore = 0;
101        spacesAfter  = width - length;
102        break;
103      case CENTER:
104        final int totalSpaces = width - length;
105        spacesBefore = totalSpaces / 2;
106        spacesAfter  = totalSpaces - spacesBefore;
107        break;
108      case RIGHT:
109      default:
110        spacesBefore = width - length;
111        spacesAfter  = 0;
112        break;
113    }
114
115    for (int i=0; i < spacesBefore; i++)
116    {
117      buffer.append(' ');
118    }
119
120    buffer.append(text);
121
122    for (int i=0; i < spacesAfter; i++)
123    {
124      buffer.append(' ');
125    }
126  }
127
128
129
130  /**
131   * Retrieves the horizontal alignment value with the specified name.
132   *
133   * @param  name  The name of the horizontal alignment value to retrieve.  It
134   *               must not be {@code null}.
135   *
136   * @return  The requested horizontal alignment value, or {@code null} if no
137   *          such value is defined.
138   */
139  @Nullable()
140  public static HorizontalAlignment forName(@NotNull final String name)
141  {
142    switch (StaticUtils.toLowerCase(name))
143    {
144      case "left":
145        return LEFT;
146      case "center":
147        return CENTER;
148      case "right":
149        return RIGHT;
150      default:
151        return null;
152    }
153  }
154}