Example usage for com.google.common.collect Interner intern

List of usage examples for com.google.common.collect Interner intern

Introduction

In this page you can find the example usage for com.google.common.collect Interner intern.

Prototype

E intern(E sample);

Source Link

Document

Chooses and returns the representative instance for any of a collection of instances that are equal to each other.

Usage

From source file:org.apache.druid.timeline.DataSegment.java

private List<String> prepareDimensionsOrMetrics(@Nullable List<String> list, Interner<List<String>> interner) {
    if (list == null) {
        return ImmutableList.of();
    } else {/* w ww .  j  a v a  2  s  . c  o m*/
        List<String> result = list.stream().filter(s -> !Strings.isNullOrEmpty(s)).map(STRING_INTERNER::intern)
                // TODO replace with ImmutableList.toImmutableList() when updated to Guava 21+
                .collect(Collectors.collectingAndThen(Collectors.toList(), ImmutableList::copyOf));
        return interner.intern(result);
    }
}

From source file:com.google.doubleclick.util.DoubleClickMetadata.java

private ImmutableMap<Object, CountryCodes> loadCountryCodes(final Interner<String> interner,
        String resourceName) {/*from w  ww .  j a v  a 2 s  . c o  m*/
    final ImmutableMap.Builder<Object, CountryCodes> map = ImmutableMap.builder();

    try (InputStream is = new ResourceTransport().open(resourceName)) {
        CSVParser.tsvParser().parse(is, "(\\d+)\\s+(.*)", new Function<List<String>, Boolean>() {
            @Override
            @Nullable
            public Boolean apply(@Nullable List<String> fields) {
                try {
                    CountryCodes codes = new CountryCodes(Integer.parseInt(fields.get(0)),
                            interner.intern(fields.get(2)), interner.intern(fields.get(3)));
                    map.put(codes.numeric(), codes);
                    map.put(codes.alpha2(), codes);
                    map.put(codes.alpha3(), codes);
                } catch (IllegalArgumentException e) {
                    logger.trace("Bad record: {}: {}", fields, e.toString());
                }
                return true;
            }
        });
    } catch (IOException e) {
        throw new ExceptionInInitializerError(e);
    }
    return map.build();
}

From source file:de.learnlib.algorithms.dhc.mealy.MealyDHC.java

@Override
public void startLearning() {
    // initialize structure to store state output signatures
    Map<List<Word<O>>, Integer> signatures = new HashMap<>();

    // set up new hypothesis machine
    hypothesis = new CompactMealy<>(alphabet);

    // initialize exploration queue
    Queue<QueueElement<I, O>> queue = new ArrayDeque<>();

    // initialize storage for access sequences
    accessSequences = hypothesis.createDynamicStateMapping();

    // first element to be explored represents the initial state with no predecessor
    queue.add(new QueueElement<I, O>(null, null, null, null));

    Interner<Word<O>> deduplicator = Interners.newStrongInterner();

    while (!queue.isEmpty()) {
        // get element to be explored from queue
        QueueElement<I, O> elem = queue.poll();

        // determine access sequence for state
        Word<I> access = assembleAccessSequence(elem);

        // assemble queries
        ArrayList<DefaultQuery<I, Word<O>>> queries = new ArrayList<>(splitters.size());
        for (Word<I> suffix : splitters) {
            queries.add(new DefaultQuery<I, Word<O>>(access, suffix));
        }/*from   w  ww  .ja va2  s. c o  m*/

        // retrieve answers
        oracle.processQueries(queries);

        // assemble output signature
        List<Word<O>> sig = new ArrayList<>(splitters.size());
        for (DefaultQuery<I, Word<O>> query : queries) {
            sig.add(deduplicator.intern(query.getOutput()));
        }

        Integer sibling = signatures.get(sig);

        if (sibling != null) {
            // this element does not possess a new output signature
            // create a transition from parent state to sibling
            hypothesis.addTransition(elem.parentState, elem.transIn, sibling, elem.transOut);
        } else {
            // this is actually an observably distinct state! Progress!
            // Create state and connect via transition to parent
            Integer state = elem.parentElement == null ? hypothesis.addInitialState() : hypothesis.addState();
            if (elem.parentElement != null) {
                hypothesis.addTransition(elem.parentState, elem.transIn, state, elem.transOut);
            }
            signatures.put(sig, state);
            accessSequences.put(state, elem);

            scheduleSuccessors(elem, state, queue, sig);
        }
    }
}

From source file:com.google.debugging.sourcemap.SourceMapConsumerV1.java

/**
 * Parse the file mappings section of the source map file.  This maps the
 * ids to the filename, line number and column number in the original
 * files./*from  w  w w  .ja  v  a  2  s  .  c o m*/
 * @param parser The parser to get the data from.
 * @param maxID The maximum id found in the character mapping section.
 */
private void parseFileMappings(ParseState parser, int maxID) throws SourceMapParseException, JSONException {
    // ['d.js', 3, 78, 'foo']
    // Intern the strings to save memory.
    Interner<String> interner = Interners.newStrongInterner();
    ImmutableList.Builder<SourceFile> mappingsBuilder = ImmutableList.builder();

    // Setup all the arrays to keep track of the various details about the
    // source file.
    ArrayList<Byte> lineOffsets = Lists.newArrayList();
    ArrayList<Short> columns = Lists.newArrayList();
    ArrayList<String> identifiers = Lists.newArrayList();

    // The indexes and details about the current position in the file to do
    // diffs against.
    String currentFile = null;
    int lastLine = -1;
    int startLine = -1;
    int startMapId = -1;
    for (int mappingId = 0; mappingId <= maxID; ++mappingId) {
        String currentLine = parser.readLine();
        JSONArray mapArray = new JSONArray(currentLine);
        if (mapArray.length() < 3) {
            parser.fail("Invalid mapping array");
        }

        // Split up the file and directory names to reduce memory usage.
        String myFile = mapArray.getString(0);
        int line = mapArray.getInt(1);
        if (!myFile.equals(currentFile) || (line - lastLine) > Byte.MAX_VALUE
                || (line - lastLine) < Byte.MIN_VALUE) {
            if (currentFile != null) {
                FileName dirFile = splitFileName(interner, currentFile);
                SourceFile.Builder builder = SourceFile.newBuilder().setDir(dirFile.dir)
                        .setFileName(dirFile.name).setStartLine(startLine).setStartMapId(startMapId)
                        .setLineOffsets(lineOffsets).setColumns(columns).setIdentifiers(identifiers);
                mappingsBuilder.add(builder.build());
            }
            // Reset all the positions back to the start and clear out the arrays
            // to start afresh.
            currentFile = myFile;
            startLine = line;
            lastLine = line;
            startMapId = mappingId;
            columns.clear();
            lineOffsets.clear();
            identifiers.clear();
        }
        // We need to add on the columns and identifiers for all the lines, even
        // for the first line.
        lineOffsets.add((byte) (line - lastLine));
        columns.add((short) mapArray.getInt(2));
        identifiers.add(interner.intern(mapArray.optString(3, "")));
        lastLine = line;
    }
    if (currentFile != null) {
        FileName dirFile = splitFileName(interner, currentFile);
        SourceFile.Builder builder = SourceFile.newBuilder().setDir(dirFile.dir).setFileName(dirFile.name)
                .setStartLine(startLine).setStartMapId(startMapId).setLineOffsets(lineOffsets)
                .setColumns(columns).setIdentifiers(identifiers);
        mappingsBuilder.add(builder.build());
    }
    mappings = mappingsBuilder.build();
}