001    // Copyright 2004, 2005 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    
015    package org.apache.hivemind.schema.rules;
016    
017    import java.util.Map;
018    
019    import org.apache.hivemind.ApplicationRuntimeException;
020    import org.apache.hivemind.HiveMind;
021    import org.apache.hivemind.Location;
022    import org.apache.hivemind.internal.Module;
023    import org.apache.hivemind.schema.Translator;
024    
025    /**
026     * Translates strings to long values.
027     *
028     * @author Howard Lewis Ship
029     */
030    public class LongTranslator implements Translator
031    {
032        private long _minValue;
033        private boolean _isMinValue;
034        private long _maxValue;
035        private boolean _isMaxValue;
036        private long _defaultValue = 0;
037    
038        public LongTranslator()
039        {
040        }
041    
042        /**
043         * Initializers:
044         * <ul>
045         * <li>default: default value for empty or invalid input
046         * <li>min: minimum acceptible value
047         * <li>max: maximum acceptible value
048         * </ul>
049         */
050        public LongTranslator(String initializer)
051        {
052            Map m = RuleUtils.convertInitializer(initializer);
053    
054            String defaultInit = (String) m.get("default");
055    
056            if (defaultInit != null)
057                _defaultValue = Long.parseLong(defaultInit);
058    
059            String minInit = (String) m.get("min");
060            if (minInit != null)
061            {
062                _isMinValue = true;
063                _minValue = Long.parseLong(minInit);
064            }
065    
066            String maxInit = (String) m.get("max");
067            if (maxInit != null)
068            {
069                _isMaxValue = true;
070                _maxValue = Long.parseLong(maxInit);
071            }
072        }
073    
074        /**
075         * Converts the string to an Long.  The empty string is returned as zero.
076         * On failure, an error is logged and the method returns zero.
077         */
078        public Object translate(
079            Module contributingModule,
080            Class propertyType,
081            String inputValue,
082            Location location)
083        {
084            if (HiveMind.isBlank(inputValue))
085                return new Long(_defaultValue);
086    
087            long value;
088    
089            try
090            {
091                value = Long.parseLong(inputValue);
092            }
093            catch (Exception ex)
094            {
095                throw new ApplicationRuntimeException(
096                    RulesMessages.invalidLongValue(inputValue),
097                    location,
098                    ex);
099            }
100    
101            if (_isMinValue && value < _minValue)
102                throw new ApplicationRuntimeException(
103                    RulesMessages.minLongValue(inputValue, _minValue));
104    
105            if (_isMaxValue && value > _maxValue)
106                throw new ApplicationRuntimeException(
107                    RulesMessages.maxLongValue(inputValue, _maxValue));
108    
109            return new Long(value);
110    
111        }
112    
113    }