1cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust/**
2cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust * Copyright (C) 2011 the original author or authors.
3cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust * See the notice.md file distributed with this work for additional
4cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust * information regarding copyright ownership.
5cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust *
6cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust * Licensed under the Apache License, Version 2.0 (the "License");
7cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust * you may not use this file except in compliance with the License.
8cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust * You may obtain a copy of the License at
9cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust *
10cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust *     http://www.apache.org/licenses/LICENSE-2.0
11cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust *
12cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust * Unless required by applicable law or agreed to in writing, software
13cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust * distributed under the License is distributed on an "AS IS" BASIS,
14cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust * See the License for the specific language governing permissions and
16cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust * limitations under the License.
17cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust */
18cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust
19cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beustpackage com.beust.jcommander.validators;
20cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust
21cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beustimport com.beust.jcommander.IParameterValidator;
22cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beustimport com.beust.jcommander.ParameterException;
23cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust
24cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust/**
25cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust * A validator that makes sure the value of the parameter is a positive integer.
26cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust *
27cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust * @author Cedric Beust <cedric@beust.com>
28cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust */
29cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beustpublic class PositiveInteger implements IParameterValidator {
30cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust
31cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust  public void validate(String name, String value)
32cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust      throws ParameterException {
33cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust    int n = Integer.parseInt(value);
34cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust    if (n < 0) {
35cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust      throw new ParameterException("Parameter " + name
36cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust          + " should be positive (found " + value +")");
37cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust    }
38cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust  }
39cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust
40cfbeb8905f1166e5fd03624a34571d6ae5ab321dCedric Beust}
41