下面列出了com.google.common.collect.Iterators#getLast ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
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;
}
/**
* 获取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());
}
@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");
}
}
@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());
}
/**
* 获取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());
}
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;
}
/**
* 获取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());
}
protected Spatial getPrevious( Spatial from ) {
Spatial previous = Iterators.getLast(new ChildIterator(null, from, TraversalDirection.PageEnd), null);
if( previous == null && focusRoot ) {
previous = getLast();
}
return previous;
}
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;
}
@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);
}
@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);
}
}
@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);
}
protected Spatial getLast() {
return Iterators.getLast(new ChildIterator(TraversalDirection.PageEnd), null);
}
/**
* 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;
}
}
@Override
default V lastValue(final IScope scope) throws GamaRuntimeException {
if (length(scope) == 0) { return null; }
return Iterators.getLast(values().iterator());
}
@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);
}