下面列出了com.google.common.collect.Iterators#get ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@NotNull
private HttpClient client(final int node) {
final String endpoint = Iterators.get(endpoints.iterator(), node);
return hostToClient.computeIfAbsent(
endpoint,
key -> new HttpClient(new ConnectionString(key + "?timeout=" + TIMEOUT)));
}
private void resetClient(final int node) {
final String endpoint = Iterators.get(endpoints.iterator(), node);
final HttpClient client = hostToClient.remove(endpoint);
if (client != null) {
client.close();
}
}
@NotNull
private String url(
final int node,
@NotNull final String id,
final int ack,
final int from) {
final String endpoint = Iterators.get(endpoints.iterator(), node);
return endpoint + "/v0/entity?id=" + id + "&replicas=" + ack + "/" + from;
}
protected Spatial getNext( Spatial from ) {
Spatial next = Iterators.get(new ChildIterator(from, null, TraversalDirection.PageHome), 0, null);
if( next == null && focusRoot ) {
next = getFirst();
}
return next;
}
private Client get(ByteBuffer pk)
{
Set<Host> hosts = metadata.getReplicas(metadata.quote(keyspace), pk);
InetAddress address = null;
if (hosts.size() > 0)
{
int pos = roundrobin.incrementAndGet() % hosts.size();
for (int i = 0 ; address == null && i < hosts.size() ; i++)
{
if (pos < 0)
pos = -pos;
Host host = Iterators.get(hosts.iterator(), (pos + i) % hosts.size());
if (whiteset == null || whiteset.contains(host.getAddress()))
address = host.getAddress();
}
}
if (address == null)
address = whitelist.get(ThreadLocalRandom.current().nextInt(whitelist.size()));
ConcurrentLinkedQueue<Client> q = cache.get(address);
if (q == null)
{
ConcurrentLinkedQueue<Client> newQ = new ConcurrentLinkedQueue<Client>();
q = cache.putIfAbsent(address, newQ);
if (q == null)
q = newQ;
}
Client tclient = q.poll();
if (tclient != null)
return tclient;
return new Client(settings.getRawThriftClient(address.getHostAddress()), address);
}
public T getFirst() {
Iterator<T> iterator = pair.iterator();
return Iterators.get(iterator, 0);
}
public T getSecond() {
//if values are same then there is only one element. Thus, take last element
int last = pair.size() - 1;
Iterator<T> iterator = pair.iterator();
return Iterators.get(iterator, last);
}
private static <T> T get(Set<T> set, int position) {
return Iterators.get(set.iterator(), position, null);
}
protected Spatial getFirst() {
Spatial first = Iterators.get(new ChildIterator(TraversalDirection.PageHome), 0, null);
return first;
}
/**
* 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 firstValue(final IScope scope) throws GamaRuntimeException {
if (length(scope) == 0) { return null; }
return Iterators.get(values().iterator(), 0);
}
default V valueAt(final int index) {
if (size() == 0) { return null; }
return Iterators.get(values().iterator(), index);
}
@Override
public PathArgument get(final int index) {
return Iterators.get(iterator(), index);
}
/**
* Returns the {@link AnchorKey} for the given anchor index, i.e. the
* reverse of {@link #getAnchorIndex(AnchorKey)}.
*
* @param anchorIndex
* The anchor index for which to determine the {@link AnchorKey}.
* @return The {@link AnchorKey} for the given anchor index.
*/
protected AnchorKey getAnchorKey(int anchorIndex) {
return Iterators.get(anchorsByKeys.keySet().iterator(), anchorIndex);
}