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