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