Example usage for java.lang StringBuilder insert

List of usage examples for java.lang StringBuilder insert

Introduction

In this page you can find the example usage for java.lang StringBuilder insert.

Prototype

@Override
public StringBuilder insert(int offset, double d) 

Source Link

Usage

From source file:com.cisco.oss.foundation.monitoring.ServerInfo.java

@SuppressWarnings("unchecked")
private static Object getChild(Object obj, String attributeName, Object value, boolean isSet)
        throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
        InvocationTargetException {
    int substrstart = attributeName.indexOf('[');
    int substrend = attributeName.indexOf(']');

    if (substrstart == -1) {
        if (isSet) {
            String strFunc = "set" + attributeName;
            callSetMethod(obj, value, strFunc);
            return null;
        } else {// w w w  . j  a  v a 2  s  .co m
            String getStr = "get";
            StringBuilder strBuild = new StringBuilder(attributeName);
            strBuild.insert(0, getStr);

            String getFuncStr = strBuild.toString();

            Method m = obj.getClass().getMethod(getFuncStr);
            Object result = m.invoke(obj);

            return result;
        }
    } else {
        String firststr = attributeName.substring(0, substrstart);
        String secondstr = attributeName.substring(substrstart + 1, substrend);
        Object parentObj = obj;
        Object requests = parentObj.getClass().getDeclaredMethod("get" + firststr, new Class[] {})
                .invoke(parentObj, new Object[] {});

        if (requests instanceof Map) {
            Object result = ((HashMap) requests).get(secondstr);
            return result;
        } else if (requests instanceof List) {
            if (isSet) {
                ((List) requests).set(Integer.parseInt(secondstr), value);
                return null;
            } else {
                Object request = ((List) requests).get(Integer.parseInt(secondstr));
                return request;
            }
        } else if (requests.getClass().isArray()) {
            if (isSet) {
                Object[] arr = (Object[]) requests;
                Array.set(arr, Integer.parseInt(secondstr), value);
                return null;
            } else {
                Object[] arr = (Object[]) requests;
                return Array.get(arr, Integer.parseInt(secondstr));
            }
        } else {
            return null;
        }
    }
}

From source file:com.github.nginate.commons.testing.Unique.java

/**
 * Generate unique double. Uses two unique longs to produce integral and fractional parts. Produces values greater
 * than 0.//  ww w.  ja  v a  2s .  c o m
 *
 * @return unique double
 * @see Long#doubleValue()
 */
@Nonnull
public static Double uniqueDouble() {
    long value = uniqueLong();
    String stringValue = String.valueOf(value);

    StringBuilder fractionalBuilder = new StringBuilder(stringValue);
    // adding dot for fractional view
    fractionalBuilder.insert(0, ".");
    // append non zero ending for fractional part
    if (stringValue.endsWith("0")) {
        fractionalBuilder.append("1");
    }
    fractionalBuilder.insert(0, value);
    return Double.parseDouble(fractionalBuilder.toString());
}

From source file:Main.java

public static final String getComment(Element elem) {
    StringBuilder sb = new StringBuilder();
    Node node = elem.getPreviousSibling();
    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            break;
        }//from  w w  w. j av a 2 s .  co  m
        if (node.getNodeType() == Node.COMMENT_NODE) {
            if (sb.length() > 0) {
                sb.insert(0, '\n');
                sb.insert(0, ((Comment) node).getData());
            } else {
                sb.append(((Comment) node).getData());
            }
        }
        node = node.getPreviousSibling();
    }
    return sb.toString();
}

From source file:br.msf.commons.text.HexUtils.java

/**
 * Puts a separator between groups of <tt>groupLen</tt> nibbles.
 * <p/>/*from  w w  w  .  j av a  2 s  .  c o m*/
 * Also, puts leading zeroes when necessary.
 *
 * @param hexString The hex string to be formatted.
 * @param groupLen  The length of the groups of nibbles.
 * @return The formatted hex string.
 */
public static String format(final String hexString, final int groupLen) {
    final String unformatted = unformat(hexString);
    ArgumentUtils.rejectIfDontMatches(unformatted, HEX_PATTERN);
    final StringBuilder buffer = new StringBuilder();
    final StringBuilder formatted = new StringBuilder();
    for (int i = CharSequenceUtils.indexOfLastChar(unformatted); i >= 0; i--) {
        buffer.insert(0, unformatted.charAt(i));
        if (buffer.length() == groupLen) {
            /* When the buffer reaches 'groupLen' size, its contents is passed to 'formatted'. */
            if (i > 0) {
                /*
                 * If unprocessed chars remains on the original string, them we put the separator on the buffer
                 * start.
                 */
                buffer.insert(0, GROUP_SEPARATOR);
            }
            /* we pass the buffer value to the 'formatted' accumulator */
            formatted.insert(0, buffer);
            /* empty the buffer */
            buffer.replace(0, buffer.length(), "");
        }
    }
    /* If unprocessed chars remains on the buffer, it means that we need to fill it up with trailing zeroes. */
    if (buffer.length() > 0) {
        buffer.insert(0, StringUtils.repeat("0", groupLen - buffer.length()));
        /* we pass the buffer value to the 'formatted' accumulator */
        formatted.insert(0, buffer);
    }
    return formatted.toString();
}

From source file:org.openspaces.rest.utils.ControllerUtils.java

public static SpaceDocument[] createSpaceDocuments(String type, String body, GigaSpace gigaSpace)
        throws TypeNotFoundException {
    HashMap<String, Object>[] propertyMapArr;
    try {//  ww w  .  j  ava2 s. c om
        //if single json object convert it to array
        String data = body;
        if (!body.startsWith("[")) {
            StringBuilder sb = new StringBuilder(body);
            sb.insert(0, "[");
            sb.append("]");
            data = sb.toString();
        }
        //convert to json
        propertyMapArr = mapper.readValue(data, typeRef);
    } catch (Exception e) {
        throw new HttpMessageNotReadableException(e.getMessage(), e.getCause());
    }
    SpaceDocument[] documents = new SpaceDocument[propertyMapArr.length];
    for (int i = 0; i < propertyMapArr.length; i++) {
        Map<String, Object> typeBasedProperties = getTypeBasedProperties(type, propertyMapArr[i], gigaSpace);
        documents[i] = new SpaceDocument(type, typeBasedProperties);
    }
    return documents;
}

From source file:com.stratio.crossdata.core.utils.ParserUtils.java

/**
 * Mark in an parsing error message the point which the parser no longer recognizes the string.
 *
 * @param query The user provided query.
 * @param ae    An {@link com.stratio.crossdata.core.utils.AntlrError}.
 * @return A string with the character {@code ?} located in the point which the parser no longer recognizes the
 * input string./*w ww  . ja v a  2  s. c  om*/
 */
public static String getQueryWithSign(String query, AntlrError ae) {
    int marker = 0;
    String q = query;
    if (q.startsWith("[")) {
        marker = q.indexOf("], ") + 3;
        q = q.substring(marker);
    }
    StringBuilder sb = new StringBuilder(q);
    int pos = getCharPosition(ae) - marker;
    if (pos >= 0) {
        sb.insert(pos, "?");
    }
    return sb.toString();
}

From source file:de.erdesignerng.dialect.msaccess.MSAccessFileFormat.java

/**
 * Expands a given string by inserting special characters between each
 * original character.// w  w  w.j  a  v a  2  s. c  o m
 *
 * @param aString           - the string to expand
 * @param aDividingCharCode - the character code to use for expanding
 * @return An expanded string
 */
private static String expand(String aString, Integer aDividingCharCode) {
    StringBuilder buffer = new StringBuilder(aString);

    for (int i = 0; i < aString.length() - 1; i++) {
        buffer.insert((i * 2) + 1, (char) (int) aDividingCharCode);
    }

    return buffer.toString();
}

From source file:org.brutusin.json.impl.JacksonCodec.java

static String addVersion(String jsonSchema) {
    jsonSchema = jsonSchema.replaceAll("\"\\$schema\"\\s*:\\s*\"[^\"]*\"\\s*,?", "");
    if (!jsonSchema.contains("\"$schema\"")) {
        if (jsonSchema.startsWith("{\"type\":")) {
            StringBuilder sb = new StringBuilder(jsonSchema);
            sb.insert(1, "\"$schema\":\"http://brutusin.org/json/json-schema-spec\",");
            return sb.toString();
        }//from  www . j ava2  s  .  c om
    }
    return jsonSchema;
}

From source file:org.brutusin.json.impl.JacksonCodec.java

static String addDraftv3(String jsonSchema) {
    jsonSchema = jsonSchema.replaceAll("\"\\$schema\"\\s*:\\s*\"[^\"]*\"\\s*,?", "");
    if (!jsonSchema.contains("\"$schema\"")) {
        if (jsonSchema.startsWith("{\"type\":")) {
            StringBuilder sb = new StringBuilder(jsonSchema);
            sb.insert(1, "\"$schema\":\"http://json-schema.org/draft-03/schema#\",");
            return sb.toString();
        }/*from  w ww  .j  a  v a2 s  .c o m*/
    }
    return jsonSchema;
}

From source file:org.eclipse.virgo.ide.runtime.core.ServerUtils.java

/**
 * Returns the name of the source jar following the BRITS conventions
 *//* w w w . ja  v  a  2 s . co  m*/
public static File getSourceFile(URI uri) {
    File file = new File(uri);
    StringBuilder builder = new StringBuilder(file.getName());
    int ix = builder.lastIndexOf("-");
    if (ix > 0) {
        builder.insert(ix, SOURCES_SUFFIX);
        File sourceFile = new File(file.getParentFile(), builder.toString());
        return sourceFile;
    }
    return null;
}