Example usage for java.util Collections nCopies

List of usage examples for java.util Collections nCopies

Introduction

In this page you can find the example usage for java.util Collections nCopies.

Prototype

public static <T> List<T> nCopies(int n, T o) 

Source Link

Document

Returns an immutable list consisting of n copies of the specified object.

Usage

From source file:org.joox.test.JOOXTest.java

License:asdf

@Test
public void testAttr() throws Exception {
    assertNull($.attr("any"));
    assertNull($.attr("id"));
    assertEquals(Arrays.asList((String) null), $.attrs("any"));
    assertEquals(Arrays.asList((String) null), $.attrs("id"));
    assertEquals("1", $.find("book").attr("id"));
    assertEquals(Arrays.asList("1", "2", "3", "4", "1", "3", "1", "2"), $.find("book").attrs("id"));

    assertEquals(Collections.nCopies(totalElements, "y"), $.find().attr("x", "y").attrs("x"));
    assertEquals(Collections.nCopies(totalElements, (String) null),
            $.find().attr("x", (String) null).attrs("x"));

    assertEquals(Collections.nCopies(totalElements, (String) null), $.find().removeAttr("id").attrs("id"));
}

From source file:org.joox.test.JOOXTest.java

License:asdf

@Test
public void testWrap() throws Exception {
    assertEquals(0, $.find("abc").wrap("parent").size());
    assertEquals(0, $.find("parent").size());

    Match wrapped = $.find("author").wrap("parent");
    assertEquals(8, wrapped.size());//from   w  w  w .  j av a 2  s .c  o m
    assertEquals(Collections.nCopies(8, "author"), wrapped.tags());
    assertEquals($.find("author"), wrapped);
    assertEquals(8, $.find("parent").size());
    assertEquals(8, $.find("parent").children("author").size());
    assertEquals($.find("author"), $.find("parent").children("author"));
    assertEquals($.find("parent"), $.find("author").parent());

    wrapped = $.wrap("newroot");
    assertEquals(1, wrapped.size());
    assertEquals("newroot", wrapped.parent().tag());
    assertEquals("newroot", wrapped.document().getDocumentElement().getTagName());
    assertEquals($.xpath("/newroot/document"), wrapped);
}

From source file:org.joox.test.JOOXTest.java

License:asdf

@Test
public void testUnwrap() throws Exception {
    assertEquals(0, $.find("abc").unwrap().size());

    Match unwrapped = $.find("author").unwrap();
    assertEquals(8, unwrapped.size());/*w w w.j  a  v a2s.com*/
    assertEquals(Collections.nCopies(8, "author"), unwrapped.tags());
    assertEquals($.find("author"), unwrapped);
    assertEquals(0, $.find("authors").size());
    assertEquals(Collections.nCopies(8, "book"), unwrapped.parent().tags());
    assertEquals($.find("book").children("author"), unwrapped);

    try {
        $.unwrap();
        fail();
    } catch (RuntimeException expected) {
    }

    try {
        $.find("library").unwrap();
        fail();
    } catch (RuntimeException expected) {
    }
}

From source file:org.joox.test.JOOXTest.java

License:asdf

@Test
public void testText() throws Exception {
    assertNull($.find("any").text());
    assertEquals("Sergio Leone", $.find("director").text());
    assertEquals("Charles Bronson", $.find("actor").text());
    assertEquals("Charles Bronson", $.find("actor").text(0));
    assertEquals("Jason Robards", $.find("actor").text(1));
    assertEquals("Claudia Cardinale", $.find("actor").text(2));
    assertEquals(Arrays.asList("Charles Bronson", "Jason Robards", "Claudia Cardinale"),
            $.find("actor").texts());

    assertEquals(Collections.nCopies(3, "Lukas Eder"), $.find("actor").text("Lukas Eder").texts());

    assertEquals("<abc/>", $.find("actors").text("<abc/>").text());
    assertEquals("<><aa>", $.find("actors").text("<><aa>").text());
}

From source file:org.joox.test.JOOXTest.java

License:asdf

@Test
public void testCData() throws Exception {
    assertNull($.find("any").cdata());
    assertEquals("Sergio Leone", $.find("director").cdata());
    assertEquals("Charles Bronson", $.find("actor").cdata());
    assertEquals("Charles Bronson", $.find("actor").cdata(0));
    assertEquals("Jason Robards", $.find("actor").cdata(1));
    assertEquals("Claudia Cardinale", $.find("actor").cdata(2));
    assertEquals(Arrays.asList("Charles Bronson", "Jason Robards", "Claudia Cardinale"),
            $.find("actor").cdatas());

    assertEquals(Collections.nCopies(3, "Lukas Eder"), $.find("actor").cdata("Lukas Eder").cdatas());
    assertEquals(Collections.nCopies(3, "<actor><![CDATA[Lukas Eder]]></actor>"),
            $.find("actor").map(e -> e.toString()));

    assertEquals("<abc/>", $.find("actors").cdata("<abc/>").cdata());
    assertEquals("<actors><![CDATA[<abc/>]]></actors>", $.find("actors").toString());

    assertEquals("<><aa>", $.find("actors").cdata("<><aa>").cdata());
    assertEquals("<actors><![CDATA[<><aa>]]></actors>", $.find("actors").toString());
}

From source file:org.joox.test.JOOXTest.java

License:asdf

@Test
public void testContent() throws Exception {
    assertEquals("Sergio Leone", $.find("director").content());
    assertEquals(Arrays.asList("Charles Bronson", "Jason Robards", "Claudia Cardinale"),
            $.find("actor").contents());

    assertEquals("Charles Bronson", $.find("actor").content());
    assertEquals("Charles Bronson", $.find("actor").content(0));
    assertEquals("Jason Robards", $.find("actor").content(1));
    assertEquals("Claudia Cardinale", $.find("actor").content(2));

    assertEquals("<><aa>", $.find("actors").content("<><aa>").text());
    assertEquals("<><aa>", $.find("actors").content());
    assertEquals("<abc><x></abc>", $.find("actors").content("<abc><x></abc>").text());
    assertEquals("<abc><x></abc>", $.find("actors").content());
    assertEquals("", $.find("actors").content("<abc><x/></abc>").text());
    assertEquals("<abc><x/></abc>", $.find("actors").content());
    assertEquals(1, $.find("abc").size());
    assertEquals(1, $.find("x").size());
    assertEquals(8, $.find("book").content("<book-content/>").size());
    assertEquals(8, $.find("book-content").size());
    assertEquals(Collections.nCopies(8, "book"), $.find("book-content").parent().tags());

    assertEquals("<xx/><xx/>", $.find("actors").content("<xx/><xx/>").content());
    assertEquals(2, $.find("xx").size());

    // Null and empty string checks
    assertEquals("<document/>", $.content((String) null).toString());
    assertEquals("<document/>", $.content("").toString());
}

From source file:com.net2plan.interfaces.networkDesign.NetPlan.java

/**
 * <p>Adds a new traffic route </p>
 * <p><b>Important</b>: Routing type must be {@link com.net2plan.utils.Constants.RoutingType#SOURCE_ROUTING SOURCE_ROUTING}.</p>
 *
 * @param demand               Demand associated to the route
 * @param carriedTraffic       Carried traffic. It must be greater or equal than zero
 * @param occupiedLinkCapacity Occupied link capacity, it must be greater or equal than zero.
 * @param sequenceOfLinks      Sequence of links traversed by the route
 * @param attributes           Map for user-defined attributes ({@code null} means 'no attribute'). Each key represents the attribute name, whereas value represents the attribute value
 * @return The newly created route object
 * @see com.net2plan.interfaces.networkDesign.Route
 * @see com.net2plan.interfaces.networkDesign.Demand
 * @see com.net2plan.interfaces.networkDesign.Link
 */// w  ww .jav  a 2s .  c  o m
public Route addRoute(Demand demand, double carriedTraffic, double occupiedLinkCapacity,
        List<Link> sequenceOfLinks, Map<String, String> attributes) {
    return addServiceChain(demand, carriedTraffic,
            Collections.nCopies(sequenceOfLinks.size(), occupiedLinkCapacity), sequenceOfLinks, attributes);
}

From source file:tachyon.master.MasterInfo.java

/**
 * @return the total bytes on each storage tier.
 *//*from   w  w  w .  j  a v  a  2 s.  c o  m*/
public List<Long> getTotalBytesOnTiers() {
    List<Long> ret = new ArrayList<Long>(Collections.nCopies(StorageLevelAlias.SIZE, 0L));
    synchronized (mWorkers) {
        for (MasterWorkerInfo worker : mWorkers.values()) {
            for (int i = 0; i < worker.getTotalBytesOnTiers().size(); i++) {
                ret.set(i, ret.get(i) + worker.getTotalBytesOnTiers().get(i));
            }
        }
    }
    return ret;
}

From source file:tachyon.master.MasterInfo.java

/**
 * @return the used bytes on each storage tier.
 *//*w w  w  . jav a 2 s. co m*/
public List<Long> getUsedBytesOnTiers() {
    List<Long> ret = new ArrayList<Long>(Collections.nCopies(StorageLevelAlias.SIZE, 0L));
    synchronized (mWorkers) {
        for (MasterWorkerInfo worker : mWorkers.values()) {
            for (int i = 0; i < worker.getUsedBytesOnTiers().size(); i++) {
                ret.set(i, ret.get(i) + worker.getUsedBytesOnTiers().get(i));
            }
        }
    }
    return ret;
}

From source file:ffx.algorithms.RotamerOptimization.java

/**
 * For decomposing the energies of the original (0th) rotamers without
 * computing the entire energy matrix.//  w w w  .j  a  va 2 s.  c o m
 */
public void decomposeOriginal() {
    allResiduesList = new ArrayList<>();
    polymers = molecularAssembly.getChains();
    for (Polymer polymer : polymers) {
        ArrayList<Residue> current = polymer.getResidues();
        for (Residue residuej : current) {
            if (residuej != null) {
                if (residuej.getRotamers(library) != null) {
                    allResiduesList.add(residuej);
                } else if (useForcedResidues && chain != null) {
                    Polymer setChain = molecularAssembly.getChain(chain);
                    if (setChain.equals(polymer) && checkIfForced(residuej)) {
                        allResiduesList.add(residuej);
                    }
                } else if (useForcedResidues && checkIfForced(residuej)) {
                    allResiduesList.add(residuej);
                }
            }
        }
    }
    numResidues = allResiduesList.size();
    allResiduesArray = allResiduesList.toArray(new Residue[numResidues]);
    Residue residues[] = residueList.toArray(new Residue[residueList.size()]);
    int nRes = residues.length;
    distanceMatrix();

    double totalEnergy;
    double localBackboneEnergy = 0;
    double localSelfEnergy[] = new double[nRes];
    double pairEnergy[][] = new double[nRes][nRes];
    double triEnergy[][][] = new double[nRes][nRes][nRes];
    double residueEnergy[][] = new double[3][nRes];

    for (int i = 0; i < nRes; i++) {
        turnOnAtoms(residues[i]);
    }
    totalEnergy = currentEnergy(allResiduesList);
    for (int i = 0; i < nRes; i++) {
        turnOffAtoms(residues[i]);
    }

    // List to contain current residues.
    List<Residue> rList = new ArrayList<>(Collections.nCopies(4, null));

    try {
        localBackboneEnergy = currentEnergy(rList);
    } catch (ArithmeticException ex) {
        logger.severe(String.format("FFX shutting down: error in calculation of backbone energy %s",
                ex.getMessage()));
    }
    for (int i = 0; i < nRes; i++) {
        Residue ri = residues[i];
        rList.set(0, ri);
        turnOnAtoms(ri);
        localSelfEnergy[i] = currentEnergy(rList) - localBackboneEnergy;
        logger.info(String.format(" Self %s:          %16.5f", ri, localSelfEnergy[i]));
        residueEnergy[0][i] = localSelfEnergy[i];
        turnOffAtoms(ri);
    }
    for (int i = 0; i < nRes; i++) {
        Residue ri = residues[i];
        rList.set(0, ri);
        turnOnAtoms(ri);
        for (int j = i + 1; j < nRes; j++) {
            Residue rj = residues[j];
            rList.set(1, rj);
            turnOnAtoms(rj);
            pairEnergy[i][j] = currentEnergy(rList) - localSelfEnergy[i] - localSelfEnergy[j]
                    - localBackboneEnergy;
            logger.info(String.format(" Pair %s %s:       %16.5f", ri, rj, pairEnergy[i][j]));
            double halfPair = pairEnergy[i][j] * 0.5;
            residueEnergy[1][i] += halfPair;
            residueEnergy[1][j] += halfPair;
            turnOffAtoms(rj);
        }
        turnOffAtoms(ri);
    }
    for (int i = 0; i < nRes; i++) {
        Residue ri = residues[i];
        rList.set(0, ri);
        turnOnAtoms(ri);
        for (int j = i + 1; j < nRes; j++) {
            Residue rj = residues[j];
            rList.set(1, rj);
            turnOnAtoms(rj);
            for (int k = j + 1; k < nRes; k++) {
                Residue rk = residues[k];
                rList.set(2, rk);
                double dist = trimerDistance(i, 0, j, 0, k, 0);
                if (dist < threeBodyCutoffDist) {
                    turnOnAtoms(rk);
                    triEnergy[i][j][k] = currentEnergy(rList) - localSelfEnergy[i] - localSelfEnergy[j]
                            - localSelfEnergy[k] - pairEnergy[i][j] - pairEnergy[j][k] - pairEnergy[i][k]
                            - localBackboneEnergy;
                    logger.info(String.format(" Tri  %s %s %s:    %16.5f", ri, rj, rk, triEnergy[i][j][k]));
                    double thirdTrimer = triEnergy[i][j][k] / 3.0;
                    residueEnergy[2][i] += thirdTrimer;
                    residueEnergy[2][j] += thirdTrimer;
                    residueEnergy[2][k] += thirdTrimer;
                    turnOffAtoms(rk);
                } else if (dist == Double.MAX_VALUE) {
                    logger.info(String.format(" Tri  %s %s %s:    set to 0.0 at NaN (very long distance)", ri,
                            rj, rk));
                    triEnergy[i][j][k] = 0.0;
                } else {
                    logger.info(String.format(" Tri  %s %s %s:    set to 0.0 at %1.5f Angstroms", ri, rj, rk,
                            dist));
                    triEnergy[i][j][k] = 0.0;
                }
            }
            turnOffAtoms(rj);
        }
        turnOffAtoms(ri);
    }

    for (int i = 0; i < nRes; i++) {
        turnOnAtoms(residues[i]);
    }
    decomposePrint(residues, totalEnergy, localBackboneEnergy, residueEnergy);
}