001/*
002 * Copyright 2022-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2022-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) 2022-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.ldap.sdk.unboundidds.logs.v2;
037
038
039
040import java.io.Serializable;
041
042import com.unboundid.ldap.sdk.unboundidds.logs.v2.syntax.LogFieldSyntax;
043import com.unboundid.util.InternalUseOnly;
044import com.unboundid.util.NotNull;
045import com.unboundid.util.Nullable;
046import com.unboundid.util.ThreadSafety;
047import com.unboundid.util.ThreadSafetyLevel;
048import com.unboundid.util.Validator;
049
050
051
052/**
053 * This class defines a data structure that represents a field that may appear
054 * in a Directory Server log message.
055 * <BR>
056 * <BLOCKQUOTE>
057 *   <B>NOTE:</B>  This class, and other classes within the
058 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
059 *   supported for use against Ping Identity, UnboundID, and
060 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
061 *   for proprietary functionality or for external specifications that are not
062 *   considered stable or mature enough to be guaranteed to work in an
063 *   interoperable way with other types of LDAP servers.
064 * </BLOCKQUOTE>
065 */
066@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
067public final class LogField
068       implements Serializable
069{
070  /**
071   * The serial version UID for this serializable class.
072   */
073  private static final long serialVersionUID = 6689083040240574632L;
074
075
076
077  // The expected syntax for this field.
078  @NotNull private final LogFieldSyntax<?> expectedSyntax;
079
080  // The name of the constant in which this field is defined.
081  @Nullable private final String constantName;
082
083  // The name for this field.
084  @NotNull private final String fieldName;
085
086
087
088  /**
089   * Creates a log field with whe provided information.
090   *
091   * @param  fieldName       The name for this field.  It must not be
092   *                         {@code null}.
093   * @param  expectedSyntax  The expected syntax for this field.  It must not be
094   *                         {@code null}.
095   */
096  public LogField(@NotNull final String fieldName,
097                  @NotNull final LogFieldSyntax<?> expectedSyntax)
098  {
099    this(fieldName, null, expectedSyntax);
100  }
101
102
103
104  /**
105   * Creates a log field with whe provided information.
106   *
107   * @param  fieldName       The name for this field.  It must not be
108   *                         {@code null}.
109   * @param  constantName    The name of the constant in which this field is
110   *                         defined.  This is primarily intended for internal
111   *                         use.
112   * @param  expectedSyntax  The expected syntax for this field.  It must not be
113   *                         {@code null}.
114   */
115  public LogField(@NotNull final String fieldName,
116                  @Nullable final String constantName,
117                  @NotNull final LogFieldSyntax<?> expectedSyntax)
118  {
119    Validator.ensureNotNullOrEmpty(fieldName,
120         "LogField.fieldName must not be null or empty.");
121    Validator.ensureNotNullWithMessage(expectedSyntax,
122         "LogField.expectedSyntax must not be null.");
123
124    this.fieldName = fieldName;
125    this.constantName = constantName;
126    this.expectedSyntax = expectedSyntax;
127  }
128
129
130
131  /**
132   * Retrieves the name for this field.
133   *
134   * @return  The name for this field.
135   */
136  @NotNull()
137  public String getFieldName()
138  {
139    return fieldName;
140  }
141
142
143
144  /**
145   * Retrieves the name of the constant in which this log field is defined.
146   *
147   * @return  The name of the constant in which this log field is defined, or
148   *          {@code null} if it is not defined in any constant.
149   */
150  @Nullable()
151  @InternalUseOnly()
152  public String getConstantName()
153  {
154    return constantName;
155  }
156
157
158
159  /**
160   * Retrieves the expected syntax for this field.  Note that this may be a
161   * generic instance of the associated syntax, which may or may not reflect the
162   * settings used to actually generate the log message.
163   *
164   * @return  The expected syntax for this field.
165   */
166  @NotNull()
167  public LogFieldSyntax<?> getExpectedSyntax()
168  {
169    return expectedSyntax;
170  }
171
172
173
174  /**
175   * Retrieves a string representation of this log field.
176   *
177   * @return  A string representation of this log field.
178   */
179  @Override()
180  @NotNull()
181  public String toString()
182  {
183    final StringBuilder buffer = new StringBuilder();
184    toString(buffer);
185    return buffer.toString();
186  }
187
188
189
190  /**
191   * Appends a string representation of this log field to the provided
192   * buffer.
193   *
194   * @param  buffer  The buffer to which the string representation should be
195   *                 appended.  It must not be {@code null}.
196   */
197  public void toString(@NotNull final StringBuilder buffer)
198  {
199    buffer.append("LogField(fieldName='");
200    buffer.append(fieldName);
201    buffer.append("', ");
202
203    if (constantName != null)
204    {
205      buffer.append("constantName='");
206      buffer.append(constantName);
207      buffer.append("', ");
208    }
209
210    buffer.append("expectedSyntax='");
211    buffer.append(expectedSyntax.getSyntaxName());
212    buffer.append("')");
213  }
214}