Example usage for java.util Set contains

List of usage examples for java.util Set contains

Introduction

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

Prototype

boolean contains(Object o);

Source Link

Document

Returns true if this set contains the specified element.

Usage

From source file:Main.java

protected static String nodeToString(Node node, Set<String> parentPrefixes, String namespaceURI)
        throws Exception {
    StringBuilder b = new StringBuilder();

    if (node == null) {
        return "";
    }/*from  ww w  . ja v  a2  s  .  co m*/

    if (node instanceof Element) {
        Element element = (Element) node;
        b.append("<");
        b.append(element.getNodeName());

        Map<String, String> thisLevelPrefixes = new HashMap();
        if (element.getPrefix() != null && !parentPrefixes.contains(element.getPrefix())) {
            thisLevelPrefixes.put(element.getPrefix(), element.getNamespaceURI());
        }

        if (element.hasAttributes()) {
            NamedNodeMap map = element.getAttributes();
            for (int i = 0; i < map.getLength(); i++) {
                Node attr = map.item(i);
                if (attr.getNodeName().startsWith("xmlns"))
                    continue;
                if (attr.getPrefix() != null && !parentPrefixes.contains(attr.getPrefix())) {
                    thisLevelPrefixes.put(attr.getPrefix(), element.getNamespaceURI());
                }
                b.append(" ");
                b.append(attr.getNodeName());
                b.append("=\"");
                b.append(attr.getNodeValue());
                b.append("\"");
            }
        }

        if (namespaceURI != null && !thisLevelPrefixes.containsValue(namespaceURI)
                && !namespaceURI.equals(element.getParentNode().getNamespaceURI())) {
            b.append(" xmlns=\"").append(namespaceURI).append("\"");
        }

        for (Map.Entry<String, String> entry : thisLevelPrefixes.entrySet()) {
            b.append(" xmlns:").append(entry.getKey()).append("=\"").append(entry.getValue()).append("\"");
            parentPrefixes.add(entry.getKey());
        }

        NodeList children = element.getChildNodes();
        boolean hasOnlyAttributes = true;
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child.getNodeType() != Node.ATTRIBUTE_NODE) {
                hasOnlyAttributes = false;
                break;
            }
        }
        if (!hasOnlyAttributes) {
            b.append(">");
            for (int i = 0; i < children.getLength(); i++) {
                b.append(nodeToString(children.item(i), parentPrefixes, children.item(i).getNamespaceURI()));
            }
            b.append("</");
            b.append(element.getNodeName());
            b.append(">");
        } else {
            b.append("/>");
        }

        for (String thisLevelPrefix : thisLevelPrefixes.keySet()) {
            parentPrefixes.remove(thisLevelPrefix);
        }

    } else if (node.getNodeValue() != null) {
        b.append(encodeText(node, node.getNodeValue()));
    }

    return b.toString();
}

From source file:StringUtil.java

/**
 * <p>Removes specified chars from a string.</p>
 *
 * @param aString the string that will be examined to remove chars
 * @param unWantedCharArray the char array containing the chars
 * that should be removed from a string//from   w w w.j a  va 2  s.  co  m
 * @return the string after removing the specified chars
 */
public static String removeCharsFromString(String aString, char[] unWantedCharArray) {

    Character character = null;

    // Store unwanted chars in a hashset
    Set unWantedCharSet = new HashSet();

    for (int i = 0; i < unWantedCharArray.length; i++) {
        character = new Character(unWantedCharArray[i]);
        unWantedCharSet.add(character);
    }

    // Create result String buffer
    StringBuffer result = new StringBuffer(aString.length());

    // For each character in aString, append it to the result string buffer
    // if it is not in unWantedCharSet
    for (int i = 0; i < aString.length(); i++) {
        character = new Character(aString.charAt(i));
        if (!unWantedCharSet.contains(character)) {
            result.append(aString.charAt(i));
        }
    }

    // Return result
    return result.toString();
}

From source file:Main.java

public static List<Element> elements(final Element element, final Set<String> allowedTagNames) {
    List<Element> elements = null;
    final NodeList nodeList = element.getChildNodes();
    if (nodeList != null) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            final Node child = nodeList.item(i);
            if (Element.class.isAssignableFrom(child.getClass())) {
                final Element childElement = (Element) child;
                final String childTagName = getTagLocalName(childElement);
                if (allowedTagNames.contains(childTagName)) {
                    if (elements == null) {
                        elements = new ArrayList<Element>();
                    }/*  w w  w. j  a v  a2s.  com*/
                    elements.add(childElement);
                }
            }
        }
    }
    return elements;
}

From source file:fr.lirmm.graphik.graal.io.owl.OWL2Parser.java

private static InMemoryAtomSet removeUselessTopInBody(InMemoryAtomSet atomset) {
    InMemoryAtomSet newAtomset = new DefaultInMemoryGraphAtomSet();
    Iterator<Atom> it = atomset.iterator();
    Atom a;/*from ww w.j a  v  a  2s  .  c o m*/
    while (it.hasNext()) {
        a = it.next();
        if (!a.getPredicate().equals(Predicate.TOP)) {
            newAtomset.add(a);
            it.remove();
        } else {

        }
    }

    // for each top predicate
    Set<Term> terms = newAtomset.getTerms();
    it = atomset.iterator();
    while (it.hasNext()) {
        a = it.next();
        if (!terms.contains(a.getTerm(0))) {
            newAtomset.add(a);
        }
    }

    return newAtomset;
}

From source file:com.piketec.jenkins.plugins.tpt.TptPluginSlaveExecutor.java

/**
 * Matches the tests cases from a test set with all the test cases found.
 * /*  w ww. ja va2  s  .c  om*/
 * @param scenColl1
 * @param scenCol2
 * @return the intersected test cases
 * @throws RemoteException
 * @throws ApiException
 */
static Collection<Scenario> intersectByHash(Collection<Scenario> scenColl1, Collection<Scenario> scenCol2)
        throws RemoteException, ApiException {
    Set<String> scenCol1Names = new HashSet<>();
    ArrayList<Scenario> result = new ArrayList<>();
    for (Scenario scen : scenColl1) {
        scenCol1Names.add(scen.getName());
    }
    for (Scenario scen : scenCol2) {
        if (scenCol1Names.contains(scen.getName())) {
            result.add(scen);
        }
    }
    return result;
}

From source file:com.asakusafw.testdriver.compiler.util.DeploymentUtil.java

private static void mergeEntries(ZipOutputStream zip, File file, Set<String> saw) throws IOException {
    try (ZipInputStream in = new ZipInputStream(new BufferedInputStream(new FileInputStream(file)))) {
        while (true) {
            ZipEntry entry = in.getNextEntry();
            if (entry == null) {
                break;
            }//from   w w  w  .  ja v a  2  s. c o  m
            if (saw.contains(entry.getName())) {
                continue;
            }
            if (LOG.isTraceEnabled()) {
                LOG.trace("Copy into archive: {} -> {}", entry.getName(), file);
            }
            saw.add(entry.getName());
            zip.putNextEntry(new ZipEntry(entry.getName()));
            copyStream(in, zip);
        }
    }
}

From source file:com.palantir.ptoss.cinch.core.BindingContext.java

private static <T> Map<String, T> dotIndex(Collection<T> items, Function<T, String> qualifierFn,
        Function<T, String> blindFn) {
    Set<String> ambiguousNames = Sets.newHashSet();
    Map<String, T> results = Maps.newHashMap();
    for (T item : items) {
        String blindKey = blindFn.apply(item);
        if (!ambiguousNames.contains(blindKey)) {
            if (results.containsKey(blindKey)) {
                results.remove(blindKey);
                ambiguousNames.add(blindKey);
            } else {
                results.put(blindKey, item);
            }// w  w  w.j  a v  a2s .  c om
        }
        String qualifiedKey = qualifierFn.apply(item) + "." + blindKey;
        results.put(qualifiedKey, item);
    }
    return results;
}

From source file:hudson.util.CascadingUtil.java

/**
 * Checks whether cascadingCandidate project can produce cycle cascading dependencies.
 *
 * @param cascadingCandidate candidate./* w w w  .  ja v  a 2  s  . co m*/
 * @param cascadingChildren children of given job.
 * @return false - if cyclic cascading dependency is not possible, true - otherwise.
 */
@SuppressWarnings("unchecked")
public static boolean hasCyclicCascadingLink(Job cascadingCandidate, Set<String> cascadingChildren) {
    if (null != cascadingCandidate && CollectionUtils.isNotEmpty(cascadingChildren)) {
        if (cascadingChildren.contains(cascadingCandidate.getName())) {
            return true;
        }
        for (String childName : cascadingChildren) {
            Job job = Functions.getItemByName(Hudson.getInstance().getAllItems(Job.class), childName);
            if (hasCyclicCascadingLink(cascadingCandidate, job.getCascadingChildrenNames())) {
                return true;
            }
        }
    }
    return false;
}

From source file:ClassFileUtilities.java

private static void computeClassDependencies(InputStream is, Set classpath, Set done, Set result, boolean rec)
        throws IOException {

    Iterator it = getClassDependencies(is).iterator();
    while (it.hasNext()) {
        String s = (String) it.next();
        if (!done.contains(s)) {
            done.add(s);//w  w w  .  j  av  a  2 s .c o  m

            Iterator cpit = classpath.iterator();
            while (cpit.hasNext()) {
                InputStream depis = null;
                String path = null;
                Object cpEntry = cpit.next();
                if (cpEntry instanceof JarFile) {
                    JarFile jarFile = (JarFile) cpEntry;
                    String classFileName = s + ".class";
                    ZipEntry ze = jarFile.getEntry(classFileName);
                    if (ze != null) {
                        path = jarFile.getName() + '!' + classFileName;
                        depis = jarFile.getInputStream(ze);
                    }
                } else {
                    path = ((String) cpEntry) + '/' + s + ".class";
                    File f = new File(path);
                    if (f.isFile()) {
                        depis = new FileInputStream(f);
                    }
                }

                if (depis != null) {
                    result.add(path);

                    if (rec) {
                        computeClassDependencies(depis, classpath, done, result, rec);
                    }
                }
            }
        }
    }
}

From source file:com.github.cbismuth.random.FastRandom.java

/**
 * Extracts random values from a source array the quickest way:
 * in-place push picked elements to the end of the array
 * and duplicates are searched from their hash code.
 *
 * @param src the source array/*  www  .  ja va 2 s . c  o m*/
 * @param p the length of the random array to return
 * @param <T> the type of elements from the source array
 *
 * @return an array with random elements from the source array without duplicates
 *
 * @throws IllegalAccessException if p == 0 or p > src.length
 */
public static <T> T[] quickRandomOfLength_withHash(final T[] src, final int p) throws IllegalAccessException {
    checkPreconditions(src, p);

    @SuppressWarnings("unchecked")
    final T[] dest = (T[]) new Object[p];
    int indexOfCurrentDestValueToSet = 0;

    int nbOfRandomCalls = 0;

    final Set<Integer> hashCodes = new HashSet<>(p);

    // while destination array is not filled
    while (indexOfCurrentDestValueToSet != p && nbOfRandomCalls < src.length) {
        final int pickedIndex = convertRandomValueToIndex(src);

        final T pickedElement = src[pickedIndex];

        final int hashCode = pickedElement.hashCode();
        final boolean isDuplicate = hashCodes.contains(hashCode);

        // add to destination array if not a duplicate
        if (!isDuplicate) {
            dest[indexOfCurrentDestValueToSet] = pickedElement;
            hashCodes.add(hashCode);
            indexOfCurrentDestValueToSet++;
        }

        // SMART part - swap processed element if duplicate or not
        swapWithLastUnpickedElement(src, pickedIndex, nbOfRandomCalls);
        nbOfRandomCalls++;
    }

    if (indexOfCurrentDestValueToSet != p) {
        throw new TooMuchDuplicatesException("Not enough distinct values in source array!");
    }

    return dest;
}