List of usage examples for com.google.common.collect Iterables getLast
public static <T> T getLast(Iterable<T> iterable)
From source file:com.github.fge.jsonpatch.AddOperation.java
private JsonNode addToArray(final JsonPointer path, final JsonNode node) throws JsonPatchException { final JsonNode ret = node.deepCopy(); final ArrayNode target = (ArrayNode) path.parent().get(ret); final TokenResolver<JsonNode> token = Iterables.getLast(path); if (token.getToken().equals(LAST_ARRAY_ELEMENT)) { target.add(value);//from w w w . j a v a 2 s .co m return ret; } final int size = target.size(); final int index; try { index = Integer.parseInt(token.toString()); } catch (NumberFormatException ignored) { throw new JsonPatchException(BUNDLE.getMessage("jsonPatch.notAnIndex")); } if (index < 0 || index > size) throw new JsonPatchException(BUNDLE.getMessage("jsonPatch.noSuchIndex")); target.insert(index, value); return ret; }
From source file:com.threerings.presents.tools.ActionScriptUtils.java
public static String toReadObject(Class<?> type) { if (type.equals(String.class)) { return "readField(String)"; } else if (type.equals(Integer.class) || type.equals(Short.class) || type.equals(Byte.class)) { return "readField(" + toSimpleName(type) + ").value"; } else if (type.equals(Long.class)) { return "readField(" + toSimpleName(type) + ")"; } else if (type.equals(Boolean.TYPE)) { return "readBoolean()"; } else if (type.equals(Byte.TYPE)) { return "readByte()"; } else if (type.equals(Short.TYPE) || type.equals(Character.TYPE)) { return "readShort()"; } else if (type.equals(Integer.TYPE)) { return "readInt()"; } else if (type.equals(Long.TYPE)) { return "readLong()"; } else if (type.equals(Float.TYPE)) { return "readFloat()"; } else if (type.equals(Double.TYPE)) { return "readDouble()"; } else if (isNaiveMap(type)) { return "readField(MapStreamer.INSTANCE)"; } else if (isNaiveList(type)) { return "readField(ArrayStreamer.INSTANCE)"; } else if (isNaiveSet(type)) { return "readField(SetStreamer.INSTANCE)"; } else if (type.isArray()) { if (!type.getComponentType().isPrimitive()) { return "readObject(TypedArray)"; } else {/*from w w w . ja v a 2s. com*/ if (Double.TYPE.equals(type.getComponentType())) { return "readField(TypedArray.getJavaType(Number))"; } else if (Boolean.TYPE.equals(type.getComponentType())) { return "readField(TypedArray.getJavaType(Boolean))"; } else if (Integer.TYPE.equals(type.getComponentType())) { return "readField(TypedArray.getJavaType(int))"; } else if (Byte.TYPE.equals(type.getComponentType())) { return "readField(ByteArray)"; } else { throw new IllegalArgumentException(type + " isn't supported to stream to actionscript"); } } } else { return "readObject(" + Iterables.getLast(DOT_SPLITTER.split(toActionScriptType(type, false))) + ")"; } }
From source file:io.crate.execution.dsl.phases.MergePhase.java
/** * Creates a MergePhase to combine the results from multiple node operations. * @param jobId The JobID of the entire execution. * @param executionNodeId A unique execution id for this phase. * @param name The name of the MergePhase. * @param numUpstreams The number of nodes to expect data from. * @param numInputs The number of different inputs to read data from which is equal to * the number of upstream phases. * @param executionNodes The nodes where this MergePhase executes. * @param inputTypes The types of the input rows. * @param projections The projections to apply when merging. * @param distributionInfo The default strategy to use when distributing the results of the MergePhase. * @param positionalOrderBy The order by positions on which the input is pre-sorted on; setting this * will result in a sorted merge. */// w ww.j ava2 s .com public MergePhase(UUID jobId, int executionNodeId, String name, int numUpstreams, int numInputs, Collection<String> executionNodes, Collection<? extends DataType> inputTypes, List<Projection> projections, DistributionInfo distributionInfo, @Nullable PositionalOrderBy positionalOrderBy) { super(jobId, executionNodeId, name, projections); this.numInputs = numInputs; this.inputTypes = inputTypes; this.numUpstreams = numUpstreams; this.distributionInfo = distributionInfo; if (projections.isEmpty()) { outputTypes = Lists.newArrayList(inputTypes); } else { outputTypes = Symbols.typeView(Iterables.getLast(projections).outputs()); } this.positionalOrderBy = positionalOrderBy; this.executionNodes = executionNodes; }
From source file:org.jboss.hal.ballroom.LabelBuilder.java
/** * Turns a list of names from the management model into a human readable enumeration wrapped in quotes and * separated with commas. The last name is separated with the specified conjunction. * * @return The list of names as human readable string or an empty string if the names are null or empty. *//*from w w w .j a va2 s . co m*/ public String enumeration(Iterable<String> names, String conjunction) { String enumeration = ""; if (names != null && !Iterables.isEmpty(names)) { int size = Iterables.size(names); if (size == 1) { return QUOTE + label(names.iterator().next()) + QUOTE; } else if (size == 2) { return QUOTE + label(Iterables.getFirst(names, "")) + QUOTE + SPACE + conjunction + SPACE + QUOTE + label(Iterables.getLast(names)) + QUOTE; } else { String last = Iterables.getLast(names); LinkedList<String> allButLast = new LinkedList<>(); Iterables.addAll(allButLast, names); allButLast.removeLast(); enumeration = allButLast.stream().map(name -> QUOTE + label(name) + QUOTE) .collect(Collectors.joining(", ")); enumeration = enumeration + SPACE + conjunction + SPACE + QUOTE + label(last) + QUOTE; } } return enumeration; }
From source file:com.google.security.zynamics.reil.translators.ReilTranslator.java
/** * Returns the addresses of all basic blocks of a function. Note that the addresses are already * converted to REIL addresses./*from w ww . j a v a 2 s . c om*/ * * @param function The input function. * * @return A list of all basic block addresses of the function. */ private static Collection<IAddress> getBlockAddresses(final IBlockContainer<?> function) { return CollectionHelpers.map(function.getBasicBlocks(), new ICollectionMapper<ICodeContainer<?>, IAddress>() { private boolean isDelayedBranch(final ReilInstruction instruction) { return instruction.getMnemonic().equals(ReilHelpers.OPCODE_JCC) && ReilHelpers.isDelayedBranch(instruction); } @Override public IAddress map(final ICodeContainer<?> block) { final IInstruction lastInstruction = Iterables.getFirst(block.getInstructions(), null); // getLastInstruction(block); final ReilTranslator<IInstruction> translator = new ReilTranslator<IInstruction>(); try { final ReilGraph reilGraph = translator.translate(new StandardEnvironment(), lastInstruction); final ReilBlock lastNode = reilGraph.getNodes().get(reilGraph.getNodes().size() - 1); final ReilInstruction lastReilInstruction = Iterables .getLast(lastNode.getInstructions()); if (isDelayedBranch(lastReilInstruction)) // If branch-delay { return ReilHelpers .toReilAddress(Iterables.get(block.getInstructions(), 1).getAddress()); } else { return ReilHelpers.toReilAddress(block.getAddress()); } } catch (final InternalTranslationException e) { return ReilHelpers.toReilAddress(block.getAddress()); } } }); }
From source file:org.apache.phoenix.hbase.index.covered.data.LocalTable.java
protected long getOldestTimestamp(Collection<List<Cell>> cellLists) { Ordering<List<Cell>> cellListOrdering = new Ordering<List<Cell>>() { @Override//from w ww . j a v a 2s. c o m public int compare(List<Cell> left, List<Cell> right) { // compare the last element of each list, since that is the smallest in that list return Longs.compare(Iterables.getLast(left).getTimestamp(), Iterables.getLast(right).getTimestamp()); } }; List<Cell> minList = cellListOrdering.min(cellLists); return Iterables.getLast(minList).getTimestamp(); }
From source file:com.bennavetta.util.tycho.impl.DefaultBundleGenerator.java
@Override public String getSymbolicName(Artifact artifact) { if (artifact.getGroupId().indexOf('.') == -1) { // Find the first package with classes in it try (JarFile jar = new JarFile(artifact.getFile())) // commons-logging:commons-logging -> org.apache.commons.logging {//ww w. ja v a2s . c om List<String> contents = new ArrayList<>(); Enumeration<JarEntry> entries = jar.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); contents.add(entry.getName()); } // sort by number of slashes Collections.sort(contents, PATH_COMPONENTS); for (String path : contents) { if (path.endsWith(".class")) { path = path.substring(0, path.lastIndexOf('/')).replace('/', '.'); if (path.startsWith("/")) { path = path.substring(1); } return path; } } } catch (IOException e) { return null; } } else if (Iterables.getLast(Splitter.on('.').split(artifact.getGroupId())) .equals(artifact.getArtifactId())) { return artifact.getGroupId(); // org.apache.maven:maven -> org.apache.maven } else { String gidEnd = Iterables.getLast(Splitter.on('.').split(artifact.getGroupId())); if (Iterables.getFirst(Splitter.on('.').split(artifact.getArtifactId()), null).equals(gidEnd)) { // org.apache.maven:maven-core -> org.apache.maven.core return artifact.getGroupId() + "." + PUNCTUATION.trimFrom(artifact.getArtifactId().substring(gidEnd.length())); } else { return artifact.getGroupId() + "." + artifact.getArtifactId(); // groupId + "." + artifactId } } return null; }
From source file:org.jclouds.vcloud.terremark.VCloudComputeClient.java
/** * /* www. ja v a2 s .com*/ * @throws ElementNotFoundException * if no address is configured */ public InetAddress getAnyPrivateAddress(String id) { VApp vApp = tmClient.getVApp(id); return Iterables.getLast(vApp.getNetworkToAddresses().values()); }
From source file:org.glowroot.ui.GaugeValueJsonService.java
@GET(path = "/backend/jvm/gauges", permission = "agent:jvm:gauges") String getGaugeValues(@BindAgentRollupId String agentRollupId, @BindRequest GaugeValueRequest request) throws Exception { int rollupLevel = rollupLevelService.getGaugeRollupLevelForView(request.from(), request.to(), agentRollupId.endsWith("::")); long dataPointIntervalMillis; if (rollupLevel == 0) { dataPointIntervalMillis = configRepository.getGaugeCollectionIntervalMillis(); } else {/*from ww w .j a v a 2 s.c om*/ dataPointIntervalMillis = configRepository.getRollupConfigs().get(rollupLevel - 1).intervalMillis(); } Map<String, List<GaugeValue>> origGaugeValues = getGaugeValues(agentRollupId, request, rollupLevel, dataPointIntervalMillis); Map<String, List<GaugeValue>> gaugeValues = origGaugeValues; if (isEmpty(gaugeValues) && noHarmFallingBackToLargestAggregate(agentRollupId, rollupLevel, request)) { // fall back to largest aggregates in case expiration settings have recently changed rollupLevel = getLargestRollupLevel(); dataPointIntervalMillis = configRepository.getRollupConfigs().get(rollupLevel - 1).intervalMillis(); gaugeValues = getGaugeValues(agentRollupId, request, rollupLevel, dataPointIntervalMillis); long lastCaptureTime = 0; for (List<GaugeValue> list : gaugeValues.values()) { if (!list.isEmpty()) { lastCaptureTime = Math.max(lastCaptureTime, Iterables.getLast(list).getCaptureTime()); } } if (lastCaptureTime != 0 && ignoreFallBackData(request, lastCaptureTime)) { // this is probably data from before the requested time period // (go back to empty gauge values) gaugeValues = origGaugeValues; } } if (rollupLevel != 0) { syncManualRollupCaptureTimes(gaugeValues, rollupLevel); } double gapMillis = dataPointIntervalMillis * 1.5; List<DataSeries> dataSeriesList = Lists.newArrayList(); for (Map.Entry<String, List<GaugeValue>> entry : gaugeValues.entrySet()) { dataSeriesList.add(convertToDataSeriesWithGaps(entry.getKey(), entry.getValue(), gapMillis)); } List<Gauge> gauges = gaugeValueRepository.getGauges(agentRollupId, request.from(), request.to()); List<Gauge> sortedGauges = new GaugeOrdering().immutableSortedCopy(gauges); sortedGauges = addCounterSuffixesIfAndWhereNeeded(sortedGauges); StringBuilder sb = new StringBuilder(); JsonGenerator jg = mapper.getFactory().createGenerator(CharStreams.asWriter(sb)); try { jg.writeStartObject(); jg.writeObjectField("dataSeries", dataSeriesList); jg.writeNumberField("dataPointIntervalMillis", dataPointIntervalMillis); jg.writeObjectField("allGauges", sortedGauges); jg.writeEndObject(); } finally { jg.close(); } return sb.toString(); }
From source file:org.diqube.metadata.inspect.TableMetadataInspector.java
/** * Identifies the most specific interesting {@link FieldMetadata} for a given columnName, as it might be used in a * diql query./* ww w . ja v a 2s. c om*/ * * @return {@link FieldMetadata} or <code>null</code>. */ public FieldMetadata findFieldMetadata(String columnName) throws ColumnNameInvalidException { List<FieldMetadata> allMetadata = findAllFieldMetadata(columnName); if (allMetadata.isEmpty()) return null; return Iterables.getLast(allMetadata); }