001package org.gwtbootstrap3.client.ui.form.validator; 002 003import org.gwtbootstrap3.client.ui.form.validator.ValidationMessages.Keys; 004 005/* 006 * #%L 007 * GwtBootstrap3 008 * %% 009 * Copyright (C) 2015 GwtBootstrap3 010 * %% 011 * Licensed under the Apache License, Version 2.0 (the "License"); 012 * you may not use this file except in compliance with the License. 013 * You may obtain a copy of the License at 014 * 015 * http://www.apache.org/licenses/LICENSE-2.0 016 * 017 * Unless required by applicable law or agreed to in writing, software 018 * distributed under the License is distributed on an "AS IS" BASIS, 019 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 020 * See the License for the specific language governing permissions and 021 * limitations under the License. 022 * #L% 023 */ 024 025/** 026 * Decimal max validator. Checks to see if the decimal value is over the maximum value. 027 * 028 * @param <T> the generic type 029 * @author Steven Jardine 030 */ 031public class DecimalMaxValidator<T> extends AbstractValidator<T> { 032 033 private Number maxValue; 034 035 /** 036 * Constructor. 037 * 038 * @param maxValue the max value 039 */ 040 public DecimalMaxValidator(Number maxValue) { 041 super(Keys.DECIMAL_MAX, new Object[] { maxValue.toString() }); 042 this.maxValue = maxValue; 043 } 044 045 /** 046 * Constructor. 047 * 048 * @param maxValue the max value 049 * @param invalidMessageOverride the invalid message override 050 */ 051 public DecimalMaxValidator(Number maxValue, String invalidMessageOverride) { 052 super(invalidMessageOverride); 053 this.maxValue = maxValue; 054 } 055 056 /** {@inheritDoc} */ 057 @Override 058 public int getPriority() { 059 return Priority.MEDIUM; 060 } 061 062 /** {@inheritDoc} */ 063 @Override 064 public boolean isValid(T value) { 065 if (value == null) { return true; } 066 if (value instanceof Number) { 067 return ((Number) value).doubleValue() <= maxValue.doubleValue(); 068 } else { 069 return Double.valueOf(value.toString()).doubleValue() <= maxValue.doubleValue(); 070 } 071 } 072 073}