@Override
  public Iterator<? extends T> iterator(long first, long count) {
    Collection<T> data = dataModel.getObject();
    if (data == null || data.size() == 0) return Iterators.emptyIterator();
    Iterator<T> it;
    final SortParam<S> sortParam = getSort();
    if (sortParam != null && sortParam.getProperty() != null) {
      Ordering<T> ordering =
          Ordering.natural()
              .nullsFirst()
              .onResultOf(
                  new Function<T, Comparable<?>>() {

                    @Override
                    public Comparable<?> apply(T input) {
                      return comparableValue(input, sortParam.getProperty());
                    }
                  });
      if (!sortParam.isAscending()) ordering = ordering.reverse();
      it = ordering.sortedCopy(data).iterator();
    } else {
      it = data.iterator();
    }
    if (first > 0) Iterators.advance(it, (int) first);
    return count >= 0 ? Iterators.limit(it, (int) count) : it;
  }
Exemplo n.º 2
0
 /**
  * Returns the element at the specified position in an iterable or a default value otherwise.
  *
  * @param position position of the element to return
  * @param defaultValue the default value to return if {@code position} is greater than or equal to
  *     the size of the iterable
  * @return the element at the specified position in {@code iterable} or {@code defaultValue} if
  *     {@code iterable} contains fewer than {@code position + 1} elements.
  * @throws IndexOutOfBoundsException if {@code position} is negative
  * @since 4.0
  */
 @Nullable
 public static <T> T get(Iterable<? extends T> iterable, int position, @Nullable T defaultValue) {
   checkNotNull(iterable);
   Iterators.checkNonnegative(position);
   if (iterable instanceof List) {
     List<? extends T> list = Lists.cast(iterable);
     return (position < list.size()) ? list.get(position) : defaultValue;
   } else {
     Iterator<? extends T> iterator = iterable.iterator();
     Iterators.advance(iterator, position);
     return Iterators.getNext(iterator, defaultValue);
   }
 }
Exemplo n.º 3
0
 /**
  * Advances {@code iterator} {@code position + 1} times, returning the element at the {@code
  * position}th position or {@code defaultValue} otherwise.
  *
  * @param position position of the element to return
  * @param defaultValue the default value to return if the iterator is empty or if {@code position}
  *     is greater than the number of elements remaining in {@code iterator}
  * @return the element at the specified position in {@code iterator} or {@code defaultValue} if
  *     {@code iterator} produces fewer than {@code position + 1} elements.
  * @throws IndexOutOfBoundsException if {@code position} is negative
  * @since 4.0
  */
 @Nullable
 public static <T> T get(Iterator<? extends T> iterator, int position, @Nullable T defaultValue) {
   checkNonnegative(position);
   advance(iterator, position);
   return getNext(iterator, defaultValue);
 }
Exemplo n.º 4
0
  /**
   * Runs the command and builds the appropriate response
   *
   * @param context - the context to use for this command
   * @throws IllegalArgumentException
   */
  @Override
  public void run(final CommandContext context) {
    final Context geogit = this.getCommandLocator(context);

    LogOp op = geogit.command(LogOp.class).setFirstParentOnly(firstParentOnly);

    if (skip != null) {
      op.setSkip(skip.intValue());
    }
    if (limit != null) {
      op.setLimit(limit.intValue());
    }

    if (this.sinceTime != null || this.untilTime != null) {
      Date since = new Date(0);
      Date until = new Date();
      if (this.sinceTime != null) {
        since = new Date(geogit.command(ParseTimestamp.class).setString(this.sinceTime).call());
      }
      if (this.untilTime != null) {
        until = new Date(geogit.command(ParseTimestamp.class).setString(this.untilTime).call());
      }
      op.setTimeRange(new Range<Date>(Date.class, since, until));
    }

    if (this.since != null) {
      Optional<ObjectId> since;
      since = geogit.command(RevParse.class).setRefSpec(this.since).call();
      Preconditions.checkArgument(since.isPresent(), "Object not found '%s'", this.since);
      op.setSince(since.get());
    }
    if (this.until != null) {
      Optional<ObjectId> until;
      until = geogit.command(RevParse.class).setRefSpec(this.until).call();
      Preconditions.checkArgument(until.isPresent(), "Object not found '%s'", this.until);
      op.setUntil(until.get());
    }
    if (paths != null && !paths.isEmpty()) {
      for (String path : paths) {
        op.addPath(path);
      }
    }

    final Iterator<RevCommit> log = op.call();

    Iterators.advance(log, page * elementsPerPage);

    if (countChanges) {
      final String pathFilter;
      if (paths != null && !paths.isEmpty()) {
        pathFilter = paths.get(0);
      } else {
        pathFilter = null;
      }
      Function<RevCommit, CommitWithChangeCounts> changeCountFunctor =
          new Function<RevCommit, CommitWithChangeCounts>() {

            @Override
            public CommitWithChangeCounts apply(RevCommit input) {
              ObjectId parent = ObjectId.NULL;
              if (input.getParentIds().size() > 0) {
                parent = input.getParentIds().get(0);
              }
              int added = 0;
              int modified = 0;
              int removed = 0;

              // If it's a shallow clone, the commit may not exist
              if (parent.equals(ObjectId.NULL) || geogit.stagingDatabase().exists(parent)) {
                final Iterator<DiffEntry> diff =
                    geogit
                        .command(DiffOp.class)
                        .setOldVersion(parent)
                        .setNewVersion(input.getId())
                        .setFilter(pathFilter)
                        .call();

                while (diff.hasNext()) {
                  DiffEntry entry = diff.next();
                  if (entry.changeType() == DiffEntry.ChangeType.ADDED) {
                    added++;
                  } else if (entry.changeType() == DiffEntry.ChangeType.MODIFIED) {
                    modified++;
                  } else {
                    removed++;
                  }
                }
              }

              return new CommitWithChangeCounts(input, added, modified, removed);
            }
          };

      final Iterator<CommitWithChangeCounts> summarizedLog =
          Iterators.transform(log, changeCountFunctor);
      context.setResponseContent(
          new CommandResponse() {
            @Override
            public void write(ResponseWriter out) throws Exception {
              out.start();
              out.writeCommitsWithChangeCounts(summarizedLog, elementsPerPage);
              out.finish();
            }
          });
    } else if (summary) {
      if (paths != null && paths.size() > 0) {
        context.setResponseContent(
            new StreamResponse() {

              @Override
              public void write(Writer out) throws Exception {
                writeCSV(context.getGeoGIT(), out, log);
              }
            });
      } else {
        throw new CommandSpecException(
            "You must specify a feature type path when getting a summary.");
      }
    } else {
      final boolean rangeLog = returnRange;
      context.setResponseContent(
          new CommandResponse() {
            @Override
            public void write(ResponseWriter out) throws Exception {
              out.start();
              out.writeCommits(log, elementsPerPage, rangeLog);
              out.finish();
            }
          });
    }
  }