Example usage for java.util TreeSet add

List of usage examples for java.util TreeSet add

Introduction

In this page you can find the example usage for java.util TreeSet add.

Prototype

public boolean add(E e) 

Source Link

Document

Adds the specified element to this set if it is not already present.

Usage

From source file:net.spfbl.data.Block.java

public static String clearCIDR(String ip, int mask) {
    try {//from   ww  w .j  ava 2s. c  o m
        if (Subnet.isValidIP(ip)) {
            TreeSet<String> blockSet = new TreeSet<String>();
            String cidr;
            while ((cidr = CIDR.get(null, ip)) != null) {
                if (blockSet.contains(cidr)) {
                    throw new ProcessException("FATAL BLOCK ERROR " + cidr);
                } else if (!CIDR.split(cidr)) {
                    return cidr;
                }
                blockSet.add(cidr);
            }
            return null;
        } else {
            return null;
        }
    } catch (ProcessException ex) {
        Server.logError(ex);
        return null;
    }
}

From source file:com.cyberway.issue.crawler.admin.StatisticsTracker.java

protected Iterator<SeedRecord> getSeedRecordsSortedByStatusCode(Iterator<String> i) {
    TreeSet<SeedRecord> sortedSet = new TreeSet<SeedRecord>(new Comparator<SeedRecord>() {
        public int compare(SeedRecord sr1, SeedRecord sr2) {
            int code1 = sr1.getStatusCode();
            int code2 = sr2.getStatusCode();
            if (code1 == code2) {
                // If the values are equal, sort by URIs.
                return sr1.getUri().compareTo(sr2.getUri());
            }//  www  .  j  a  va  2 s  .c o  m
            // mirror and shift the nubmer line so as to
            // place zero at the beginning, then all negatives 
            // in order of ascending absolute value, then all 
            // positives descending
            code1 = -code1 - Integer.MAX_VALUE;
            code2 = -code2 - Integer.MAX_VALUE;

            return new Integer(code1).compareTo(new Integer(code2));
        }
    });
    while (i.hasNext()) {
        String seed = i.next();
        SeedRecord sr = (SeedRecord) processedSeedsRecords.get(seed);
        if (sr == null) {
            sr = new SeedRecord(seed, SEED_DISPOSITION_NOT_PROCESSED);
            processedSeedsRecords.put(seed, sr);
        }
        sortedSet.add(sr);
    }
    return sortedSet.iterator();
}

From source file:com.fortmoon.utils.CSVDBLoader.java

private void getColumnClass(int columnNum) throws IOException {
    log.trace("called");
    // Let's try and find uniques without running out of memory. Dump the list as soon as we find a dup.

    TreeSet<String> uniques = new TreeSet<String>();
    numLines = 1;//from  ww  w . j ava 2s  .  c om
    String line = lr.readLine();
    ColumnBean column = this.columnModel.get(columnNum);
    while (line != null) {
        String[] result = line.split("\t");
        String val = result[columnNum];
        //         String colName = this.columnNames.get(columnNum);
        if (val.isEmpty()) {
            if (log.isDebugEnabled())
                log.debug("********NULL Value for column: " + column + "*********");
            column.setNullable(true);
            column.setUnique(false);
        } else {
            if (log.isDebugEnabled())
                log.debug("Value for column: " + column.getName() + " = " + val + " line = " + line);
            SQLTYPE colType = SQLUtil.getType(val);
            if (skipBlobs && (colType == SQLTYPE.BLOB || colType == SQLTYPE.LONGBLOB))
                colType = SQLTYPE.VARCHAR;

            log.debug("SQL Type: " + colType);
            if (column.getType().getValue() > colType.getValue()) {
                column.setType(colType);
            }
            int valSize = val.length();
            if (skipBlobs && valSize > 255)
                valSize = 255;
            if (column.getColumnSize() < valSize) {
                column.setColumnSize(valSize);
                log.debug("Size for column: " + column.getName() + " = " + column.getColumnSize());
            }
            digest.update(val.getBytes());
            String hash = new String(digest.digest());
            if (uniques != null && !uniques.contains(hash))
                uniques.add(hash);
            else {
                uniques = null;
                column.setUnique(false);
            }

        }
        line = lr.readLine();
        numLines++;
        if (numLines % 100000 == 0)
            log.debug("Finished processing number of lines in first pass scan: " + numLines);
    }
}

From source file:br.bireme.ngrams.NGrams.java

private static Set<String> results2json(final Parameters parameters, final Set<Result> results) {
    assert parameters != null;
    assert results != null;

    String name;//from w ww . j a  v  a 2 s  . c o  m
    String doc;
    final StringBuilder builder = new StringBuilder();
    final TreeSet<String> ret = new TreeSet<>();

    for (Result result : results) {
        builder.setLength(0);
        builder.append("{");
        name = parameters.db.name;
        doc = result.doc.get(name).replace('\"', '\'');
        builder.append(" \"").append(name).append("\":\"").append(doc).append("\",");
        name = parameters.id.name;
        doc = result.doc.get(name).replace('\"', '\'');
        builder.append(" \"").append(name).append("\":\"").append(doc).append("\",");
        name = parameters.indexed.name;
        doc = result.doc.get(name).replace('\"', '\'');
        builder.append(" \"").append(name).append("\":\"").append(doc).append("\"");
        for (ExactField exact : parameters.exacts) {
            name = exact.name;
            doc = result.doc.get(name).replace('\"', '\'');
            builder.append(", \"").append(name).append("\":\"").append(doc).append("\"");
        }
        for (ExactField exact : parameters.exacts) {
            name = exact.name;
            doc = result.doc.get(name).replace('\"', '\'');
            builder.append(", \"").append(name).append("\":\"").append(doc).append("\"");
        }
        for (NGramField ngrams : parameters.ngrams) {
            name = ngrams.name;
            doc = result.doc.get(name).replace('\"', '\'');
            builder.append(", \"").append(name).append("\":\"").append(doc).append("\"");
        }
        for (RegExpField regexps : parameters.regexps) {
            name = regexps.name;
            doc = result.doc.get(name).replace('\"', '\'');
            builder.append(", \"").append(name).append("\":\"").append(doc).append("\"");
        }
        for (NoCompareField nocompare : parameters.nocompare) {
            name = nocompare.name;
            doc = result.doc.get(name).replace('\"', '\'');
            builder.append(", \"").append(name).append("\":\"").append(doc).append("\"");
        }
        builder.append(", \"score\":\"").append(result.score).append("\"");
        builder.append(" }");
        ret.add(builder.toString());
    }

    return ret.descendingSet();
}

From source file:net.spfbl.data.Block.java

public static void clear(User user, String token, String name) {
    try {/*from  w  w w  .  java 2s  .c om*/
        String block;
        if (SubnetIPv4.isValidIPv4(token)) {
            String ip = SubnetIPv4.normalizeIPv4(token);
            if ((block = Block.clearCIDR(ip, 32)) != null) {
                Server.logInfo("false positive BLOCK '" + block + "' detected by '" + name + "'.");
            }
        } else if (SubnetIPv6.isValidIPv6(token)) {
            String ip = SubnetIPv6.normalizeIPv6(token);
            if ((block = Block.clearCIDR(ip, 64)) != null) {
                Server.logInfo("false positive BLOCK '" + block + "' detected by '" + name + "'.");
            }
        }
        TreeSet<String> blockSet = new TreeSet<String>();
        while ((block = Block.find(null, user, token, false, true, true, false)) != null) {
            if (blockSet.contains(block)) {
                throw new ProcessException("FATAL BLOCK ERROR " + block);
            } else if (Block.drop(block)) {
                Server.logInfo("false positive BLOCK '" + block + "' detected by '" + name + "'.");
            }
            blockSet.add(block);
        }
    } catch (ProcessException ex) {
        Server.logError(ex);
    }
}

From source file:net.spfbl.data.Block.java

public static void clearHREF(User user, String token, String name) {
    try {//  ww w. ja  v  a 2s.c  om
        TreeSet<String> blockSet = new TreeSet<String>();
        String block;
        while ((block = Block.findHREF(null, user, token, false)) != null) {
            if (blockSet.contains(block)) {
                throw new ProcessException("FATAL BLOCK ERROR " + block);
            } else if (Block.drop(block)) {
                Server.logInfo("false positive BLOCK '" + block + "' detected by '" + name + "'.");
            }
            blockSet.add(block);
        }
    } catch (ProcessException ex) {
        Server.logError(ex);
    }
}

From source file:com.bt.download.android.gui.Librarian.java

private Set<Integer> getSharedFiles(byte fileType) {
    TreeSet<Integer> result = new TreeSet<Integer>();
    List<Integer> delete = new ArrayList<Integer>();

    Cursor c = null;/*from   w w w  .j  a va  2s  .c om*/

    try {
        ContentResolver cr = context.getContentResolver();
        String[] columns = new String[] { SharingColumns.FILE_ID, SharingColumns._ID };
        c = cr.query(Sharing.Media.CONTENT_URI, columns,
                SharingColumns.SHARED + "=1 AND " + SharingColumns.FILE_TYPE + "=?",
                new String[] { String.valueOf(fileType) }, null);

        if (c == null || !c.moveToFirst()) {
            return result;
        }

        int fileIdCol = c.getColumnIndex(SharingColumns.FILE_ID);
        int sharingIdCol = c.getColumnIndex(SharingColumns._ID);

        Pair<List<Integer>, List<String>> pair = getAllFiles(fileType);
        List<Integer> files = pair.first;
        List<String> paths = pair.second;

        do {
            int fileId = c.getInt(fileIdCol);
            int sharingId = c.getInt(sharingIdCol);

            int index = Collections.binarySearch(files, fileId);

            try {
                if (index >= 0) {
                    File f = new File(paths.get(index));
                    if (f.exists() && f.isFile()) {
                        result.add(fileId);
                    } else {
                        delete.add(sharingId);
                    }
                } else {
                    delete.add(sharingId);
                }
            } catch (Throwable e) {
                Log.e(TAG, "Error checking fileId: " + fileId + ", fileType: " + fileId);
            }
        } while (c.moveToNext());

    } catch (Throwable e) {
        Log.e(TAG, "General failure getting shared/unshared files ids", e);
    } finally {
        if (c != null) {
            c.close();
        }

        if (delete.size() > 0) {
            deleteSharedStates(delete);
        }
    }

    return result;
}

From source file:net.spfbl.data.Block.java

public static TreeSet<String> get(Client client, User user) throws ProcessException {
    TreeSet<String> blockSet = new TreeSet<String>();
    // Definio do e-mail do usurio.
    String userEmail = null;//from w  w w  . j  ava 2  s .  co  m
    if (user != null) {
        userEmail = user.getEmail();
    } else if (client != null) {
        userEmail = client.getEmail();
    }
    if (userEmail != null) {
        for (String token : get(userEmail)) {
            int index = token.indexOf(':') + 1;
            token = token.substring(index);
            blockSet.add(token);
        }
    }
    return blockSet;
}

From source file:net.spfbl.data.Block.java

public static void clear(String token, String name) {
    try {/*ww  w .  jav a2 s.c  o m*/
        if (SubnetIPv4.isValidIPv4(token)) {
            String ip = SubnetIPv4.normalizeIPv4(token);
            if (Block.clearCIDR(ip, 32) != null) {
                Server.logInfo("false positive BLOCK '" + ip + "/32' detected by '" + name + "'.");
            }
        } else if (SubnetIPv6.isValidIPv6(token)) {
            String ip = SubnetIPv6.normalizeIPv6(token);
            if (Block.clearCIDR(ip, 64) != null) {
                Server.logInfo("false positive BLOCK '" + ip + "/64' detected by '" + name + "'.");
            }
        }
        TreeSet<String> blockSet = new TreeSet<String>();
        String block;
        while ((block = Block.find(null, null, token, false, true, true, false)) != null) {
            if (blockSet.contains(block)) {
                throw new ProcessException("FATAL BLOCK ERROR " + block);
            } else if (Block.drop(block)) {
                Server.logInfo("false positive BLOCK '" + block + "' detected by '" + name + "'.");
            }
            blockSet.add(block);
        }
    } catch (ProcessException ex) {
        Server.logError(ex);
    }
}

From source file:org.dasein.cloud.aws.platform.SimpleDB.java

@Override
public Map<String, Set<KeyValuePair>> query(String queryString, boolean consistentRead)
        throws CloudException, InternalException {
    APITrace.begin(provider, "KVDB.query");
    try {/*from   www . jav  a  2s.co m*/
        Map<String, Set<KeyValuePair>> pairs = new HashMap<String, Set<KeyValuePair>>();
        String marker = null;

        do {
            Map<String, String> parameters = provider.getStandardSimpleDBParameters(provider.getContext(),
                    SELECT);
            NodeList blocks;
            EC2Method method;
            Document doc;

            if (marker != null) {
                parameters.put("NextToken", marker);
            }
            parameters.put("SelectExpression", queryString);
            parameters.put("ConsistentRead", String.valueOf(consistentRead));
            method = new EC2Method(SERVICE_ID, provider, parameters);
            try {
                doc = method.invoke();
            } catch (EC2Exception e) {
                String code = e.getCode();

                if (code != null && code.equals("NoSuchDomain")) {
                    return null;
                }
                throw new CloudException(e);
            }
            marker = null;
            blocks = doc.getElementsByTagName("NextToken");
            if (blocks.getLength() > 0) {
                for (int i = 0; i < blocks.getLength(); i++) {
                    Node item = blocks.item(i);

                    if (item.hasChildNodes()) {
                        marker = item.getFirstChild().getNodeValue().trim();
                    }
                }
                if (marker != null) {
                    break;
                }
            }
            blocks = doc.getElementsByTagName("Item");
            for (int i = 0; i < blocks.getLength(); i++) {
                Node item = blocks.item(i);

                if (item.hasChildNodes()) {
                    TreeSet<KeyValuePair> itemPairs = new TreeSet<KeyValuePair>();
                    NodeList children = item.getChildNodes();
                    String itemId = null;

                    for (int j = 0; j < children.getLength(); j++) {
                        Node child = children.item(j);

                        if (child.hasChildNodes()) {
                            String nn = child.getNodeName();

                            if (nn.equals("Name")) {
                                itemId = child.getFirstChild().getNodeValue();
                            } else if (nn.equals("Attribute")) {
                                NodeList parts = child.getChildNodes();
                                String key = null, value = null;

                                for (int k = 0; k < parts.getLength(); k++) {
                                    Node part = parts.item(k);

                                    if (part.hasChildNodes()) {
                                        String nv = part.getFirstChild().getNodeValue();

                                        if (part.getNodeName().equals("Name")) {
                                            key = nv;
                                        } else if (part.getNodeName().equals("Value")) {
                                            value = nv;
                                        }
                                    }
                                }
                                if (key != null) {
                                    itemPairs.add(new KeyValuePair(key, value));
                                }
                            }
                        }
                    }
                    if (itemId != null) {
                        pairs.put(itemId, itemPairs);
                    }
                }
            }
        } while (marker != null);
        return pairs;
    } finally {
        APITrace.end();
    }
}