001// Copyright 2009, 2012 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.internal.translator;
016
017import org.apache.tapestry5.dom.Element;
018
019import java.text.ParseException;
020
021/**
022 * Used to generate the client-side JSON specification for how a number-based validator operates. Uses {@link
023 * org.apache.tapestry5.ioc.services.ThreadLocale} to determine the locale for any locale-specific operations.
024 *
025 * @since 5.1.0.1
026 */
027public interface NumericTranslatorSupport
028{
029    /**
030     * Parses a client-submitted value in a localized manner.
031     *
032     * @param type        desired type of value
033     * @param clientValue value from client; this will be trimmed of leading/trailing whitespace
034     * @param <T>
035     * @return the parsed value
036     * @throws ParseException
037     * @see org.apache.tapestry5.Translator#parseClient(org.apache.tapestry5.Field, String, String)
038     */
039    <T extends Number> T parseClient(Class<T> type, String clientValue) throws ParseException;
040
041    /**
042     * Converts a server-side value to a client-side string. Integer types are formatted simply; decimal types may be
043     * formatted using thousands-seperator commas.
044     *
045     * @param type  type of value to convert
046     * @param value current (non-null) value
047     * @param <T>
048     * @return value formatted
049     */
050    <T extends Number> String toClient(Class<T> type, T value);
051
052    /**
053     * Returns the default message key for parse failures for the indicated type.
054     *
055     * @param type
056     * @param <T>
057     * @return a message key: either "integer-format-exception" or "number-format-exception"
058     */
059    <T extends Number> String getMessageKey(Class<T> type);
060
061    /**
062     * Adds client-side format validation for the field, appropriate to the indicated type.
063     *
064     * @param type    value type
065     * @param element
066     * @param message message if the client-side value can't be parsed as a number
067     */
068    <T extends Number> void setupTranslation(Class<T> type, Element element, String message);
069}