Example usage for java.util HashSet contains

List of usage examples for java.util HashSet contains

Introduction

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

Prototype

public boolean contains(Object o) 

Source Link

Document

Returns true if this set contains the specified element.

Usage

From source file:com.thoughtworks.go.plugin.configrepo.contract.CRJob.java

public String validateNameUniqueness(HashSet<String> names) {
    if (names.contains(this.getName()))
        return String.format("Job %s is defined more than once", this.getName());
    else/*from w w w.  j av a2 s  .co  m*/
        names.add(this.getName());
    return null;
}

From source file:org.magnum.mobilecloud.video.controller.VideoController.java

@RequestMapping(value = VIDEO_SVC_PATH + "/{id}" + "/like", method = RequestMethod.POST)
public @ResponseBody Video likeVideo(@PathVariable("id") long id, HttpServletResponse response, Principal p) {

    String username = p.getName();

    Video v = videos.findById(id);//from   ww  w  .  j a v a  2 s . c om

    if (v != null) {

        HashSet<String> videoLikes = v.getUserLikes();

        if (videoLikes.contains(username)) {

            response.setStatus(400);
            return v;

        } else {

            System.out.print("These are the videoLikes: " + videoLikes);

            videoLikes.add(username);

            System.out.print("These are the videoLikes: " + videoLikes);

            v.setLikes((long) (v.getLikes() + 1));

            v.setUserLikes(videoLikes);

            response.setStatus(200);

            videos.save(v);

            return v;

        }

    }

    response.setStatus(404);
    return null;

}

From source file:org.magnum.mobilecloud.video.controller.VideoController.java

@RequestMapping(value = VIDEO_SVC_PATH + "/{id}" + "/unlike", method = RequestMethod.POST)
public @ResponseBody Video unlikeVideo(@PathVariable("id") long id, HttpServletResponse response, Principal p) {

    String username = p.getName();

    Video v = videos.findById(id);//from   ww w  .j a v  a2 s.c  om

    if (v != null) {

        HashSet<String> videoLikes = v.getUserLikes();

        if (videoLikes.contains(username)) {

            System.out.print("These are the videoLikes: " + videoLikes);

            videoLikes.remove(username);

            System.out.print("These are the videoLikes: " + videoLikes);

            v.setLikes((long) (v.getLikes() - 1));

            v.setUserLikes(videoLikes);

            response.setStatus(200);

            videos.save(v);

            return v;

        } else {

            response.setStatus(400);
            return v;

        }

    }

    response.setStatus(404);
    return null;

}

From source file:edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.preprocessors.NewResourcePreprocessor.java

private void replaceNewURIs() {
    if (editConfiguration.isUpdate()) {
        List<String> urisOnForm = editConfiguration.getUrisOnform();
        HashSet<String> configUrisOnForm = new HashSet<String>();
        configUrisOnForm.addAll(urisOnForm);
        for (String varName : forceNewURIsList) {
            //No value submitted for uri, so new resource needs to be created
            //and old one should not be used
            if (configUrisOnForm.contains(varName) && !urisFromForm.containsKey(varName)) {
                submission.addUriToForm(editConfiguration, varName,
                        new String[] { EditConfigurationConstants.NEW_URI_SENTINEL });
            }//from   w  w  w .  j  av  a 2 s. c  o  m
        }
    }
}

From source file:com.voidsearch.voidbase.quant.feed.TokenFrequency.java

private void dumpTopTerms() throws Exception {

    int termCnt = 0;

    Collections.sort(topTerms);//from  w w w  .  j ava2s .  c o  m
    HashSet<String> displaySet = new HashSet<String>();

    for (int i = topTerms.size() - 1; i >= 0; i--) {

        TokenEntry entry = topTerms.get(i);
        if (!displaySet.contains(entry.getToken())) {

            if (termCnt++ <= MAX_TOKENS_LIMIT) {

                System.out.println(entry + "\t" + currentFreq.get(entry.getToken()));

                // handle queue entry
                Integer freq = 0;
                if (currentFreq.containsKey(entry.getToken())) {
                    freq = currentFreq.get(entry.getToken());
                }

                queueClient.create(entry.getToken(), 1000);
                queueClient.put(entry.getToken(), freq.toString());

                displaySet.add(entry.getToken());

            } else {

                // cleanup non-active queues
                queueClient.delete(entry.getToken());

            }
        }
    }

}

From source file:org.apache.solr.client.solrj.SolrSchemalessExampleTest.java

@Test
public void testFieldMutating() throws Exception {
    HttpSolrClient client = (HttpSolrClient) getSolrClient();
    client.deleteByQuery("*:*");
    client.commit();//from www  . ja v  a  2  s. c  om
    assertNumFound("*:*", 0); // make sure it got in
    // two docs, one with uniqueKey, another without it
    String json = "{\"name one\": \"name\"} " + "{\"name  two\" : \"name\"}" + "{\"first-second\" : \"name\"}"
            + "{\"x+y\" : \"name\"}" + "{\"p%q\" : \"name\"}" + "{\"p.q\" : \"name\"}" + "{\"a&b\" : \"name\"}";
    HttpClient httpClient = client.getHttpClient();
    HttpPost post = new HttpPost(client.getBaseURL() + "/update/json/docs");
    post.setHeader("Content-Type", "application/json");
    post.setEntity(new InputStreamEntity(new ByteArrayInputStream(json.getBytes("UTF-8")), -1));
    HttpResponse response = httpClient.execute(post);
    assertEquals(200, response.getStatusLine().getStatusCode());
    client.commit();
    List<String> expected = Arrays.asList("name_one", "name__two", "first-second", "a_b", "p_q", "p.q", "x_y");
    HashSet set = new HashSet();
    QueryResponse rsp = assertNumFound("*:*", expected.size());
    for (SolrDocument doc : rsp.getResults())
        set.addAll(doc.getFieldNames());
    for (String s : expected) {
        assertTrue(s + " not created " + rsp, set.contains(s));
    }

}

From source file:functionaltests.RestSchedulerPushPullFileTest.java

public void testIt(String spaceName, String spacePath, String destPath, boolean encode) throws Exception {
    File testPushFile = RestFuncTHelper.getDefaultJobXmlfile();
    // you can test pushing pulling a big file :
    // testPushFile = new File("path_to_a_big_file");
    File destFile = new File(new File(spacePath, destPath), testPushFile.getName());
    if (destFile.exists()) {
        destFile.delete();/*from  ww  w  .ja  v a 2 s .  c  o m*/
    }

    // PUSHING THE FILE
    String pushfileUrl = getResourceUrl("dataspace/" + spaceName + "/"
            + (encode ? URLEncoder.encode(destPath, "UTF-8") : destPath.replace("\\", "/")));
    // either we encode or we test human readable path (with no special character inside)

    HttpPost reqPush = new HttpPost(pushfileUrl);
    setSessionHeader(reqPush);
    // we push a xml job as a simple test

    MultipartEntity multipartEntity = new MultipartEntity();
    multipartEntity.addPart("fileName", new StringBody(testPushFile.getName()));
    multipartEntity.addPart("fileContent", new InputStreamBody(FileUtils.openInputStream(testPushFile),
            MediaType.APPLICATION_OCTET_STREAM, null));
    reqPush.setEntity(multipartEntity);
    HttpResponse response = executeUriRequest(reqPush);

    System.out.println(response.getStatusLine());
    assertHttpStatusOK(response);
    Assert.assertTrue(destFile + " exists for " + spaceName, destFile.exists());

    Assert.assertTrue("Original file and result are equals for " + spaceName,
            FileUtils.contentEquals(testPushFile, destFile));

    // LISTING THE TARGET DIRECTORY
    String pullListUrl = getResourceUrl("dataspace/" + spaceName + "/"
            + (encode ? URLEncoder.encode(destPath, "UTF-8") : destPath.replace("\\", "/")));

    HttpGet reqPullList = new HttpGet(pullListUrl);
    setSessionHeader(reqPullList);

    HttpResponse response2 = executeUriRequest(reqPullList);

    System.out.println(response2.getStatusLine());
    assertHttpStatusOK(response2);

    InputStream is = response2.getEntity().getContent();
    List<String> lines = IOUtils.readLines(is);
    HashSet<String> content = new HashSet<>(lines);
    System.out.println(lines);
    Assert.assertTrue("Pushed file correctly listed", content.contains(testPushFile.getName()));

    // PULLING THE FILE
    String pullfileUrl = getResourceUrl("dataspace/" + spaceName + "/"
            + (encode ? URLEncoder.encode(destPath + "/" + testPushFile.getName(), "UTF-8")
                    : destPath.replace("\\", "/") + "/" + testPushFile.getName()));

    HttpGet reqPull = new HttpGet(pullfileUrl);
    setSessionHeader(reqPull);

    HttpResponse response3 = executeUriRequest(reqPull);

    System.out.println(response3.getStatusLine());
    assertHttpStatusOK(response3);

    InputStream is2 = response3.getEntity().getContent();

    File answerFile = tmpFolder.newFile();
    FileUtils.copyInputStreamToFile(is2, answerFile);

    Assert.assertTrue("Original file and result are equals for " + spaceName,
            FileUtils.contentEquals(answerFile, testPushFile));

    // DELETING THE HIERARCHY
    String rootPath = destPath.substring(0, destPath.contains("/") ? destPath.indexOf("/") : destPath.length());
    String deleteUrl = getResourceUrl("dataspace/" + spaceName + "/"
            + (encode ? URLEncoder.encode(rootPath, "UTF-8") : rootPath.replace("\\", "/")));
    HttpDelete reqDelete = new HttpDelete(deleteUrl);
    setSessionHeader(reqDelete);

    HttpResponse response4 = executeUriRequest(reqDelete);

    System.out.println(response4.getStatusLine());

    assertHttpStatusOK(response4);

    Assert.assertTrue(destFile + " still exist", !destFile.exists());
}

From source file:org.eclipse.lyo.testsuite.oslcv2.CreationAndUpdateJsonTests.java

private void modifySomeProperty(JSONObject toUpdate, JSONObject instanceShapeResource) throws JSONException {
    JSONArray array = instanceShapeResource.getJSONArray("oslc:property");
    for (int i = 0; i < array.length(); ++i) {
        JSONObject property = array.getJSONObject(i);

        if (isPropertyReadOnly(property)) {
            continue;
        }/*from  w ww .j av  a2s.  c o  m*/

        // Like AbstractCreationAndUpdateRdfTests, let's try to keep things
        // simple and find a string property to modify. This test will fail
        // if there isn't an editable string property for the resource.
        HashSet<String> valueTypes = getValueTypes(property);

        if (valueTypes.contains(OSLCConstants.STRING_TYPE)
                || valueTypes.contains(OSLCConstants.XML_LITERAL_TYPE)) {
            String string = generateStringValue(getMaxSize(property));
            toUpdate.put(getQName(property), string);
            return;
        }
    }
}

From source file:edu.cornell.mannlib.vitro.webapp.sparql.GetClazzAllProperties.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    if (!isAuthorizedToDisplayPage(request, response, SimplePermission.USE_MISCELLANEOUS_PAGES.ACTION)) {
        return;/*w w  w.j  a v  a2 s .  c om*/
    }

    VitroRequest vreq = new VitroRequest(request);

    String vClassURI = vreq.getParameter("vClassURI");
    if (vClassURI == null || vClassURI.trim().equals("")) {
        return;
    }

    Map<String, String> hm = new HashMap();

    // Get Data Properties
    // Add rdfs:label to the list
    hm.put("label", "http://www.w3.org/2000/01/rdf-schema#label0");
    /*
     * respo += "<option>" + "<key>" + "label" + "</key>" + "<value>" +
     * "http://www.w3.org/2000/01/rdf-schema#label" + "</value>" +
     * "<type>0</type>" + "</option>";
     */
    DataPropertyDao ddao = vreq.getUnfilteredWebappDaoFactory().getDataPropertyDao();

    Collection<DataProperty> dataProps = ddao.getDataPropertiesForVClass(vClassURI);
    Iterator<DataProperty> dataPropIt = dataProps.iterator();
    HashSet<String> dpropURIs = new HashSet<String>();
    while (dataPropIt.hasNext()) {
        DataProperty dp = dataPropIt.next();
        if (!(dpropURIs.contains(dp.getURI()))) {
            dpropURIs.add(dp.getURI());
            DataProperty dprop = (DataProperty) ddao.getDataPropertyByURI(dp.getURI());
            if (dprop != null) {
                if (dprop.getLocalName() != null || !dprop.getLocalName().equals("")) {
                    hm.put(dprop.getLocalName(), dprop.getURI() + "0");
                }
                /*
                 * respo += "<option>" + "<key>" + dprop.getLocalName() +
                 * "</key>" + "<value>" + dprop.getURI() + "</value>" +
                 * "<type>0</type>" + "</option>";
                 */
            }
        }
    }

    // Get Object Properties

    ObjectPropertyDao odao = vreq.getUnfilteredWebappDaoFactory().getObjectPropertyDao();
    PropertyInstanceDao piDao = vreq.getUnfilteredWebappDaoFactory().getPropertyInstanceDao();
    VClassDao vcDao = vreq.getUnfilteredWebappDaoFactory().getVClassDao();

    // incomplete list of classes to check, but better than before
    List<String> superclassURIs = vcDao.getAllSuperClassURIs(vClassURI);
    superclassURIs.add(vClassURI);
    superclassURIs.addAll(vcDao.getEquivalentClassURIs(vClassURI));

    Map<String, PropertyInstance> propInstMap = new HashMap<String, PropertyInstance>();
    for (String classURI : superclassURIs) {
        Collection<PropertyInstance> propInsts = piDao.getAllPropInstByVClass(classURI);
        try {
            for (PropertyInstance propInst : propInsts) {
                propInstMap.put(propInst.getPropertyURI(), propInst);
            }
        } catch (NullPointerException ex) {
            continue;
        }
    }
    List<PropertyInstance> propInsts = new ArrayList<PropertyInstance>();
    propInsts.addAll(propInstMap.values());
    Collections.sort(propInsts);

    Iterator propInstIt = propInsts.iterator();
    HashSet opropURIs = new HashSet();
    while (propInstIt.hasNext()) {
        PropertyInstance pi = (PropertyInstance) propInstIt.next();
        if (!(opropURIs.contains(pi.getPropertyURI()))) {
            opropURIs.add(pi.getPropertyURI());
            ObjectProperty oprop = (ObjectProperty) odao.getObjectPropertyByURI(pi.getPropertyURI());
            if (oprop != null) {
                /*
                 * respo += "<option>" + "<key>" + oprop.getLocalName() +
                 * "</key>" + "<value>" + oprop.getURI() + "</value>" +
                 * "<type>1</type>" + "</option>";
                 */
                if (oprop.getLocalName() != null || !oprop.getLocalName().equals("")) {
                    hm.put(oprop.getLocalName(), oprop.getURI() + "1");
                }

            }
        }
    }
    String respo = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    respo += "<options>";
    Object[] keys = hm.keySet().toArray();
    Arrays.sort(keys);
    for (int i = 0; i < keys.length; i++) {
        String key = (String) keys[i];
        String value = hm.get(key);

        respo += "<option>" + "<key>" + key + "</key>" + "<value>" + value.substring(0, value.length() - 1)
                + "</value>" + "<type>" + value.charAt(value.length() - 1) + "</type>" + "</option>";
    }
    respo += "</options>";
    response.setContentType("text/xml");
    response.setCharacterEncoding("UTF-8");
    PrintWriter out = response.getWriter();

    out.println(respo);
    out.flush();
    out.close();
}

From source file:cn.vlabs.duckling.vwb.service.config.impl.DomainServiceImpl.java

private Properties compact(Map<String, String> domains) {
    Properties prop = new Properties();
    ArrayList<String> keys = new ArrayList<String>();
    keys.addAll(domains.keySet());//  w w  w .ja v  a  2  s .  co m
    Collections.sort(keys);
    int id = 0;
    HashSet<String> valueSet = new HashSet<String>();
    for (String key : keys) {
        String value = domains.get(key);
        if (!valueSet.contains(value) && isValidDomain(value)) {
            if (id == 0) {
                prop.put(KeyConstants.SITE_DOMAIN_KEY, value);
            } else {
                prop.put(KeyConstants.SITE_DOMAIN_KEY + "." + id, value);
            }
            id++;
            valueSet.add(value);
        }
    }
    return prop;
}