/*******************************************************************************
 * Copyright (c), 2001, 2002 N2 Broadband, Inc.  All Rights Reserved.
 *
 * This module contains unpublished, confidential, proprietary
 * material.  The use and dissemination of this material are
 * governed by a license.  The above copyright notice does not
 * evidence any actual or intended publication of this material.
 *
 * Author:  Drake H. Henderson
 * Created:  11-12-01
 *
 ******************************************************************************/

package com.n2bb.util;

import java.util.Vector;
import java.lang.reflect.Method;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.n2bb.util.N2bbSettings;

public class IndexFinder {
  
  private static Log n2bbLog = LogFactory.getLog(N2bbSettings.N2BB_LOG);

  public static int indexOf(Vector vector, String methodName, Object matchValue) {
    if ((vector == null) || (vector.size() == 0)) {
      return -1;
    }

    if (methodName == null) {
      n2bbLog.error("method name is null");
      return -1;
    }

    if (matchValue == null) {
      n2bbLog.error("match value is null");
      return -1;
    }

    try {
      Method method = vector.elementAt(0).getClass().getDeclaredMethod(methodName, null);
      String returnType = "" + method.getReturnType();

      for (int i = 0; i < vector.size(); i++) {
        if (returnType.indexOf("String") >= 0) {
          String reqVal = ((String) matchValue).toLowerCase();
          String val = ((String) method.invoke(vector.elementAt(i), null)).toLowerCase();
          if (reqVal.equals(val))
            return i;
        } else if ((returnType.indexOf("int") >= 0) || (returnType.indexOf("Integer") >= 0)) {
          int reqVal = ((Integer) matchValue).intValue();
          int val = ((Integer) method.invoke(vector.elementAt(i), null)).intValue();
          if (reqVal == val)
            return i;
        }
      }
      return -1;
    } catch (Exception e) {
      n2bbLog.error("exception - message... " + e.getMessage(), e);
      return -1;
    }
  }


}

