001package com.thetransactioncompany.jsonrpc2.util;
002
003
004/**
005 * The base abstract class for the JSON-RPC 2.0 parameter retrievers.
006 *
007 * @author Vladimir Dzhuvinov
008 */
009public abstract class ParamsRetriever {
010
011        
012        /**
013         * Returns the parameter count.
014         *
015         * @return The parameters count.
016         */
017        public abstract int size();
018
019
020        /**
021         * Matches a string against an array of acceptable values.
022         *
023         * @param input       The string to match.
024         * @param enumStrings The acceptable string values. Must not be 
025         *                    {@code null}.
026         * @param ignoreCase  {@code true} for a case insensitive match.
027         *
028         * @return The matching string value, {@code null} if no match was
029         *         found.
030         */
031        protected static String getEnumStringMatch(final String input, 
032                                                   final String[] enumStrings, 
033                                                   final boolean ignoreCase) {
034        
035                for (final String en: enumStrings) {
036                
037                        if (ignoreCase) {
038                                if (en.equalsIgnoreCase(input))
039                                        return en;
040                        }
041                        else {
042                                if (en.equals(input))
043                                        return en;
044                        }
045                }
046
047                return null;
048        }
049        
050        
051        /**
052         * Matches a string against an enumeration of acceptable values.
053         *
054         * @param input      The string to match.
055         * @param enumClass  The enumeration class specifying the acceptable 
056         *                   string values. Must not be {@code null}.
057         * @param ignoreCase {@code true} for a case insensitive match.
058         *
059         * @return The matching enumeration constant, {@code null} if no match
060         *         was found.
061         */
062        protected static <T extends Enum<T>> T getEnumStringMatch(final String input, 
063                                                                  final Class<T> enumClass, 
064                                                                  final boolean ignoreCase) {
065                
066                for (T en: enumClass.getEnumConstants()) {
067                
068                        if (ignoreCase) {
069                                if (en.toString().equalsIgnoreCase(input))
070                                        return en;
071                        }
072                        else {
073                                if (en.toString().equals(input))
074                                        return en;
075                        }
076                }
077                
078                return null;
079        }
080}