Example usage for java.util List set

List of usage examples for java.util List set

Introduction

In this page you can find the example usage for java.util List set.

Prototype

E set(int index, E element);

Source Link

Document

Replaces the element at the specified position in this list with the specified element (optional operation).

Usage

From source file:com.taobao.android.PatchFieldTool.java

/**
 * ?dexfield,???String/*  w ww  .  j a  v  a 2s . co  m*/
 *
 * @param dexFile
 * @param outDexFile
 * @param orgFieldValue
 * @param newFieldValue
 * @return
 */
public static boolean modifyFieldValue(File dexFile, File outDexFile, String orgFieldValue,
        String newFieldValue) throws IOException, RecognitionException {
    File smaliFolder = new File(outDexFile.getParentFile(), "smali");
    if (smaliFolder.exists()) {
        FileUtils.deleteDirectory(smaliFolder);
    }
    smaliFolder.mkdirs();
    boolean disassembled = SmaliUtils.disassembleDexFile(dexFile, smaliFolder);
    if (disassembled) {
        Collection<File> smaliFiles = FileUtils.listFiles(smaliFolder, new String[] { "smali" }, true);
        for (File smaliFile : smaliFiles) {
            List<String> lines = FileUtils.readLines(smaliFile);
            for (int index = 0; index < lines.size(); index++) {
                String line = lines.get(index);
                String newLine = StringUtils.replace(line, "\"" + orgFieldValue + "\"",
                        "\"" + newFieldValue + "\"");
                lines.set(index, newLine);
            }
            FileUtils.writeLines(smaliFile, lines);
        }

        //?dex
        boolean assembled = SmaliUtils.assembleSmaliFile(smaliFolder, outDexFile);
        if (assembled) {
            FileUtils.deleteDirectory(smaliFolder);
            return true;
        }
    }
    return false;
}

From source file:jef.tools.collection.CollectionUtil.java

/**
 * List/*from   w  ww.ja v a2 s.  c  o  m*/
 * @param obj
 * @param index
 * @param value
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void listSet(List obj, int index, Object value) {
    int length = obj.size();
    if (index < 0)
        index += length;
    obj.set(index, value);
}

From source file:org.mongojack.internal.util.SerializationUtils.java

private static Object serializeQueryField(Object value, JsonSerializer serializer,
        SerializerProvider serializerProvider, String op) {
    if (serializer == null) {
        if (value == null || BASIC_TYPES.contains(value.getClass())) {
            // Return as is
            return value;
        } else if (value instanceof Collection) {
            Collection<?> coll = (Collection<?>) value;
            List<Object> copy = null;
            int position = 0;
            for (Object item : coll) {
                Object returned = serializeQueryField(item, null, serializerProvider, op);
                if (returned != item) {
                    if (copy == null) {
                        copy = new ArrayList<Object>(coll);
                    }/*from  w  w w.  ja  v  a  2  s  .  c  om*/
                    copy.set(position, returned);
                }
                position++;
            }
            if (copy != null) {
                return copy;
            } else {
                return coll;
            }
        } else if (value.getClass().isArray()) {
            if (BASIC_TYPES.contains(value.getClass().getComponentType())) {
                return value;
            }
            Object[] array = (Object[]) value;
            Object[] copy = null;
            for (int i = 0; i < array.length; i++) {
                Object returned = serializeQueryField(array[i], null, serializerProvider, op);
                if (returned != array[i]) {
                    if (copy == null) {
                        copy = new Object[array.length];
                        System.arraycopy(array, 0, copy, 0, array.length);
                    }
                    copy[i] = returned;
                }
            }
            if (copy != null) {
                return copy;
            } else {
                return array;
            }
        } else {
            // We don't know what it is, just find a serializer for it
            serializer = JacksonAccessor.findValueSerializer(serializerProvider, value.getClass());
        }
    }
    BsonObjectGenerator objectGenerator = new BsonObjectGenerator();
    try {
        serializer.serialize(value, objectGenerator, serializerProvider);
    } catch (IOException e) {
        throw new MongoJsonMappingException("Error serializing value " + value + " in DBQuery operation " + op,
                e);
    }
    return objectGenerator.getValue();
}

From source file:eu.tsp.sal.WSN.java

/**
 * Initializes a random population.//from ww w. ja v  a 2 s  . c  o  m
 * @param   len     lenth of chromosome
 * @param   popSize population size
 */
private static ElitisticListPopulation randomPopulation(int len, int popSize) {
    List<Chromosome> popList = new ArrayList<>();

    for (int i = 0; i < popSize; i++) {
        List<Integer> rList = new ArrayList<Integer>(len);

        // set each element randomly to 0 or 1
        for (int j = 0; j < len; j++)
            rList.add(GeneticAlgorithm.getRandomGenerator().nextInt(2));

        // count the number of 1 and set to m
        int M = 0;
        for (int e : rList)
            if (e == 1)
                M++;

        // update 0 with a random number according to the algorithm
        // random of (cj, cj + M)
        int c = 2;
        for (int j = 0; j < rList.size(); j++) {
            int e = rList.get(j);
            if (e == 1)
                continue;
            else {
                int val = c + GeneticAlgorithm.getRandomGenerator().nextInt(M);
                rList.set(j, val);
                c += M;
            }
        }

        Chromosome randChrom = new SensorIndividual(rList);
        popList.add(randChrom);

    }
    return new ElitisticListPopulation(popList, popList.size(), ELITISM_RATE);
}

From source file:com.gargoylesoftware.htmlunit.CodeStyleTest.java

private static List<String> alertsToList(final List<String> lines, final int alertsIndex,
        final boolean preserveCommas) {
    if ("    @Alerts".equals(lines.get(alertsIndex))) {
        lines.set(alertsIndex, "    @Alerts()");
    }/*from   w w w.  j  ava 2  s.  co m*/
    if (!lines.get(alertsIndex).startsWith("    @Alerts(")) {
        throw new IllegalArgumentException("No @Alerts found in " + (alertsIndex + 1));
    }
    final StringBuilder alerts = new StringBuilder();
    for (int i = alertsIndex;; i++) {
        final String line = lines.get(i);
        if (alerts.length() != 0) {
            alerts.append('\n');
        }
        if (line.startsWith("    @Alerts(")) {
            alerts.append(line.substring("    @Alerts(".length()));
        } else {
            alerts.append(line);
        }
        if (line.endsWith(")")) {
            alerts.deleteCharAt(alerts.length() - 1);
            break;
        }
    }
    final List<String> list = alertsToList(alerts.toString());
    if (!preserveCommas) {
        for (int i = 0; i < list.size(); i++) {
            String value = list.get(i);
            if (value.startsWith(",")) {
                value = value.substring(1).trim();
            }
            list.set(i, value);
        }
    }
    return list;
}

From source file:backtype.storm.utils.Utils.java

private static Object normalizeConf(Object conf) {
    if (conf == null)
        return new HashMap();
    if (conf instanceof Map) {
        Map confMap = new HashMap((Map) conf);
        for (Object key : confMap.keySet()) {
            Object val = confMap.get(key);
            confMap.put(key, normalizeConf(val));
        }/*from  w  w  w .  java  2 s  . co  m*/
        return confMap;
    } else if (conf instanceof List) {
        List confList = new ArrayList((List) conf);
        for (int i = 0; i < confList.size(); i++) {
            Object val = confList.get(i);
            confList.set(i, normalizeConf(val));
        }
        return confList;
    } else if (conf instanceof Integer) {
        return ((Integer) conf).longValue();
    } else if (conf instanceof Float) {
        return ((Float) conf).doubleValue();
    } else {
        return conf;
    }
}

From source file:fll.scheduler.TableOptimizer.java

/**
 * Recursive function that computes permutations. To
 * be called from {@see #permutate(int)}.
 * //from   ww w . j a  va  2 s  . com
 * @param arrayCount
 * @param elements the elements to compute permutations of
 * @param order
 * @param permutations the resulting permutations
 */
static private void permutate(final int arrayCount, final List<Integer> elements, final List<Integer> order,
        final List<List<Integer>> permutations) {
    if (elements.isEmpty()) {
        throw new IllegalArgumentException("Cannot permutate 0 elements");
    }

    final int position = arrayCount - elements.size();

    if (elements.size() == 1) {
        order.set(position, elements.get(0));
        permutations.add(order);
    } else {
        for (int i = 0; i < elements.size(); ++i) {
            final int element = elements.get(i);
            final List<Integer> newOrder = new ArrayList<Integer>(order);
            newOrder.set(position, element);

            final List<Integer> newElements = new ArrayList<Integer>(elements);
            newElements.remove(i);
            permutate(arrayCount, newElements, newOrder, permutations);
        }
    }
}

From source file:fr.landel.utils.assertor.helper.HelperMessage.java

/**
 * Converts parameters list into array and also converts types to improve
 * readability (ex: {@link Calendar} into {@link java.util.Date})
 * /*from   w  w w  . j  a  v  a 2s  . c o m*/
 * @param parameters
 *            the input list
 * @return the output array
 */
public static Object[] convertParams(final List<ParameterAssertor<?>> parameters) {
    if (CollectionUtils.isNotEmpty(parameters)) {
        final List<Object> convertedParams = CollectionUtils2.transformIntoList(parameters,
                HelperAssertor.PARAM_TRANSFORMER);
        // The object, the type and if it's a checked object
        ParameterAssertor<?> param;
        int calendarField = -1;

        // in order for binary search
        final EnumType[] surroundable = new EnumType[] { EnumType.ARRAY, EnumType.ITERABLE, EnumType.MAP };

        for (int i = 0; i < parameters.size(); i++) {
            param = parameters.get(i);
            if (param.getObject() != null) {
                if (EnumType.DATE.equals(param.getType())
                        && Calendar.class.isAssignableFrom(param.getObject().getClass())) {
                    convertedParams.set(i, ((Calendar) param.getObject()).getTime());
                } else if (EnumType.CALENDAR_FIELD.equals(param.getType())) {
                    calendarField = (Integer) param.getObject();
                    if (CALENDAR_FIELDS.containsKey(calendarField)) {
                        convertedParams.set(i, CALENDAR_FIELDS.get(calendarField));
                    }
                } else if (EnumType.CLASS.equals(param.getType())) {
                    convertedParams.set(i, ((Class<?>) param.getObject()).getSimpleName());
                } else if (Arrays.binarySearch(surroundable, param.getType()) > -1) {
                    convertedParams.set(i,
                            HelperMessage.surroundByBrackets(param.getObject(), param.getType()));
                }
            }
        }
        return convertedParams.toArray();
    }
    return new Object[0];
}

From source file:com.evolveum.midpoint.repo.sql.data.common.RObjectTextInfo.java

private static Set<RObjectTextInfo> createItemsSet(RObject repo, List<String> allWords) {
    Set<RObjectTextInfo> rv = new HashSet<>();
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < allWords.size(); i++) {
        String word = allWords.get(i);
        if (sb.length() + word.length() + 2 <= MAX_TEXT_SIZE) {
            sb.append(" ").append(word);
        } else {//from  w  ww . ja v a2  s .  c o m
            if (sb.length() > 0) {
                sb.append(" ");
                rv.add(new RObjectTextInfo(repo, sb.toString()));
                sb = new StringBuilder();
                i--; // to reiterate
            } else {
                // a problem - too large string
                LOGGER.warn("Word too long to be correctly indexed: {}", word);
                rv.add(new RObjectTextInfo(repo, " " + word.substring(0, MAX_TEXT_SIZE - 2) + " "));
                allWords.set(i, word.substring(MAX_TEXT_SIZE - 2));
                i--; // to reiterate (with shortened word)
            }
        }
    }
    if (sb.length() > 0) {
        sb.append(" ");
        rv.add(new RObjectTextInfo(repo, sb.toString()));
    }
    return rv;
}

From source file:Main.java

/**
 * A shell sort//from  ww  w.ja  va 2 s  .c  o  m
 * 
 * @author Jason Harrison
 */
public static <T extends Comparable<T>> void shellSort(List<T> list) {
    int h = 1; /* find the largest h value possible */
    while ((h * 3 + 1) < list.size()) {
        h = 3 * h + 1;
    } /* 
       * while h remains larger than 0 
       */
    while (h > 0) { /* 
                     * for each set of elements (there are h sets) 
                     */
        for (int i = h - 1; i < list.size(); i++) { /*
                                                     * pick the last element in the set 
                                                     */
            T A;
            T B = list.get(i);
            int j = i;
            /* 
             * compare the element at B to the one before it in the set 
             * if they are out of order continue this loop, moving
             * elements "back" to make room for B to be inserted. 
             */
            for (j = i; (j >= h) && ((A = list.get(j - h)).compareTo(B) > 0); j -= h) {
                list.set(j, A);
            } /* 
               * insert B into the correct place
               */
            list.set(j, B);
        } /*
           * all sets h-sorted, now decrease set size
           */
        h = h / 3;
    }
}