Example usage for java.util HashSet size

List of usage examples for java.util HashSet size

Introduction

In this page you can find the example usage for java.util HashSet size.

Prototype

public int size() 

Source Link

Document

Returns the number of elements in this set (its cardinality).

Usage

From source file:org.apache.hadoop.hdfs.server.namenode.TestBlockPlacementPolicyRaid.java

private void verifyNetworkLocations(DatanodeDescriptor[] locations, int expectedNumOfRacks) {
    HashSet<String> racks = new HashSet<String>();
    for (DatanodeDescriptor loc : locations) {
        racks.add(loc.getNetworkLocation());
    }//from  www .j  a v a  2s. c o  m
    assertEquals(expectedNumOfRacks, racks.size());
}

From source file:net.shibboleth.idp.attribute.resolver.ad.impl.ScriptedAttributeTest.java

@Test
public void v2Context()
        throws IOException, ComponentInitializationException, ResolutionException, ScriptException {

    final ScriptedAttributeDefinition scripted = new ScriptedAttributeDefinition();
    scripted.setId("scripted");
    scripted.setScript(new EvaluableScript(SCRIPT_LANGUAGE, getScript("requestContext.script")));
    scripted.initialize();/*from  w  w  w  .  j a v  a 2 s.c om*/

    IdPAttribute result = scripted.resolve(generateContext());
    HashSet<IdPAttributeValue> set = new HashSet(result.getValues());
    Assert.assertEquals(set.size(), 3);
    Assert.assertTrue(set.contains(new StringAttributeValue(TestSources.PRINCIPAL_ID)));
    Assert.assertTrue(set.contains(new StringAttributeValue(TestSources.IDP_ENTITY_ID)));
    Assert.assertTrue(set.contains(new StringAttributeValue(TestSources.SP_ENTITY_ID)));

}

From source file:org.apache.solr.handler.TestConfigReload.java

private void checkConfReload(SolrZkClient client, String resPath, String name, String uri) throws Exception {
    Stat stat = new Stat();
    byte[] data = null;
    try {/*from  www . j ava 2s.  c  o m*/
        data = client.getData(resPath, null, stat, true);
    } catch (KeeperException.NoNodeException e) {
        data = "{}".getBytes(StandardCharsets.UTF_8);
        log.info("creating_node {}", resPath);
        client.create(resPath, data, CreateMode.PERSISTENT, true);
    }
    long startTime = System.nanoTime();
    Stat newStat = client.setData(resPath, data, true);
    client.setData("/configs/conf1", new byte[] { 1 }, true);
    assertTrue(newStat.getVersion() > stat.getVersion());
    log.info("new_version " + newStat.getVersion());
    Integer newVersion = newStat.getVersion();
    long maxTimeoutSeconds = 20;
    DocCollection coll = cloudClient.getZkStateReader().getClusterState().getCollection("collection1");
    List<String> urls = new ArrayList<>();
    for (Slice slice : coll.getSlices()) {
        for (Replica replica : slice.getReplicas())
            urls.add("" + replica.get(ZkStateReader.BASE_URL_PROP) + "/"
                    + replica.get(ZkStateReader.CORE_NAME_PROP));
    }
    HashSet<String> succeeded = new HashSet<>();

    while (TimeUnit.SECONDS.convert(System.nanoTime() - startTime, TimeUnit.NANOSECONDS) < maxTimeoutSeconds) {
        Thread.sleep(50);
        for (String url : urls) {
            Map respMap = getAsMap(url + uri + "?wt=json");
            if (String.valueOf(newVersion)
                    .equals(String.valueOf(getObjectByPath(respMap, true, asList(name, "znodeVersion"))))) {
                succeeded.add(url);
            }
        }
        if (succeeded.size() == urls.size())
            break;
        succeeded.clear();
    }
    assertEquals(StrUtils.formatString("tried these servers {0} succeeded only in {1} ", urls, succeeded),
            urls.size(), succeeded.size());
}

From source file:org.apache.ambari.server.checks.ServiceComponentHostVersionMatchCheck.java

/**
 * Iterates over services, and enumerates reported versions of host components.
 * @param services collection of services
 * @return number of distinct actual versions found
 *///from w ww. ja  v  a2s . c o m
private int countDistinctVersionsOnHosts(Collection<Service> services) {
    HashSet<Object> versions = new HashSet<>();
    for (Service service : services) {
        for (ServiceComponent serviceComponent : service.getServiceComponents().values()) {
            if (!serviceComponent.isVersionAdvertised())
                continue;
            for (ServiceComponentHost serviceComponentHost : serviceComponent.getServiceComponentHosts()
                    .values()) {
                versions.add(serviceComponentHost.getVersion());
            }
        }
    }
    return versions.size();
}

From source file:cw.kop.autobackground.settings.AppSettings.java

public static void checkUsedLinksSize() {

    HashSet<String> set = getUsedLinks();

    int iterations = set.size() - getImageHistorySize();

    if (iterations > 0) {
        List<String> linkList = new ArrayList<String>();
        linkList.addAll(set);/*  w  ww .j  a  va 2s  .  co m*/

        Collections.sort(linkList, new Comparator<String>() {
            @Override
            public int compare(String lhs, String rhs) {

                try {
                    long first = Long.parseLong(lhs.substring(lhs.lastIndexOf("Time:")));
                    long second = Long.parseLong(rhs.substring(lhs.lastIndexOf("Time:")));

                    return (int) (first - second);
                } catch (Exception e) {

                }

                return 0;
            }
        });

        for (int i = 0; i < iterations; i++) {
            linkList.remove(0);
        }

        HashSet<String> newSet = new HashSet<String>(linkList);

        prefs.edit().putStringSet("used_history_links", newSet).commit();

    }
}

From source file:org.rhq.plugins.agent.AgentEnvironmentScriptDiscoveryComponent.java

/**
 * Looks for the script relative to the agent home directory.
 * //  w  w  w. j  ava  2  s. com
 * @param context
 * @param version
 * @param baseName
 * @param discoveries where the new details are stored if the script is discovered
 * 
 * @return <code>true</code> if this method discovers the script; <code>false</code> if not
 */
private boolean findInAgentHome(ResourceDiscoveryContext<AgentServerComponent<?>> context, String version,
        String baseName, HashSet<DiscoveredResourceDetails> discoveries) {

    try {
        EmsAttribute home = context.getParentResourceComponent().getAgentBean()
                .getAttribute("AgentHomeDirectory");
        home.refresh();
        Object agentHome = home.getValue();
        if (agentHome != null) {
            File file = new File(agentHome.toString(), "bin/" + baseName);
            if (file.exists()) {
                discoveries.add(createDetails(context, version, file));
            }
        }

        return discoveries.size() > 0;
    } catch (Exception e) {
        log.debug("Cannot use agent home to find environment script. Cause: " + e);
        return false;
    }
}

From source file:org.rhq.plugins.agent.AgentLauncherScriptDiscoveryComponent.java

/**
 * Looks for the launcher script relative to the agent home directory.
 *
 * @param context/*from  ww  w.j a v  a  2  s  .  c o m*/
 * @param version
 * @param baseName
 * @param discoveries where the new details are stored if the script is discovered
 *
 * @return <code>true</code> if this method discovers the launcher script; <code>false</code> if not
 */
private boolean findInAgentHome(ResourceDiscoveryContext<AgentServerComponent<?>> context, String version,
        String baseName, HashSet<DiscoveredResourceDetails> discoveries) {

    try {
        EmsAttribute home = context.getParentResourceComponent().getAgentBean()
                .getAttribute("AgentHomeDirectory");
        home.refresh();
        Object agentHome = home.getValue();
        if (agentHome != null) {
            File file = new File(agentHome.toString(), baseName);
            if (file.exists()) {
                discoveries.add(createDetails(context, version, file));
            }
        }

        return discoveries.size() > 0;
    } catch (Exception e) {
        log.debug("Cannot use agent home to find launcher script. Cause: " + e);
        return false;
    }
}

From source file:com.marklogic.contentpump.DatabaseContentWriter.java

/**
 * fetch the options information from conf and metadata, set to the field
 * "options"// ww  w.j  a  va2s.  c  om
 */
protected ContentCreateOptions newContentCreateOptions(DocumentMetadata meta) {
    ContentCreateOptions opt = (ContentCreateOptions) options.clone();
    if (meta != null) {
        if (opt.getQuality() == 0) {
            opt.setQuality(meta.quality);
        }
        HashSet<String> colSet = new HashSet<String>(meta.collectionsList);
        if (opt.getCollections() != null) {
            // union copy_collection and output_collection
            for (String s : opt.getCollections()) {
                colSet.add(s);
            }
        }
        opt.setCollections(colSet.toArray(new String[colSet.size()]));
        HashSet<ContentPermission> pSet = new HashSet<ContentPermission>(meta.permissionsList);
        if (opt.getPermissions() != null) {
            // union of output_permission & copy_permission
            for (ContentPermission p : opt.getPermissions()) {
                pSet.add(p);
            }
        }
        opt.setPermissions(pSet.toArray(new ContentPermission[pSet.size()]));
    }
    return opt;
}

From source file:InlineSchemaValidator.java

public String getPrefix(String namespaceURI) {
    if (namespaceURI == null) {
        throw new IllegalArgumentException("Namespace URI cannot be null.");
    } else if (XMLConstants.XML_NS_URI.equals(namespaceURI)) {
        return XMLConstants.XML_NS_PREFIX;
    } else if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceURI)) {
        return XMLConstants.XMLNS_ATTRIBUTE;
    } else if (fURIToPrefixMappings != null) {
        HashSet prefixes = (HashSet) fURIToPrefixMappings.get(namespaceURI);
        if (prefixes != null && prefixes.size() > 0) {
            return (String) prefixes.iterator().next();
        }/* www . jav a  2s  .  c  o  m*/
    }
    return null;
}

From source file:net.shibboleth.idp.attribute.resolver.ad.impl.ScriptedAttributeTest.java

@Test
public void examples() throws ScriptException, IOException, ComponentInitializationException {

    IdPAttribute attribute = runExample("example1.script", "example1.attribute.xml", "swissEduPersonUniqueID");

    Assert.assertEquals(attribute.getValues().iterator().next().getValue(),
            DigestUtils.md5Hex("12345678some#salt#value#12345679") + "@switch.ch");

    attribute = runExample("example2.script", "example2.attribute.xml", "eduPersonAffiliation");
    HashSet<IdPAttributeValue> set = new HashSet(attribute.getValues());
    Assert.assertEquals(set.size(), 3);
    Assert.assertTrue(set.contains(new StringAttributeValue("affiliate")));
    Assert.assertTrue(set.contains(new StringAttributeValue("student")));
    Assert.assertTrue(set.contains(new StringAttributeValue("staff")));

    attribute = runExample("example3.script", "example3.attribute.xml", "eduPersonAffiliation");
    set = new HashSet(attribute.getValues());
    Assert.assertEquals(set.size(), 2);//  w ww. ja  v a2s  .  c  o m
    Assert.assertTrue(set.contains(new StringAttributeValue("member")));
    Assert.assertTrue(set.contains(new StringAttributeValue("staff")));

    attribute = runExample("example3.script", "example3.attribute.2.xml", "eduPersonAffiliation");
    set = new HashSet(attribute.getValues());
    Assert.assertEquals(set.size(), 3);
    Assert.assertTrue(set.contains(new StringAttributeValue("member")));
    Assert.assertTrue(set.contains(new StringAttributeValue("staff")));
    Assert.assertTrue(set.contains(new StringAttributeValue("walkin")));

    attribute = runExample("example4.script", "example4.attribute.xml", "eduPersonEntitlement");
    set = new HashSet(attribute.getValues());
    Assert.assertEquals(set.size(), 1);
    Assert.assertTrue(set.contains(new StringAttributeValue("urn:mace:dir:entitlement:common-lib-terms")));

    attribute = runExample("example4.script", "example4.attribute.2.xml", "eduPersonEntitlement");
    set = new HashSet(attribute.getValues());
    Assert.assertEquals(set.size(), 2);
    Assert.assertTrue(set.contains(new StringAttributeValue("urn:mace:dir:entitlement:common-lib-terms")));
    Assert.assertTrue(set.contains(new StringAttributeValue("LittleGreenMen")));

    attribute = runExample("example4.script", "example4.attribute.3.xml", "eduPersonEntitlement");
    Assert.assertNull(attribute);

}