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

org.asteriskjava.util.AstUtil Maven / Gradle / Ivy

There is a newer version: 3.41.0
Show newest version
package org.asteriskjava.util;

import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

/**
 * Some static utility methods to imitate Asterisk specific logic. 
* See Asterisk's util.c.
* Client code is not supposed to use this class. * * @author srt * @version $Id$ */ public class AstUtil { private static final Set TRUE_LITERALS; private static final Set NULL_LITERALS; static { TRUE_LITERALS = new HashSet<>(20); TRUE_LITERALS.add("yes"); TRUE_LITERALS.add("true"); TRUE_LITERALS.add("y"); TRUE_LITERALS.add("t"); TRUE_LITERALS.add("1"); TRUE_LITERALS.add("on"); TRUE_LITERALS.add("enabled"); NULL_LITERALS = new HashSet<>(20); NULL_LITERALS.add(""); NULL_LITERALS.add("unknown"); NULL_LITERALS.add("none"); // VarSet event in pbx.c NULL_LITERALS.add(""); NULL_LITERALS.add("-none-"); // IPaddress in PeerEntryEvent NULL_LITERALS.add("(none)"); NULL_LITERALS.add(""); NULL_LITERALS.add("(not set)"); NULL_LITERALS.add(""); NULL_LITERALS.add("n/a"); // channel in AgentsEvent NULL_LITERALS.add(""); NULL_LITERALS.add("(null)"); // appData in ListDialplanEvent } private AstUtil() { // hide constructor } /** * Checks if a String represents true or false * according to Asterisk's logic.
* The original implementation is util.c is as follows:
* *
     *     int ast_true(const char *s)
     *     {
     *         if (!s || ast_strlen_zero(s))
     *             return 0;
     * 
* if (!strcasecmp(s, "yes") || * !strcasecmp(s, "true") || * !strcasecmp(s, "y") || * !strcasecmp(s, "t") || * !strcasecmp(s, "1") || * !strcasecmp(s, "on")) * return -1; *
* return 0; * } *
* *
* To support the dnd property of * {@link org.asteriskjava.manager.event.ZapShowChannelsEvent} this method * also consideres the string "Enabled" as true. * * @param o the Object (usually a String) to check for true. * @return true if s represents true, * false otherwise. */ public static boolean isTrue(Object o) { if (o == null) { return false; } if (o instanceof Boolean) { return (Boolean) o; } return TRUE_LITERALS.contains(o.toString().toLowerCase(Locale.US)); } /** * @param a an object * @param b an object to be compared with {@code a} for equality * @return {@code true} if the arguments are equal to each other and * {@code false} otherwise */ public static boolean isEqual(Object a, Object b) { return a == b || a != null && a.equals(b); } /** * Parses a string for caller id information.
* The caller id string should be in the form * "Some Name" <1234>.
* This resembles ast_callerid_parse in callerid.c * but strips any whitespace. * * @param s the string to parse * @return a String[] with name (index 0) and number (index 1) */ public static String[] parseCallerId(String s) { final String[] result = new String[2]; final int lbPosition; final int rbPosition; String name; String number; if (s == null) { return result; } lbPosition = s.lastIndexOf('<'); rbPosition = s.lastIndexOf('>'); // no opening and closing brace? use value as CallerId name if (lbPosition < 0 || rbPosition < 0) { name = s.trim(); if (name.length() == 0) { name = null; } result[0] = name; return result; } number = s.substring(lbPosition + 1, rbPosition).trim(); if (number.length() == 0) { number = null; } name = s.substring(0, lbPosition).trim(); if (name.startsWith("\"") && name.endsWith("\"") && name.length() > 1) { name = name.substring(1, name.length() - 1).trim(); } if (name.length() == 0) { name = null; } result[0] = name; result[1] = number; return result; } /** * Checks if the value of s was null in Asterisk.
* This method is useful as Asterisk likes to replace null * values with different string values like "unknown", "<unknown>" or * "<null>".
* To find such replacements search for S_OR in Asterisk's * source code. You will find things like * *
     * S_OR(chan->cid.cid_num, "<unknown>")
     * fdprintf(fd, "agi_callerid: %s\n", S_OR(chan->cid.cid_num, "unknown"));
     * 
* * and more... * * @param s the string to test, may be null. If s is not a * string the only test that is performed is a check for * null. * @return true if the s was null in Asterisk; * false otherwise. */ public static boolean isNull(Object s) { if (s == null) { return true; } if (!(s instanceof String)) { return false; } return NULL_LITERALS.contains(((String) s).toLowerCase(Locale.US)); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy