Example usage for com.google.common.collect ImmutableCollection iterator

List of usage examples for com.google.common.collect ImmutableCollection iterator

Introduction

In this page you can find the example usage for com.google.common.collect ImmutableCollection iterator.

Prototype

public abstract UnmodifiableIterator<E> iterator();

Source Link

Usage

From source file:org.mutabilitydetector.checkers.hint.WrappingHintGenerator.java

/**
 * Pick arbitrary copying method from available configuration and don't forget to 
 * set generic method type if required.// w ww  .java 2  s.  c om
 * @param builder
 */
private void generateCopyingPart(WrappingHint.Builder builder) {
    ImmutableCollection<CopyMethod> copyMethods = ImmutableMultimap.<String, CopyMethod>builder()
            .putAll(configuration.FIELD_TYPE_TO_COPY_METHODS).putAll(userDefinedCopyMethods).build()
            .get(typeAssignedToField);

    if (copyMethods.isEmpty()) {
        throw new WrappingHintGenerationException();
    }

    CopyMethod firstSuitable = copyMethods.iterator().next();
    builder.setCopyMethodOwnerName(firstSuitable.owner.toString()).setCopyMethodName(firstSuitable.name);

    if (firstSuitable.isGeneric && typeSignature != null) {
        CollectionField withRemovedWildcards = CollectionField.from(typeAssignedToField, typeSignature)
                .transformGenericTree(GenericType::withoutWildcard);
        builder.setCopyTypeParameterName(formatTypeParameter(withRemovedWildcards.asSimpleString()));
    }
}

From source file:org.fao.geonet.api.records.formatters.SchemaLocalizations.java

public String nodeTranslation(String qualifiedNodeName, String qualifiedParentNodeName, String type)
        throws Exception {
    if (qualifiedParentNodeName == null) {
        qualifiedParentNodeName = "";
    }//ww  w. j  ava  2  s  .c  om

    for (SchemaLocalization schemaLocalization : this.schemaLocalizations) {
        final ImmutableTable<String, String, Element> labelIndex = schemaLocalization
                .getLabelIndex(this.languageHolder.getLang3());
        Element element = labelIndex.get(qualifiedNodeName, qualifiedParentNodeName);
        if (element == null) {
            element = labelIndex.get(qualifiedNodeName, "");
        }
        if (element == null) {
            final ImmutableCollection<Element> values = labelIndex.row(qualifiedNodeName).values();
            if (!values.isEmpty()) {
                element = values.iterator().next();
            }
        }
        if (element != null) {
            return element.getChildText(type);
        }
    }

    return qualifiedNodeName;
}

From source file:com.outerspacecat.icalendar.Component.java

/**
 * Returns a single {@link Property} with the specified name, or throws a
 * {@link CalendarParseException} if a property with the specified name does
 * not occur exactly once.//from ww  w .ja v a2 s  .com
 * 
 * @param name the name of the property to retrieve. Must be non {@code null}.
 * @return a single {@link Property} with the specified name. Never
 *         {@code null}.
 * @throws CalendarParseException if a property with the specified name does
 *         not occur exactly once.
 */
public Property getSingleProperty(final String name) throws CalendarParseException {
    Preconditions.checkNotNull(name, "name required");

    ImmutableCollection<Property> props = getProperties().get(name.toUpperCase());
    if (props.isEmpty())
        throw new CalendarParseException("no properties for name: " + name);
    if (props.size() > 1)
        throw new CalendarParseException("more than one property for name: " + name);
    return props.iterator().next();
}

From source file:com.google.googlejavaformat.java.JavaInput.java

/**
 * Convert from an offset and length flag pair to a token range.
 *
 * @param offset the {@code 0}-based offset in characters
 * @param length the length in characters
 * @return the {@code 0}-based {@link Range} of tokens
 * @throws FormatterException//from   w  ww. j  a  v  a  2s  .c  o  m
 */
Range<Integer> characterRangeToTokenRange(int offset, int length) throws FormatterException {
    int requiredLength = offset + length;
    if (requiredLength > text.length()) {
        throw new FormatterException(String.format(
                "error: invalid length %d, offset + length (%d) is outside the file", length, requiredLength));
    }
    if (length < 0) {
        return EMPTY_RANGE;
    }
    if (length == 0) {
        // 0 stands for "format the line under the cursor"
        length = 1;
    }
    ImmutableCollection<Token> enclosed = getPositionTokenMap()
            .subRangeMap(Range.closedOpen(offset, offset + length)).asMapOfRanges().values();
    if (enclosed.isEmpty()) {
        return EMPTY_RANGE;
    }
    return Range.closedOpen(enclosed.iterator().next().getTok().getIndex(),
            getLast(enclosed).getTok().getIndex() + 1);
}

From source file:com.facebook.buck.android.dalvik.DalvikAwareZipSplitter.java

@Override
public ImmutableMultimap<APKModule, Path> execute() throws IOException {
    ClasspathTraverser classpathTraverser = new DefaultClasspathTraverser();
    Set<String> secondaryTail = new HashSet<String>();

    // Start out by writing the primary zip and recording which entries were added to it.
    primaryOut = newZipOutput(outPrimary);
    secondaryDexWriter.reset();/*ww w.  ja  va2  s  .  c om*/

    ImmutableMap.Builder<String, FileLike> entriesBuilder = ImmutableMap.builder();
    List<String> additionalDexStoreEntries = new ArrayList<>();

    // Iterate over all of the inFiles and add all entries that match the requiredInPrimaryZip
    // predicate.
    LOG.verbose("Traversing classpath for primary zip");

    classpathTraverser.traverse(new ClasspathTraversal(inFiles, filesystem) {
        @Override
        public void visit(FileLike entry) throws IOException {
            LOG.verbose("Visiting " + entry.getRelativePath());

            String relativePath = entry.getRelativePath();
            if (!relativePath.endsWith(".class")) {
                // We don't need resources in dex jars, so just drop them.
                return;
            }
            String classPath = relativePath.replaceAll("\\.class$", "");

            Objects.requireNonNull(primaryOut);
            Objects.requireNonNull(classPathToDexStore);

            if (requiredInPrimaryZip.test(relativePath)) {
                primaryOut.putEntry(entry);
            } else if (wantedInPrimaryZip.contains(relativePath)
                    || (secondaryHeadSet != null && secondaryHeadSet.contains(relativePath))) {
                entriesBuilder.put(relativePath, new BufferedFileLike(entry));
            } else if (secondaryTailSet != null && secondaryTailSet.contains(relativePath)) {
                entriesBuilder.put(relativePath, new BufferedFileLike(entry));
                secondaryTail.add(relativePath);
            } else {
                ImmutableCollection<APKModule> containingModule = classPathToDexStore.get(classPath);
                if (!containingModule.isEmpty()) {
                    if (containingModule.size() > 1) {
                        throw new IllegalStateException(
                                String.format("classpath %s is contained in multiple dex stores: %s", classPath,
                                        classPathToDexStore.get(classPath).asList().toString()));
                    }
                    APKModule dexStore = containingModule.iterator().next();
                    if (!dexStore.equals(rootModule)) {
                        MySecondaryDexHelper dexHelper = additionalDexWriters.get(dexStore);
                        Objects.requireNonNull(dexHelper);
                        dexHelper.getOutputToWriteTo(entry).putEntry(entry);
                        additionalDexStoreEntries.add(relativePath);
                    }
                }
            }
        }
    });

    // Put as many of the items wanted in the primary dex as we can into the primary dex.
    ImmutableMap<String, FileLike> entries = entriesBuilder.build();
    for (String wanted : wantedInPrimaryZip) {
        FileLike entry = entries.get(wanted);
        if ((entry != null) && !primaryOut.containsEntry(entry) && primaryOut.canPutEntry(entry)) {
            primaryOut.putEntry(entry);
        }
    }

    if (secondaryHeadSet != null) {
        for (String head : secondaryHeadSet) {
            FileLike headEntry = entries.get(head);
            if ((headEntry != null) && !primaryOut.containsEntry(headEntry)) {
                secondaryDexWriter.getOutputToWriteTo(headEntry).putEntry(headEntry);
            }
        }
    }

    LOG.verbose("Traversing classpath for secondary zip");

    // Now that all of the required entries have been added to the primary zip, fill the rest of
    // the zip up with the remaining entries.
    classpathTraverser.traverse(new ClasspathTraversal(inFiles, filesystem) {
        @Override
        public void visit(FileLike entry) throws IOException {
            Objects.requireNonNull(primaryOut);
            String relativePath = entry.getRelativePath();

            // skip if it is the primary dex, is part of a modular dex store, or is not a class file
            if (primaryOut.containsEntry(entry) || additionalDexStoreEntries.contains(relativePath)) {
                return;
            }

            LOG.verbose("Visiting " + entry.getRelativePath());

            // Even if we have started writing a secondary dex, we still check if there is any
            // leftover
            // room in the primary dex for the current entry in the traversal.
            if (dexSplitStrategy == DexSplitStrategy.MAXIMIZE_PRIMARY_DEX_SIZE
                    && primaryOut.canPutEntry(entry)) {
                primaryOut.putEntry(entry);
            } else {
                if (secondaryHeadSet != null && secondaryHeadSet.contains(relativePath)) {
                    return;
                }
                if (secondaryTail.contains(relativePath)) {
                    return;
                }
                secondaryDexWriter.getOutputToWriteTo(entry).putEntry(entry);
            }
        }
    });
    if (secondaryTailSet != null) {
        for (String tail : secondaryTailSet) {
            FileLike tailEntry = entries.get(tail);
            if ((tailEntry != null) && !primaryOut.containsEntry(tailEntry) && secondaryTail.contains(tail)) {
                secondaryDexWriter.getOutputToWriteTo(tailEntry).putEntry(tailEntry);
            }
        }
    }
    primaryOut.close();
    secondaryDexWriter.close();

    ImmutableMultimap.Builder<APKModule, Path> outputFilesBuilder = ImmutableMultimap.builder();
    APKModule secondaryDexStore = rootModule;
    outputFilesBuilder.putAll(secondaryDexStore, secondaryDexWriter.getFiles());
    for (Map.Entry<APKModule, MySecondaryDexHelper> entry : additionalDexWriters.entrySet()) {
        if (!entry.getKey().equals(secondaryDexStore)) {
            entry.getValue().close();
            outputFilesBuilder.putAll(entry.getKey(), entry.getValue().getFiles());
        }
    }
    return outputFilesBuilder.build();
}