Example usage for java.util NavigableSet add

List of usage examples for java.util NavigableSet add

Introduction

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

Prototype

boolean add(E e);

Source Link

Document

Adds the specified element to this set if it is not already present (optional operation).

Usage

From source file:org.cretz.sbnstat.gource.Gourcer.java

@Override
public void run(Arguments args) {
    try {//from   www .j a v  a 2 s  .c om
        //ok, we're gonna dump to a custom file format
        //dates
        Calendar from = DateUtils.toBeginningOfDayCalendar(args.getFrom());
        Calendar to = DateUtils.toEndOfDayCalendar(args.getTo());
        logger.info("Dumping gource custom log from {} to {}", from.getTime(), to.getTime());
        //build connection
        Connection conn = JdbcUtils.connectToMySqlDatabase(args.getDatabaseHost(), args.getDatabasePort(),
                args.getDatabaseName(), args.getDatabaseUser(), args.getDatabasePass());
        //maps to hold data
        final NavigableSet<CommentState> allComments = new TreeSet<CommentState>();
        final Map<Long, CommentState> mappedComments = new HashMap<Long, CommentState>();
        final Map<String, PostState> mappedPosts = new HashMap<String, PostState>();
        try {
            //load data
            new SbnStatDao(conn).readComments(new Timestamp(from.getTimeInMillis()),
                    new Timestamp(to.getTimeInMillis()), new CommentHandler() {
                        @Override
                        public void onComment(CommentInfo info) throws Exception {
                            PostState post = mappedPosts.get(info.getPostUrl());
                            if (post == null) {
                                post = new PostState(info.getPostUrl(), info.getPostTitle(),
                                        info.getPostUsername(), info.getPostDate());
                                mappedPosts.put(post.url, post);
                            }
                            CommentState state = new CommentState(info.getId(), post,
                                    mappedComments.get(info.getParentId()), info.getDate(),
                                    info.getCommentUsername(), info.getSubject(),
                                    info.getCommentUserThumbnail());
                            allComments.add(state);
                            mappedComments.put(state.id, state);
                        }
                    });
        } finally {
            JdbcUtils.closeQuietly(conn);
        }
        //loop through and write lines
        PrintStream out;
        if (args.getOut() != null) {
            out = new PrintStream(new File(args.getOut()));
        } else {
            out = System.out;
        }
        try {
            Set<String> processedPosts = new HashSet<String>();
            File dir = args.getDir() == null ? null : new File(args.getDir());
            if (dir != null && !dir.exists()) {
                dir.mkdirs();
            }
            List<String> lines = new ArrayList<String>();
            for (CommentState comment : allComments) {
                //let's grab the user avatar image
                if (dir != null && comment.thumbnail != null) {
                    saveUserAvatar(dir, comment);
                }
                String title;
                if (comment.post.title != null) {
                    title = comment.post.title;
                } else {
                    title = "";
                }
                if (!processedPosts.contains(comment.post.url)) {
                    //add a post
                    lines.add(getGourceLine(comment.post.date, comment.post.username, 'A',
                            "/" + title.replace('/', ' ')));
                    processedPosts.add(comment.post.url);
                }
                //make the parent string (bad performance)
                String path = "";
                CommentState parent = comment;
                while (parent != null) {
                    path = "/" + parent.subject.replace('/', ' ') + path;
                    parent = parent.parent;
                }
                lines.add(getGourceLine(comment.date, comment.username, comment.parent == null ? 'A' : 'M',
                        "/" + title.replace('/', ' ') + path));
            }
            Collections.sort(lines);
            for (String line : lines) {
                out.println(line);
            }
        } finally {
            Closeables.closeQuietly(out);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.google.gwt.emultest.java.util.TreeSetTest.java

/**
 * Test method for 'java.util.SortedSet.subSet(Object, Object)' and
 * 'java.util.NavigableSet.subSet(Object, boolean, Object, boolean)'.
 *
 * @see java.util.SortedSet#subSet(Object, Object)
 * @see java.util.NavigableSet#subSet(Object, boolean, Object, boolean)
 */// w  ww.  j a  v a 2 s. co m
public void testSubSet() {
    NavigableSet<E> sortedSet = createNavigableSet();
    // test with no entries
    assertEquals(0, sortedSet.subSet(getKeys()[0], getKeys()[0]).size());
    assertEquals(0, sortedSet.subSet(getKeys()[0], false, getKeys()[0], false).size());
    assertEquals(0, sortedSet.subSet(getKeys()[0], true, getKeys()[0], false).size());
    assertEquals(0, sortedSet.subSet(getKeys()[0], false, getKeys()[0], true).size());
    assertEquals(0, sortedSet.subSet(getKeys()[0], true, getKeys()[0], true).size());

    // test with a single entry set
    sortedSet.add(getKeys()[0]);
    assertEquals(0, sortedSet.subSet(getKeys()[0], getKeys()[0]).size());
    // bounded by a "wide" range
    assertEquals(1, sortedSet.subSet(getLessThanMinimumKey(), getGreaterThanMaximumKey()).size());
    assertEquals(1, sortedSet.subSet(getLessThanMinimumKey(), false, getGreaterThanMaximumKey(), false).size());
    assertEquals(1, sortedSet.subSet(getLessThanMinimumKey(), true, getGreaterThanMaximumKey(), false).size());
    assertEquals(1, sortedSet.subSet(getLessThanMinimumKey(), false, getGreaterThanMaximumKey(), true).size());
    assertEquals(1, sortedSet.subSet(getLessThanMinimumKey(), true, getGreaterThanMaximumKey(), true).size());

    // test with two entry set
    sortedSet.add(getKeys()[1]);

    assertEquals(1, sortedSet.subSet(getKeys()[0], getKeys()[1]).size());
    assertEquals(getKeys()[0], sortedSet.subSet(getKeys()[0], getKeys()[1]).toArray()[0]);

    assertEquals(0, sortedSet.subSet(getKeys()[0], false, getKeys()[1], false).size());

    assertEquals(1, sortedSet.subSet(getKeys()[0], false, getKeys()[1], true).size());
    assertEquals(getKeys()[1], sortedSet.subSet(getKeys()[0], false, getKeys()[1], true).toArray()[0]);

    assertEquals(1, sortedSet.subSet(getKeys()[0], true, getKeys()[1], false).size());
    assertEquals(getKeys()[0], sortedSet.subSet(getKeys()[0], true, getKeys()[1], false).toArray()[0]);

    assertEquals(2, sortedSet.subSet(getKeys()[0], true, getKeys()[1], true).size());
    assertEquals(getKeys()[0], sortedSet.subSet(getKeys()[0], true, getKeys()[1], true).toArray()[0]);
    assertEquals(getKeys()[1], sortedSet.subSet(getKeys()[0], true, getKeys()[1], true).toArray()[1]);

    // bounded by a "wide" range
    SortedSet<E> subSet = sortedSet.subSet(getLessThanMinimumKey(), getGreaterThanMaximumKey());

    assertEquals(2, subSet.size());

    assertEquals(2, sortedSet.subSet(getLessThanMinimumKey(), false, getGreaterThanMaximumKey(), false).size());
    assertEquals(1, sortedSet.subSet(getKeys()[0], false, getGreaterThanMaximumKey(), false).size());
    assertEquals(0, sortedSet.subSet(getKeys()[0], false, getKeys()[1], false).size());
    assertEquals(2, sortedSet.subSet(getKeys()[0], true, getGreaterThanMaximumKey(), false).size());
    assertEquals(1, sortedSet.subSet(getKeys()[0], true, getKeys()[1], false).size());
    assertEquals(2, sortedSet.subSet(getKeys()[0], true, getGreaterThanMaximumKey(), true).size());
    assertEquals(2, sortedSet.subSet(getKeys()[0], true, getKeys()[1], true).size());
}

From source file:com.streamsets.pipeline.stage.origin.remote.FTPRemoteDownloadSourceDelegate.java

@Override
void queueFiles(FileQueueChecker fqc, NavigableSet<RemoteFile> fileQueue, FileFilter fileFilter)
        throws IOException, StageException {
    boolean done = false;
    int retryCounter = 0;
    while (!done && retryCounter < MAX_RETRIES) {
        try {/*from   w ww .j  av a  2s .c o m*/
            remoteDir.refresh();
            // A call to getChildren() and then refresh() is needed in order to properly refresh if files were updated
            // A possible bug in VFS?
            remoteDir.getChildren();
            remoteDir.refresh();
            done = true;
        } catch (FileSystemException fse) {
            // Refresh can fail due to session is down, a timeout, etc; so try getting a new connection
            if (retryCounter < MAX_RETRIES - 1) {
                LOG.info("Got FileSystemException when trying to refresh remote directory. '{}'",
                        fse.getMessage(), fse);
                LOG.warn("Retrying connection to remote directory");
                remoteDir = VFS.getManager().resolveFile(remoteURI.toString(), options);
            } else {
                throw new StageException(Errors.REMOTE_18, fse.getMessage(), fse);
            }
        }
        retryCounter++;
    }

    FileObject[] theFiles = remoteDir.getChildren();
    if (conf.processSubDirectories) {
        theFiles = ArrayUtils.addAll(theFiles, remoteDir.findFiles(fileFilter));
    }

    for (FileObject file : theFiles) {
        String path = relativizeToRoot(file.getName().getPath());
        LOG.debug("Checking {}", path);
        if (file.getType() != FileType.FILE) {
            LOG.trace("Skipping {} because it is not a file", path);
            continue;
        }

        //check if base name matches - not full path.
        Matcher matcher = fileFilter.getRegex().matcher(file.getName().getBaseName());
        if (!matcher.matches()) {
            LOG.trace("Skipping {} because it does not match the regex", path);
            continue;
        }

        RemoteFile tempFile = new FTPRemoteFile(path, getModTime(file), file);
        if (fqc.shouldQueue(tempFile)) {
            LOG.debug("Queuing file {} with modtime {}", tempFile.getFilePath(), tempFile.getLastModified());
            // If we are done with all files, the files with the final mtime might get re-ingested over and over.
            // So if it is the one of those, don't pull it in.
            fileQueue.add(tempFile);
        }
    }
}

From source file:org.alfresco.extension.bulkimport.source.fs.DirectoryAnalyser.java

private final NavigableSet<FilesystemBulkImportItemVersion> constructImportItemVersions(
        final SortedMap<BigDecimal, Pair<File, File>> itemVersions) throws InterruptedException {
    // PRECONDITIONS
    if (itemVersions == null)
        throw new IllegalArgumentException("itemVersions cannot be null.");
    if (itemVersions.size() <= 0)
        throw new IllegalArgumentException("itemVersions cannot be empty.");

    // Body/*  w  w  w  .j a  va 2  s  .c  om*/
    final NavigableSet<FilesystemBulkImportItemVersion> result = new TreeSet<>();

    for (final BigDecimal versionNumber : itemVersions.keySet()) {
        if (importStatus.isStopping() || Thread.currentThread().isInterrupted())
            throw new InterruptedException(
                    Thread.currentThread().getName() + " was interrupted. Terminating early.");

        final Pair<File, File> contentAndMetadataFiles = itemVersions.get(versionNumber);
        final FilesystemBulkImportItemVersion version = new FilesystemBulkImportItemVersion(serviceRegistry,
                configuredContentStore, metadataLoader, versionNumber, contentAndMetadataFiles.getFirst(),
                contentAndMetadataFiles.getSecond());

        result.add(version);
    }

    return (result);
}

From source file:org.alfresco.extension.bulkimport.source.sample.SampleSourceImportItem.java

private final static NavigableSet<SampleSourceImportItemVersion> synthesiseVersions(final String name,
        final boolean isDirectory, final int numVersions) {
    NavigableSet<SampleSourceImportItemVersion> result = new TreeSet<>();

    // Add at least one version
    result.add(new SampleSourceImportItemVersion(name, isDirectory, BigDecimal.ONE));

    if (!isDirectory && numVersions > 1) {
        for (int i = 1; i < numVersions; i++) {
            result.add(new SampleSourceImportItemVersion(name, isDirectory, BigDecimal.valueOf(i + 1)));
        }/*  w  ww  . j  ava 2 s  . c  om*/
    }

    return (result);
}

From source file:org.apache.hadoop.hbase.client.Get.java

/**
 * Get the column from the specific family with the specified qualifier.
 * <p>// w  w  w  .jav a  2  s  . co m
 * Overrides previous calls to addFamily for this family.
 * @param family family name
 * @param qualifier column qualifier
 * @return the Get objec
 */
public Get addColumn(byte[] family, byte[] qualifier) {
    NavigableSet<byte[]> set = familyMap.get(family);
    if (set == null) {
        set = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR);
    }
    if (qualifier == null) {
        qualifier = HConstants.EMPTY_BYTE_ARRAY;
    }
    set.add(qualifier);
    familyMap.put(family, set);
    return this;
}

From source file:org.apache.hadoop.hbase.client.Scan.java

/**
 * Get the column from the specified family with the specified qualifier.
 * <p>//from   ww w. java2 s . c om
 * Overrides previous calls to addFamily for this family.
 * @param family family name
 * @param qualifier column qualifier
 * @return this
 */
public Scan addColumn(byte[] family, byte[] qualifier) {
    NavigableSet<byte[]> set = familyMap.get(family);
    if (set == null) {
        set = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR);
    }
    if (qualifier == null) {
        qualifier = HConstants.EMPTY_BYTE_ARRAY;
    }
    set.add(qualifier);
    familyMap.put(family, set);
    return this;
}

From source file:org.apache.hadoop.hbase.HBaseTestingUtility.java

public static NavigableSet<String> getAllOnlineRegions(MiniHBaseCluster cluster) throws IOException {
    NavigableSet<String> online = new TreeSet<String>();
    for (RegionServerThread rst : cluster.getLiveRegionServerThreads()) {
        try {/*from   ww  w  .j ava 2s .  co m*/
            for (HRegionInfo region : ProtobufUtil.getOnlineRegions(rst.getRegionServer().getRSRpcServices())) {
                online.add(region.getRegionNameAsString());
            }
        } catch (RegionServerStoppedException e) {
            // That's fine.
        }
    }
    for (MasterThread mt : cluster.getLiveMasterThreads()) {
        try {
            for (HRegionInfo region : ProtobufUtil.getOnlineRegions(mt.getMaster().getRSRpcServices())) {
                online.add(region.getRegionNameAsString());
            }
        } catch (RegionServerStoppedException e) {
            // That's fine.
        }
    }
    return online;
}

From source file:org.apache.hadoop.hbase.master.TableNamespaceManager.java

public synchronized NavigableSet<NamespaceDescriptor> list() throws IOException {
    NavigableSet<NamespaceDescriptor> ret = Sets
            .newTreeSet(NamespaceDescriptor.NAMESPACE_DESCRIPTOR_COMPARATOR);
    ResultScanner scanner = getNamespaceTable().getScanner(HTableDescriptor.NAMESPACE_FAMILY_INFO_BYTES);
    try {/* ww  w .j  av a  2 s .  c o  m*/
        for (Result r : scanner) {
            byte[] val = CellUtil.cloneValue(r.getColumnLatestCell(HTableDescriptor.NAMESPACE_FAMILY_INFO_BYTES,
                    HTableDescriptor.NAMESPACE_COL_DESC_BYTES));
            ret.add(ProtobufUtil.toNamespaceDescriptor(HBaseProtos.NamespaceDescriptor.parseFrom(val)));
        }
    } finally {
        scanner.close();
    }
    return ret;
}

From source file:org.apache.hadoop.hbase.master.TestRollingRestart.java

private NavigableSet<String> getDoubleAssignedRegions(MiniHBaseCluster cluster) throws IOException {
    NavigableSet<String> online = new TreeSet<String>();
    NavigableSet<String> doubled = new TreeSet<String>();
    for (RegionServerThread rst : cluster.getLiveRegionServerThreads()) {
        for (HRegionInfo region : ProtobufUtil.getOnlineRegions(rst.getRegionServer().getRSRpcServices())) {
            if (!online.add(region.getRegionNameAsString())) {
                doubled.add(region.getRegionNameAsString());
            }//from  w  w  w  . ja va  2s .  c  o m
        }
    }
    return doubled;
}