001/*
002 * Copyright 2016-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2016-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) 2016-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.args;
037
038
039
040import java.io.Serializable;
041import java.util.Date;
042
043import com.unboundid.util.NotMutable;
044import com.unboundid.util.NotNull;
045import com.unboundid.util.Nullable;
046import com.unboundid.util.StaticUtils;
047import com.unboundid.util.ThreadSafety;
048import com.unboundid.util.ThreadSafetyLevel;
049
050import static com.unboundid.util.args.ArgsMessages.*;
051
052
053
054/**
055 * This class provides an implementation of an argument value validator that
056 * ensures that values must be timestamps (parsable by the
057 * {@link TimestampArgument} class) within a specified time range.
058 */
059@NotMutable()
060@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
061public final class TimestampRangeArgumentValueValidator
062       extends ArgumentValueValidator
063       implements Serializable
064{
065  /**
066   * The serial version UID for this serializable class.
067   */
068  private static final long serialVersionUID = 7248120077176469324L;
069
070
071
072  // The most recent timestamp value that will be accepted.
073  @Nullable private final Date mostRecentAllowedDate;
074
075  // The oldest timestamp value that will be accepted.
076  @Nullable private final Date oldestAllowedDate;
077
078
079
080  /**
081   * Creates a new validator that will ensure that timestamp values are within
082   * the specified time range.
083   *
084   * @param  oldestAllowedDate      The oldest timestamp that will be accepted
085   *                                by this validator.  It may be {@code null}
086   *                                if any timestamp older than the provided
087   *                                {@code mostRecentAllowedDate} will be
088   *                                permitted.
089   * @param  mostRecentAllowedDate  The most recent timestamp that will be
090   *                                accepted by this validator.  It may be
091   *                                {@code null} if any timestamp more recent
092   *                                than the provided {@code oldestAllowedDate}
093   *                                will be permitted.
094   */
095  public TimestampRangeArgumentValueValidator(
096              @Nullable final Date oldestAllowedDate,
097              @Nullable final Date mostRecentAllowedDate)
098  {
099    this.oldestAllowedDate = oldestAllowedDate;
100    this.mostRecentAllowedDate = mostRecentAllowedDate;
101  }
102
103
104
105  /**
106   * Retrieves the oldest allowed date value that will be permitted by this
107   * validator.
108   *
109   * @return  The oldest allowed date value that will be permitted by this
110   *          validator, or {@code null} if any timestamp older than the
111   *          most recent allowed date will be permitted.
112   */
113  @Nullable()
114  public Date getOldestAllowedDate()
115  {
116    return oldestAllowedDate;
117  }
118
119
120
121  /**
122   * Retrieves the most recent allowed date value that will be permitted by this
123   * validator.
124   *
125   * @return  The most recent allowed date value that will be permitted by this
126   *          validator, or {@code null} if any timestamp newer than the oldest
127   *          allowed date will be permitted.
128   */
129  @Nullable()
130  public Date getMostRecentAllowedDate()
131  {
132    return mostRecentAllowedDate;
133  }
134
135
136
137  /**
138   * {@inheritDoc}
139   */
140  @Override()
141  public void validateArgumentValue(@NotNull final Argument argument,
142                                    @NotNull final String valueString)
143         throws ArgumentException
144  {
145    // Ensure that the value can be parsed as a valid timestamp.
146    final Date parsedDate;
147    try
148    {
149      parsedDate = TimestampArgument.parseTimestamp(valueString);
150    }
151    catch (final Exception e)
152    {
153      throw new ArgumentException(
154           ERR_TIMESTAMP_VALUE_NOT_TIMESTAMP.get(valueString,
155                argument.getIdentifierString()),
156           e);
157    }
158
159    final long parsedTime = parsedDate.getTime();
160    if ((oldestAllowedDate != null) &&
161        (parsedTime < oldestAllowedDate.getTime()))
162    {
163      throw new ArgumentException(ERR_TIMESTAMP_RANGE_VALIDATOR_TOO_OLD.get(
164           valueString, argument.getIdentifierString(),
165           StaticUtils.encodeGeneralizedTime(oldestAllowedDate)));
166    }
167
168    if ((mostRecentAllowedDate != null) &&
169        (parsedTime > mostRecentAllowedDate.getTime()))
170    {
171      throw new ArgumentException(ERR_TIMESTAMP_RANGE_VALIDATOR_TOO_NEW.get(
172           valueString, argument.getIdentifierString(),
173           StaticUtils.encodeGeneralizedTime(mostRecentAllowedDate)));
174    }
175  }
176
177
178
179  /**
180   * Retrieves a string representation of this argument value validator.
181   *
182   * @return  A string representation of this argument value validator.
183   */
184  @Override()
185  @NotNull()
186  public String toString()
187  {
188    final StringBuilder buffer = new StringBuilder();
189    toString(buffer);
190    return buffer.toString();
191  }
192
193
194
195  /**
196   * Appends a string representation of this argument value validator to the
197   * provided buffer.
198   *
199   * @param  buffer  The buffer to which the string representation should be
200   *                 appended.
201   */
202  public void toString(@NotNull final StringBuilder buffer)
203  {
204    buffer.append("TimestampRangeArgumentValueValidator(");
205
206    if (oldestAllowedDate != null)
207    {
208      buffer.append("oldestAllowedDate='");
209      buffer.append(StaticUtils.encodeGeneralizedTime(oldestAllowedDate));
210      buffer.append('\'');
211
212      if (mostRecentAllowedDate != null)
213      {
214        buffer.append(", mostRecentAllowedDate='");
215        buffer.append(StaticUtils.encodeGeneralizedTime(mostRecentAllowedDate));
216        buffer.append('\'');
217      }
218    }
219    else if (mostRecentAllowedDate != null)
220    {
221      buffer.append("mostRecentAllowedDate='");
222      buffer.append(StaticUtils.encodeGeneralizedTime(mostRecentAllowedDate));
223      buffer.append('\'');
224    }
225
226    buffer.append(')');
227  }
228}