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