001/*
002 * Copyright 2016-2019 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2016-2019 Ping Identity Corporation
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU General Public License (GPLv2 only)
010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011 * as published by the Free Software Foundation.
012 *
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program; if not, see <http://www.gnu.org/licenses>.
020 */
021package com.unboundid.util.args;
022
023
024
025import java.io.Serializable;
026import java.util.Date;
027
028import com.unboundid.util.NotMutable;
029import com.unboundid.util.StaticUtils;
030import com.unboundid.util.ThreadSafety;
031import com.unboundid.util.ThreadSafetyLevel;
032
033import static com.unboundid.util.args.ArgsMessages.*;
034
035
036
037/**
038 * This class provides an implementation of an argument value validator that
039 * ensures that values must be timestamps (parsable by the
040 * {@link TimestampArgument} class) within a specified time range.
041 */
042@NotMutable()
043@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
044public final class TimestampRangeArgumentValueValidator
045       extends ArgumentValueValidator
046       implements Serializable
047{
048  /**
049   * The serial version UID for this serializable class.
050   */
051  private static final long serialVersionUID = 7248120077176469324L;
052
053
054
055  // The most recent timestamp value that will be accepted.
056  private final Date mostRecentAllowedDate;
057
058  // The oldest timestamp value that will be accepted.
059  private final Date oldestAllowedDate;
060
061
062
063  /**
064   * Creates a new validator that will ensure that timestamp values are within
065   * the specified time range.
066   *
067   * @param  oldestAllowedDate      The oldest timestamp that will be accepted
068   *                                by this validator.  It may be {@code null}
069   *                                if any timestamp older than the provided
070   *                                {@code mostRecentAllowedDate} will be
071   *                                permitted.
072   * @param  mostRecentAllowedDate  The most recent timestamp that will be
073   *                                accepted by this validator.  It may be
074   *                                {@code null} if any timestamp more recent
075   *                                than the provided {@code oldestAllowedDate}
076   *                                will be permitted.
077   */
078  public TimestampRangeArgumentValueValidator(final Date oldestAllowedDate,
079                                              final Date mostRecentAllowedDate)
080  {
081    if (oldestAllowedDate == null)
082    {
083      this.oldestAllowedDate = null;
084    }
085    else
086    {
087      this.oldestAllowedDate = oldestAllowedDate;
088    }
089
090    if (mostRecentAllowedDate == null)
091    {
092      this.mostRecentAllowedDate = null;
093    }
094    else
095    {
096      this.mostRecentAllowedDate = mostRecentAllowedDate;
097    }
098  }
099
100
101
102  /**
103   * Retrieves the oldest allowed date value that will be permitted by this
104   * validator.
105   *
106   * @return  The oldest allowed date value that will be permitted by this
107   *          validator, or {@code null} if any timestamp older than the
108   *          most recent allowed date will be permitted.
109   */
110  public Date getOldestAllowedDate()
111  {
112    return oldestAllowedDate;
113  }
114
115
116
117  /**
118   * Retrieves the most recent allowed date value that will be permitted by this
119   * validator.
120   *
121   * @return  The most recent allowed date value that will be permitted by this
122   *          validator, or {@code null} if any timestamp newer than the oldest
123   *          allowed date will be permitted.
124   */
125  public Date getMostRecentAllowedDate()
126  {
127    return mostRecentAllowedDate;
128  }
129
130
131
132  /**
133   * {@inheritDoc}
134   */
135  @Override()
136  public void validateArgumentValue(final Argument argument,
137                                    final String valueString)
138         throws ArgumentException
139  {
140    // Ensure that the value can be parsed as a valid timestamp.
141    final Date parsedDate;
142    try
143    {
144      parsedDate = TimestampArgument.parseTimestamp(valueString);
145    }
146    catch (final Exception e)
147    {
148      throw new ArgumentException(
149           ERR_TIMESTAMP_VALUE_NOT_TIMESTAMP.get(valueString,
150                argument.getIdentifierString()),
151           e);
152    }
153
154    final long parsedTime = parsedDate.getTime();
155    if ((oldestAllowedDate != null) &&
156        (parsedTime < oldestAllowedDate.getTime()))
157    {
158      throw new ArgumentException(ERR_TIMESTAMP_RANGE_VALIDATOR_TOO_OLD.get(
159           valueString, argument.getIdentifierString(),
160           StaticUtils.encodeGeneralizedTime(oldestAllowedDate)));
161    }
162
163    if ((mostRecentAllowedDate != null) &&
164        (parsedTime > mostRecentAllowedDate.getTime()))
165    {
166      throw new ArgumentException(ERR_TIMESTAMP_RANGE_VALIDATOR_TOO_NEW.get(
167           valueString, argument.getIdentifierString(),
168           StaticUtils.encodeGeneralizedTime(mostRecentAllowedDate)));
169    }
170  }
171
172
173
174  /**
175   * Retrieves a string representation of this argument value validator.
176   *
177   * @return  A string representation of this argument value validator.
178   */
179  @Override()
180  public String toString()
181  {
182    final StringBuilder buffer = new StringBuilder();
183    toString(buffer);
184    return buffer.toString();
185  }
186
187
188
189  /**
190   * Appends a string representation of this argument value validator to the
191   * provided buffer.
192   *
193   * @param  buffer  The buffer to which the string representation should be
194   *                 appended.
195   */
196  public void toString(final StringBuilder buffer)
197  {
198    buffer.append("TimestampRangeArgumentValueValidator(");
199
200    if (oldestAllowedDate != null)
201    {
202      buffer.append("oldestAllowedDate='");
203      buffer.append(StaticUtils.encodeGeneralizedTime(oldestAllowedDate));
204      buffer.append('\'');
205
206      if (mostRecentAllowedDate != null)
207      {
208        buffer.append(", mostRecentAllowedDate='");
209        buffer.append(StaticUtils.encodeGeneralizedTime(mostRecentAllowedDate));
210        buffer.append('\'');
211      }
212    }
213    else if (mostRecentAllowedDate != null)
214    {
215      buffer.append("mostRecentAllowedDate='");
216      buffer.append(StaticUtils.encodeGeneralizedTime(mostRecentAllowedDate));
217      buffer.append('\'');
218    }
219
220    buffer.append(')');
221  }
222}