All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.testng.internal.annotations.Converter Maven / Gradle / Ivy

There is a newer version: 7.10.1
Show newest version
package org.testng.internal.annotations;

import org.testng.collections.Lists;
import org.testng.internal.ClassHelper;
import org.testng.internal.Utils;

import java.util.List;
import java.util.StringTokenizer;

/**
 * Convert a string values into primitive types.
 *
 * Created on Dec 20, 2005
 * @author cbeust
 */
public class Converter {

  public static boolean  getBoolean(String tagValue, boolean def) {
    boolean result = def;
    if (tagValue != null) {
      result = Boolean.valueOf(tagValue);
    }
    return result;
  }

  public static int getInt(String tagValue, int def) {
    int result = def;
    if (tagValue != null) {
      result = new Integer(tagValue);
    }
    return result;
  }

  public static String getString(String tagValue, String def) {
    String result = def;
    if (tagValue != null) {
      result = tagValue;
    }
    return result;
  }

  public static long getLong(String tagValue, long def) {
    long result = def;
    if (tagValue != null) {
      result = new Long(tagValue);
    }
    return result;
  }

  public static String[] getStringArray(String tagValue, String[] def) {
    String[] result = def;
    if (tagValue != null) {
      result = Utils.stringToArray(tagValue);
    }

    return result;
  }

  public static Class[] getClassArray(String tagValue, Class[] def) {
    Class[] result = def;
    List vResult = Lists.newArrayList();
    if (tagValue != null) {
      StringTokenizer st = new StringTokenizer(tagValue, " ,");
      while (st.hasMoreElements()) {
        String className = (String) st.nextElement();
        try {
          Class cls = Class.forName(className);
          vResult.add(cls);
        }
        catch (ClassNotFoundException e) {
          e.printStackTrace();
        }
      }
      result = (Class[]) vResult.toArray(new Class[vResult.size()]);
    }

    return result;
  }

  public static Class getClass(String namedParameter) {
    Class result = null;
    if (namedParameter != null) {
      result = ClassHelper.forName(namedParameter);
    }

    return result;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy