Java Code Examples for com.google.common.collect.Iterators#getLast()

The following examples show how to use com.google.common.collect.Iterators#getLast() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SSTableAttachedSecondaryIndexTest.java    From sasi with Apache License 2.0 6 votes vote down vote up
private static Set<DecoratedKey> getPaged(ColumnFamilyStore store, int pageSize, IndexExpression... expressions)
{
    List<Row> currentPage;
    Set<DecoratedKey> uniqueKeys = new TreeSet<>();

    DecoratedKey lastKey = null;
    do
    {
        currentPage = getIndexed(store, new IdentityQueryFilter(), lastKey, pageSize, expressions);

        if (currentPage == null)
            break;

        for (Row row : currentPage)
            uniqueKeys.add(row.key);

        Row lastRow = Iterators.getLast(currentPage.iterator(), null);
        if (lastRow == null)
            break;

        lastKey = lastRow.key;
    }
    while (currentPage.size() == pageSize);

    return uniqueKeys;
}
 
Example 2
Source File: CollectionUtil.java    From vjtools with Apache License 2.0 5 votes vote down vote up
/**
 * 获取Collection的最后一个元素,如果collection为空返回null.
 */
public static <T> T getLast(Collection<T> collection) {
	if (isEmpty(collection)) {
		return null;
	}

	// 当类型List时,直接取得最后一个元素.
	if (collection instanceof List) {
		List<T> list = (List<T>) collection;
		return list.get(list.size() - 1);
	}

	return Iterators.getLast(collection.iterator());
}
 
Example 3
Source File: RecordCursorTest.java    From fdb-record-layer with Apache License 2.0 5 votes vote down vote up
@Test
public void hasNextErrorStack() throws Exception {
    final Iterator<String> erring = new BrokenCursor();
    try {
        Iterators.getLast(erring, null);
    } catch (Exception ex) {
        for (Throwable t = ex; t != null; t = t.getCause()) {
            if (Arrays.stream(t.getStackTrace()).anyMatch(s -> s.getMethodName().equals("getLast"))) {
                return;
            }
        }
        fail("did not find getLast() on stack");
    }
}
 
Example 4
Source File: JavaInput.java    From google-java-format with Apache License 2.0 5 votes vote down vote up
private static int updateColumn(int columnI, String originalTokText) {
  Integer last = Iterators.getLast(Newlines.lineOffsetIterator(originalTokText));
  if (last > 0) {
    columnI = originalTokText.length() - last;
  } else {
    columnI += originalTokText.length();
  }
  return columnI;
}
 
Example 5
Source File: BigQueryWrite.java    From feast with Apache License 2.0 5 votes vote down vote up
@Override
public TableSchema getSchema(String featureSet) {
  Map<String, Iterable<TableSchema>> schemasValue = sideInput(schemas);
  Iterable<TableSchema> schemasIt = schemasValue.get(featureSet);
  if (schemasIt == null) {
    return null;
  }
  return Iterators.getLast(schemasIt.iterator());
}
 
Example 6
Source File: CollectionUtil.java    From vjtools with Apache License 2.0 5 votes vote down vote up
/**
 * 获取Collection的最后一个元素,如果collection为空返回null.
 */
public static <T> T getLast(Collection<T> collection) {
	if (isEmpty(collection)) {
		return null;
	}

	// 当类型List时,直接取得最后一个元素.
	if (collection instanceof List) {
		List<T> list = (List<T>) collection;
		return list.get(list.size() - 1);
	}

	return Iterators.getLast(collection.iterator());
}
 
Example 7
Source File: JavaInput.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
private static int updateColumn(int columnI, String originalTokText) {
    Integer last = Iterators.getLast(Newlines.lineOffsetIterator(originalTokText));
    if (last > 0) {
        columnI = originalTokText.length() - last;
    } else {
        columnI += originalTokText.length();
    }
    return columnI;
}
 
Example 8
Source File: JavaInput.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
private static int updateColumn(int columnI, String originalTokText) {
    Integer last = Iterators.getLast(Newlines.lineOffsetIterator(originalTokText));
    if (last > 0) {
        columnI = originalTokText.length() - last;
    } else {
        columnI += originalTokText.length();
    }
    return columnI;
}
 
Example 9
Source File: CollectionUtil.java    From j360-dubbo-app-all with Apache License 2.0 5 votes vote down vote up
/**
 * 获取Collection的最后一个元素,如果collection为空返回null.
 */
public static <T> T getLast(Collection<T> collection) {
	if (isEmpty(collection)) {
		return null;
	}

	// 当类型List时,直接取得最后一个元素.
	if (collection instanceof List) {
		List<T> list = (List<T>) collection;
		return list.get(list.size() - 1);
	}

	return Iterators.getLast(collection.iterator());
}
 
Example 10
Source File: FocusTraversalAdapter.java    From Lemur with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected Spatial getPrevious( Spatial from ) {       
    Spatial previous = Iterators.getLast(new ChildIterator(null, from, TraversalDirection.PageEnd), null);
    if( previous == null && focusRoot ) {
        previous = getLast();
    }
    return previous;
}
 
Example 11
Source File: Doc.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@Override
public State computeBreaks(CommentsHelper commentsHelper, int maxWidth, State state) {
  text = commentsHelper.rewrite(tok, maxWidth, state.column);
  int firstLineLength = text.length() - Iterators.getLast(Newlines.lineOffsetIterator(text));
  return state.withColumn(state.column + firstLineLength);
}
 
Example 12
Source File: Doc.java    From google-java-format with Apache License 2.0 4 votes vote down vote up
@Override
public State computeBreaks(CommentsHelper commentsHelper, int maxWidth, State state) {
  text = commentsHelper.rewrite(tok, maxWidth, state.column);
  int firstLineLength = text.length() - Iterators.getLast(Newlines.lineOffsetIterator(text));
  return state.withColumn(state.column + firstLineLength);
}
 
Example 13
Source File: IMap.java    From gama with GNU General Public License v3.0 4 votes vote down vote up
@Override
default V lastValue(final IScope scope) throws GamaRuntimeException {
	if (length(scope) == 0) { return null; }
	return Iterators.getLast(values().iterator());
}
 
Example 14
Source File: BashAbstractProcessor.java    From BashSupport with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the best results. It takes all the elements which have been rated the best
 * and returns the first / last, depending on the parameter.
 *
 * @param results          The results to check
 * @param firstResult      If the first element of the best element list should be returned.
 * @param referenceElement
 * @return The result
 */
private PsiElement findBestResult(Multimap<Integer, PsiElement> results, boolean firstResult, PsiElement referenceElement) {
    if (!hasResults()) {
        return null;
    }

    if (firstResult) {
        return Iterators.get(results.values().iterator(), 0);
    }

    //if the first should not be used return the best element
    int referenceLevel = preferNeigbourhood && (referenceElement != null) ? BashPsiUtils.blockNestingLevel(referenceElement) : 0;

    // find the best suitable result rating
    // The best one is as close as possible to the given referenceElement
    int bestRating = Integer.MAX_VALUE;
    int bestDelta = Integer.MAX_VALUE;
    for (int rating : results.keySet()) {
        final int delta = Math.abs(referenceLevel - rating);
        if (delta < bestDelta) {
            bestDelta = delta;
            bestRating = rating;
        }
    }

    // now get the best result
    // if there are equal definitions on the same level we prefer the first if the neighbourhood is not preferred
    if (preferNeigbourhood) {
        return Iterators.getLast(results.get(bestRating).iterator());
    } else {
        //return the element which has the lowest textOffset
        long smallestOffset = Integer.MAX_VALUE;
        PsiElement bestElement = null;

        for (PsiElement e : results.get(bestRating)) {
            //if the element is injected compute the text offset in the real file
            int textOffset = e.getTextOffset();
            if (BashPsiUtils.isInjectedElement(e)) {
                //fixme optimize this
                PsiLanguageInjectionHost injectionHost = InjectedLanguageManager.getInstance(e.getProject()).getInjectionHost(e);
                if (injectionHost != null) {
                    textOffset = textOffset + injectionHost.getTextOffset();
                }
            }

            // comparing the offset is only meaningful within the same file
            // for definitions in included files we need to compare against the offset of the include command
            Collection<PsiElement> includeCommands = this.includeCommands != null ? this.includeCommands.get(e) : Collections.emptyList();
            if (!includeCommands.isEmpty()) {
                for (PsiElement includeCommand : includeCommands) {
                    int includeOffset = includeCommand.getTextOffset();
                    if (includeOffset < smallestOffset) {
                        smallestOffset = includeOffset;
                        bestElement = e;
                    }
                }
            } else if (textOffset < smallestOffset) {
                smallestOffset = textOffset;
                bestElement = e;
            }
        }

        return bestElement;
    }
}
 
Example 15
Source File: FocusTraversalAdapter.java    From Lemur with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
protected Spatial getLast() {
    return Iterators.getLast(new ChildIterator(TraversalDirection.PageEnd), null);
}
 
Example 16
Source File: ValidateFeatureRowDoFn.java    From feast with Apache License 2.0 4 votes vote down vote up
@ProcessElement
public void processElement(ProcessContext context) {
  String error = null;
  FeatureRow featureRow = context.element();
  Iterable<FeatureSetProto.FeatureSetSpec> featureSetSpecs =
      context.sideInput(getFeatureSets()).get(featureRow.getFeatureSet());

  List<FieldProto.Field> fields = new ArrayList<>();
  if (featureSetSpecs != null) {
    FeatureSetProto.FeatureSetSpec latestSpec = Iterators.getLast(featureSetSpecs.iterator());
    FeatureSet featureSet = new FeatureSet(latestSpec);

    for (FieldProto.Field field : featureRow.getFieldsList()) {
      Field fieldSpec = featureSet.getField(field.getName());
      if (fieldSpec == null) {
        // skip
        continue;
      }
      // If value is set in the FeatureRow, make sure the value type matches
      // that defined in FeatureSetSpec
      if (!field.getValue().getValCase().equals(ValCase.VAL_NOT_SET)) {
        int expectedTypeFieldNumber = fieldSpec.getType().getNumber();
        int actualTypeFieldNumber = field.getValue().getValCase().getNumber();
        if (expectedTypeFieldNumber != actualTypeFieldNumber) {
          error =
              String.format(
                  "FeatureRow contains field '%s' with invalid type '%s'. Feast expects the field type to match that in FeatureSet '%s'. Please check the FeatureRow data.",
                  field.getName(), field.getValue().getValCase(), fieldSpec.getType());
          break;
        }
      }
      if (!fields.contains(field)) {
        fields.add(field);
      }
    }
  } else {
    error =
        String.format(
            "FeatureRow contains invalid feature set id %s. Please check that the feature rows are being published to the correct topic on the feature stream.",
            featureRow.getFeatureSet());
  }

  if (error != null) {
    FailedElement.Builder failedElement =
        FailedElement.newBuilder()
            .setTransformName("ValidateFeatureRow")
            .setJobName(context.getPipelineOptions().getJobName())
            .setPayload(featureRow.toString())
            .setErrorMessage(error);
    if (featureSetSpecs != null) {
      FeatureSetProto.FeatureSetSpec spec = Iterators.getLast(featureSetSpecs.iterator());
      failedElement =
          failedElement.setProjectName(spec.getProject()).setFeatureSetName(spec.getName());
    }
    context.output(getFailureTag(), failedElement.build());
  } else {
    featureRow = featureRow.toBuilder().clearFields().addAllFields(fields).build();
    context.output(getSuccessTag(), featureRow);
  }
}
 
Example 17
Source File: Doc.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
@Override
public State computeBreaks(CommentsHelper commentsHelper, int maxWidth, State state) {
  text = commentsHelper.rewrite(tok, maxWidth, state.column);
  int firstLineLength = text.length() - Iterators.getLast(Newlines.lineOffsetIterator(text));
  return state.withColumn(state.column + firstLineLength);
}