Example usage for java.util ArrayList clear

List of usage examples for java.util ArrayList clear

Introduction

In this page you can find the example usage for java.util ArrayList clear.

Prototype

public void clear() 

Source Link

Document

Removes all of the elements from this list.

Usage

From source file:de.decidr.test.database.factories.EntityFactory.java

/**
 * Returns a subset containing numItems items of the given list, in random
 * order./*w w  w. j a va  2s  .co  m*/
 * 
 * @param list
 *            must not be null
 * @param numItems
 *            must be positive
 * @return random subset
 */
@SuppressWarnings("unchecked")
public List getRandomList(List list, int numItems) {
    ArrayList result = new ArrayList(Math.min(list.size(), numItems));

    if (list.isEmpty()) {
        // nothing to do, return empty list
    } else if (list.size() == 1) {
        // (frequent) special case where the input list can serve as a
        // random pool
        result.add(list.get(rnd.nextInt(list.size())));
    } else {
        // create temporary shallow copy that serves as a random pool.
        ArrayList copy = new ArrayList(list.size());
        copy.addAll(list);

        for (int i = 0; i < numItems; i++) {
            result.add(copy.remove(rnd.nextInt(copy.size())));
        }

        copy.clear();
    }

    return result;
}

From source file:com.github.haixing_hu.bean.DefaultProperty.java

@Override
public final void setIndexedValue(final ArrayList<Object> list) {
    checkKind(PropertyKind.INDEXED);//from   w  w w. j a  va 2 s . c o  m
    requireNonNull("list", list);
    for (final Object obj : list) {
        checkType(obj);
    }
    @SuppressWarnings("unchecked")
    final ArrayList<Object> valueList = (ArrayList<Object>) value;
    valueList.clear();
    valueList.addAll(list);
}

From source file:eu.medsea.mimeutil.detector.MagicMimeMimeDetector.java

private static void parse(final String magicFile, final Reader r) throws IOException {
    long start = System.currentTimeMillis();

    BufferedReader br = new BufferedReader(r);
    String line;//from w w w .  j  a  v  a 2 s.  c om
    ArrayList sequence = new ArrayList();

    long lineNumber = 0;
    line = br.readLine();
    if (line != null)
        ++lineNumber;
    while (true) {
        if (line == null) {
            break;
        }
        line = line.trim();
        if (line.length() == 0 || line.charAt(0) == '#') {
            line = br.readLine();
            if (line != null)
                ++lineNumber;
            continue;
        }
        sequence.add(line);

        // read the following lines until a line does not begin with '>' or
        // EOF
        while (true) {
            line = br.readLine();
            if (line != null)
                ++lineNumber;
            if (line == null) {
                addEntry(magicFile, lineNumber, sequence);
                sequence.clear();
                break;
            }
            line = line.trim();
            if (line.length() == 0 || line.charAt(0) == '#') {
                continue;
            }
            if (line.charAt(0) != '>') {
                addEntry(magicFile, lineNumber, sequence);
                sequence.clear();
                break;
            }
            sequence.add(line);
        }

    }
    if (!sequence.isEmpty()) {
        addEntry(magicFile, lineNumber, sequence);
    }

    if (log.isDebugEnabled())
        log.debug("Parsing \"" + magicFile + "\" took " + (System.currentTimeMillis() - start) + " msec.");
}

From source file:de.vandermeer.skb.base.utils.Skb_STUtils_Tests.java

@Test
public void testGetMissingSTArguments() {
    //null and empty ST
    assertNull(Skb_STUtils.getMissingSTArguments(null, null));
    assertNull(Skb_STUtils.getMissingSTArguments(new ST(""), null));
    assertNull(Skb_STUtils.getMissingSTArguments(null, new ArrayList<String>()));

    //empty but not-null ST and empty chunk list
    assertNotNull(Skb_STUtils.getMissingSTArguments(new ST(""), new ArrayList<String>()));
    assertEquals(0, Skb_STUtils.getMissingSTArguments(new ST(""), new ArrayList<String>()).size());

    //some vars for further testing
    ArrayList<String> chunks = new ArrayList<String>();
    STGroup stg = new STGroupFile(this.stgFileSimple);
    assertNotNull(stg);/*from   w w  w.jav  a 2s.c  o m*/
    ST st;

    //test an empty template
    chunks.clear();
    st = stg.getInstanceOf("noArg");
    assertNotNull(st);
    assertEquals(0, Skb_STUtils.getMissingSTArguments(st, chunks).size());

    chunks.add("one");
    assertEquals(1, Skb_STUtils.getMissingSTArguments(st, chunks).size());
    assertTrue(Skb_STUtils.getMissingSTArguments(st, chunks).contains("one"));

    chunks.add("two");
    assertEquals(2, Skb_STUtils.getMissingSTArguments(st, chunks).size());
    assertTrue(Skb_STUtils.getMissingSTArguments(st, chunks).contains("one"));
    assertTrue(Skb_STUtils.getMissingSTArguments(st, chunks).contains("two"));

    //test a template with 1 argument
    chunks.clear();
    st = stg.getInstanceOf("oneArg");
    assertNotNull(st);
    assertEquals(0, Skb_STUtils.getMissingSTArguments(st, chunks).size());

    chunks.add("one");
    assertEquals(0, Skb_STUtils.getMissingSTArguments(st, chunks).size());

    chunks.add("two");
    assertEquals(1, Skb_STUtils.getMissingSTArguments(st, chunks).size());
    assertTrue(Skb_STUtils.getMissingSTArguments(st, chunks).contains("two"));

    //test a template with 2 arguments
    chunks.clear();
    st = stg.getInstanceOf("twoArgs");
    assertNotNull(st);

    assertEquals(0, Skb_STUtils.getMissingSTArguments(st, chunks).size());

    chunks.add("one");
    assertEquals(0, Skb_STUtils.getMissingSTArguments(st, chunks).size());

    chunks.add("two");
    assertEquals(0, Skb_STUtils.getMissingSTArguments(st, chunks).size());

    chunks.add("three");
    assertEquals(1, Skb_STUtils.getMissingSTArguments(st, chunks).size());
    assertTrue(Skb_STUtils.getMissingSTArguments(st, chunks).contains("three"));

    chunks.add("four");
    assertEquals(2, Skb_STUtils.getMissingSTArguments(st, chunks).size());
    assertTrue(Skb_STUtils.getMissingSTArguments(st, chunks).contains("three"));
    assertTrue(Skb_STUtils.getMissingSTArguments(st, chunks).contains("four"));

    chunks.add("five");
    assertEquals(3, Skb_STUtils.getMissingSTArguments(st, chunks).size());
    assertTrue(Skb_STUtils.getMissingSTArguments(st, chunks).contains("three"));
    assertTrue(Skb_STUtils.getMissingSTArguments(st, chunks).contains("four"));
    assertTrue(Skb_STUtils.getMissingSTArguments(st, chunks).contains("five"));
}

From source file:com.clustercontrol.collect.util.CollectGraphUtil.java

/**
 * ?????????//from  w  w  w.  j  a  v  a 2s. c o  m
 * @param item
 * @param count
 * @return ?
 */
public static int sortManagerNameFacilityIdMap(FacilityTreeItem item, int count) {
    int size = 0;
    ArrayList<String> targetConditionFacilityIdList = new ArrayList<String>();
    targetConditionFacilityIdList.clear();
    if (item.getData().getFacilityType() == FacilityConstant.TYPE_COMPOSITE) {
        // ??[]???????????
        return 0;
    }
    String managerName = ScopePropertyUtil.getManager(item).getData().getFacilityId();
    String facilityId = item.getData().getFacilityId();
    String facilityName = item.getData().getFacilityName();
    if (item.getData().getFacilityType() == FacilityConstant.TYPE_NODE) {
        getInstance().m_managerFacilityIdNameMap.put(managerName + SQUARE_SEPARATOR + facilityId, facilityName);
        getInstance().m_managerFacilityIdDummyNameMap.put(managerName + SQUARE_SEPARATOR + facilityId,
                "dummy_" + count);
    }
    m_log.debug("sortManagerNameFacilityIdMap() managerName:" + managerName + ", facilityId:" + facilityId
            + ", facilityName:" + facilityName);
    switch (item.getData().getFacilityType()) {

    case FacilityConstant.TYPE_NODE:
        // ????
        targetConditionFacilityIdList.add(facilityId);
        addTargetFacilityIdMap(managerName, facilityId);
        break;

    case FacilityConstant.TYPE_SCOPE:
    case FacilityConstant.TYPE_MANAGER:
        // ????????
        setTargetNodeFacilityId(item, targetConditionFacilityIdList);
        break;

    default: // ??
        break;
    }
    size = getInstance().m_managerFacilityIdNameMap.size();
    return size;
}

From source file:edu.cudenver.bios.matrix.test.TestOrthogonalPolynomials.java

/**
 * Test a three-factor U matrix //  w  w  w . j ava 2  s  . co  m
 */
public void testThreeFactorContrast() {
    double[] x1 = { 1, 2, 3 };
    String x1Name = "x1";
    Factor factorX1 = new Factor(x1Name, x1);
    double[] x2 = { 1, 2, 3 };
    String x2Name = "x2";
    Factor factorX2 = new Factor(x2Name, x2);
    double[] x3 = { 1, 2, 3 };
    String x3Name = "x3";
    Factor factorX3 = new Factor(x3Name, x3);
    ArrayList<Factor> factorList = new ArrayList<Factor>();
    factorList.add(factorX1);
    factorList.add(factorX2);
    factorList.add(factorX3);
    OrthogonalPolynomialContrastCollection contrastCollection = OrthogonalPolynomials
            .withinSubjectContrast(factorList);

    printMatrix("Grand mean", contrastCollection.getGrandMean().getContrastMatrix());
    printMatrix("Main effect x1", contrastCollection.getMainEffectContrast(factorX1).getContrastMatrix());
    printMatrix("Main effect x2", contrastCollection.getMainEffectContrast(factorX2).getContrastMatrix());
    printMatrix("Main effect x3", contrastCollection.getMainEffectContrast(factorX3).getContrastMatrix());

    ArrayList<Factor> intFactors = new ArrayList<Factor>(3);
    intFactors.add(factorX1);
    intFactors.add(factorX2);
    printMatrix("Two factor contrast: x1, x2",
            contrastCollection.getInteractionContrast(intFactors).getContrastMatrix());
    intFactors.clear();
    intFactors.add(factorX1);
    intFactors.add(factorX3);
    printMatrix("Two factor contrast: x1, x3",
            contrastCollection.getInteractionContrast(intFactors).getContrastMatrix());
    intFactors.clear();
    intFactors.add(factorX2);
    intFactors.add(factorX3);
    printMatrix("Two factor contrast: x2, x3",
            contrastCollection.getInteractionContrast(intFactors).getContrastMatrix());
    intFactors.clear();
    intFactors.add(factorX1);
    intFactors.add(factorX2);
    intFactors.add(factorX3);
    printMatrix("Three factor contrast",
            contrastCollection.getInteractionContrast(intFactors).getContrastMatrix());
}

From source file:com.sxt.superqq.fragment.SettingsFragment.java

void logout() {
    final ProgressDialog pd = new ProgressDialog(getActivity());
    String st = getResources().getString(R.string.Are_logged_out);
    pd.setMessage(st);/*www  . j  av  a2  s  .c  o m*/
    pd.setCanceledOnTouchOutside(false);
    pd.show();
    SuperQQApplication.getInstance().logout(new EMCallBack() {

        @Override
        public void onSuccess() {
            getActivity().runOnUiThread(new Runnable() {
                public void run() {
                    //????
                    ArrayList<UserBean> contactList = SuperQQApplication.getInstance().getContactList();
                    contactList.clear();
                    HashMap<Integer, ContactBean> contacts = SuperQQApplication.getInstance().getContacts();
                    contacts.clear();
                    pd.dismiss();
                    // ??
                    ((MainActivity) getActivity()).finish();
                    startActivity(new Intent(getActivity(), LoginActivity.class));

                }
            });
        }

        @Override
        public void onProgress(int progress, String status) {

        }

        @Override
        public void onError(int code, String message) {

        }
    });
}

From source file:edu.oregonstate.eecs.mcplan.ensemble.VotingPolicyEnsemble.java

@Override
public A getAction() {
    final TObjectIntMap<A> counts = new TObjectIntHashMap<A>();
    for (final Policy<S, A> pi : Pi) {
        final A a = pi.getAction();
        counts.adjustOrPutValue(a, 1, 1);
    }//w w w  .  j  a  va2 s.  c om

    int max_count = 0;
    final ArrayList<A> candidates = new ArrayList<A>();
    for (final TObjectIntIterator<A> itr = counts.iterator(); itr.hasNext();) {
        itr.advance();
        //         System.out.println( "\t" + itr.key() + " -> " + itr.value() );
        final int c = itr.value();
        if (c > max_count) {
            candidates.clear();
            max_count = c;
            candidates.add(itr.key());
        } else if (c == max_count) {
            candidates.add(itr.key());
        }
    }

    if (candidates.size() == 1) {
        return candidates.get(0);
    } else {
        return candidates.get(rng.nextInt(candidates.size()));
    }
}

From source file:com.norconex.collector.core.data.store.impl.mongo.MongoCrawlDataStore.java

private void processedToCached() {
    BasicDBObject whereQuery = new BasicDBObject(IMongoSerializer.FIELD_STAGE, Stage.PROCESSED.name());
    whereQuery.put(IMongoSerializer.FIELD_IS_VALID, true);
    DBCursor cursor = collRefs.find(whereQuery);

    // Add them to cache in batch
    ArrayList<DBObject> list = new ArrayList<DBObject>(BATCH_UPDATE_SIZE);
    while (cursor.hasNext()) {
        list.add(cursor.next());/*ww w  . j  a va 2 s .  c om*/
        if (list.size() == BATCH_UPDATE_SIZE) {
            collCached.insert(list);
            list.clear();
        }
    }
    if (!list.isEmpty()) {
        collCached.insert(list);
    }
}

From source file:org.apache.hadoop.mapred.gridmix.TestFilePool.java

@Test
public void testPool() throws Exception {
    final Random r = new Random();
    final Configuration conf = new Configuration();
    conf.setLong(FilePool.GRIDMIX_MIN_FILE, 3 * 1024);
    final FilePool pool = new FilePool(conf, base);
    pool.refresh();//from w  w w. ja v a2  s  .co m
    final ArrayList<FileStatus> files = new ArrayList<FileStatus>();

    // ensure 1k, 2k files excluded
    final int expectedPoolSize = (NFILES / 2 * (NFILES / 2 + 1) - 6) * 1024;
    assertEquals(expectedPoolSize, pool.getInputFiles(Long.MAX_VALUE, files));
    assertEquals(NFILES - 4, files.size());

    // exact match
    files.clear();
    assertEquals(expectedPoolSize, pool.getInputFiles(expectedPoolSize, files));

    // match random within 12k
    files.clear();
    final long rand = r.nextInt(expectedPoolSize);
    assertTrue("Missed: " + rand, (NFILES / 2) * 1024 > rand - pool.getInputFiles(rand, files));

    // all files
    conf.setLong(FilePool.GRIDMIX_MIN_FILE, 0);
    pool.refresh();
    files.clear();
    assertEquals((NFILES / 2 * (NFILES / 2 + 1)) * 1024, pool.getInputFiles(Long.MAX_VALUE, files));
}