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:com.google.gitiles.blame.BlameCacheImpl.java

private static List<Region> loadRegions(BlameGenerator gen) throws IOException {
    Map<ObjectId, PooledCommit> commits = Maps.newHashMap();
    Interner<String> strings = Interners.newStrongInterner();
    int lineCount = gen.getResultContents().size();

    List<Region> regions = Lists.newArrayList();
    while (gen.next()) {
        String path = gen.getSourcePath();
        PersonIdent author = gen.getSourceAuthor();
        ObjectId commit = gen.getSourceCommit();
        checkState(path != null && author != null && commit != null);

        PooledCommit pc = commits.get(commit);
        if (pc == null) {
            pc = new PooledCommit(commit.copy(), new PersonIdent(strings.intern(author.getName()),
                    strings.intern(author.getEmailAddress()), author.getWhen(), author.getTimeZone()));
            commits.put(pc.commit, pc);//from  ww  w. j a  v  a  2 s  . co  m
        }
        path = strings.intern(path);
        commit = pc.commit;
        author = pc.author;
        regions.add(new Region(path, commit, author, gen.getResultStart(), gen.getResultEnd()));
    }
    Collections.sort(regions);

    // Fill in any gaps left by bugs in JGit, since rendering code assumes the
    // full set of contiguous regions.
    List<Region> result = Lists.newArrayListWithExpectedSize(regions.size());
    Region last = null;
    for (Region r : regions) {
        if (last != null) {
            checkState(last.getEnd() <= r.getStart());
            if (last.getEnd() < r.getStart()) {
                result.add(new Region(null, null, null, last.getEnd(), r.getStart()));
            }
        }
        result.add(r);
        last = r;
    }
    if (last != null && last.getEnd() != lineCount) {
        result.add(new Region(null, null, null, last.getEnd(), lineCount));
    }

    return ImmutableList.copyOf(result);
}

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

private static ImmutableMap<CityDMARegionKey, CityDMARegionValue> loadCitiesDMARegions(
        final Interner<String> interner, Transport transport, String resourceName) {
    final Map<CityDMARegionKey, CityDMARegionValue> map = new LinkedHashMap<>();
    try (InputStream is = transport.open(resourceName)) {
        CSVParser.csvParser().parse(is, ".*,(\\d+),.*,.*,(\\d+)", new Function<List<String>, Boolean>() {
            @Override//from   ww  w. j  a  v a2 s  .co  m
            public Boolean apply(List<String> fields) {
                map.put(new CityDMARegionKey(Integer.parseInt(fields.get(1)), interner.intern(fields.get(3))),
                        new CityDMARegionValue(Integer.parseInt(fields.get(4)), interner.intern(fields.get(0)),
                                interner.intern(fields.get(2))));
                return true;
            }
        });
    } catch (IOException e) {
        throw new ExceptionInInitializerError(e);
    }
    return ImmutableMap.copyOf(map);
}

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

private static ImmutableMap<Integer, String> load(Interner<String> interner, Transport transport,
        String resourceName) {//from w ww. ja  va  2 s. c o  m
    try (InputStream isMetadata = transport.open(resourceName)) {
        Pattern pattern = Pattern.compile("(\\d+)\\s+(.*)");
        ImmutableMap.Builder<Integer, String> builder = ImmutableMap.builder();
        BufferedReader rd = new BufferedReader(new InputStreamReader(isMetadata));
        String record;

        while ((record = rd.readLine()) != null) {
            Matcher matcher = pattern.matcher(record);

            if (matcher.matches()) {
                try {
                    builder.put(Integer.parseInt(matcher.group(1)), interner.intern(matcher.group(2)));
                } catch (NumberFormatException e) {
                    logger.trace("Bad record, ignoring: {} - [{}]", e.toString(), record);
                }
            }
        }

        return builder.build();
    } catch (IOException e) {
        throw new ExceptionInInitializerError(e);
    }
}

From source file:org.eclipse.scada.da.master.AbstractConfigurableMasterHandlerImpl.java

protected String getPrefixed(final String id, final Interner<String> stringInterner) {
    if (id == null) {
        return this.prefix;
    } else {/*from  w ww.j av  a  2 s. c  om*/
        return stringInterner.intern(this.dotPrefix + id);
    }
}

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

private static ImmutableMap<Integer, GeoTarget> loadGeoTargets(final Interner<String> interner,
        Transport transport, String resourceName) {
    final Map<Integer, GeoTarget> targetsById = new LinkedHashMap<>();
    final Map<Integer, List<Integer>> parentIdsById = new LinkedHashMap<>();
    final Map<String, GeoTarget> targetsByCanon = new LinkedHashMap<>();
    final Set<String> duplicateCanon = new LinkedHashSet<>();

    try (InputStream is = transport.open(resourceName)) {
        CSVParser.csvParser().parse(is, "(\\d+),(.*)", new Function<List<String>, Boolean>() {
            @Override//from   w  ww  .j  a v a  2  s .  c  o m
            public Boolean apply(List<String> fields) {
                try {
                    if (fields.size() == 7) {
                        GeoTarget target = new GeoTarget(Integer.valueOf(fields.get(0)),
                                Type.valueOf(toEnumName(fields.get(6))), interner.intern(fields.get(2)),
                                interner.intern(fields.get(1)), interner.intern(fields.get(5)), null, null);
                        List<Integer> idParent = Lists.transform(CSVParser.csvParser().parse(fields.get(3)),
                                new Function<String, Integer>() {
                                    @Override
                                    public Integer apply(@Nullable String id) {
                                        return Integer.valueOf(id);
                                    }
                                });

                        targetsById.put(target.criteriaId(), target);
                        parentIdsById.put(target.criteriaId(), idParent);

                        if (targetsByCanon.containsKey(target.canonicalName())) {
                            duplicateCanon.add(target.canonicalName());
                            targetsByCanon.remove(target.canonicalName());
                        } else {
                            targetsByCanon.put(target.canonicalName(), target);
                        }
                    }
                } catch (ParseException | IllegalArgumentException e) {
                    logger.trace("Bad record [{}]: {}", fields, e.toString());
                }
                return true;
            }
        });

        for (Map.Entry<Integer, GeoTarget> entry : targetsById.entrySet()) {
            GeoTarget target = entry.getValue();
            GeoTarget canonParent = targetsByCanon.get(target.findCanonParentName());
            if (canonParent != null) {
                target.setCanonParent(canonParent);
            }
        }

        for (Map.Entry<Integer, GeoTarget> entry : targetsById.entrySet()) {
            GeoTarget target = entry.getValue();
            List<Integer> parentIds = parentIdsById.get(target.criteriaId());
            for (Integer parentId : parentIds) {
                GeoTarget idParent = targetsById.get(parentId);
                if (idParent != null) {
                    target.setIdParent(idParent);
                    break;
                }
            }
        }

        return ImmutableMap.copyOf(targetsById);
    } catch (IOException e) {
        throw new ExceptionInInitializerError(e);
    }
}

From source file:org.apache.hadoop.hive.ql.plan.PartitionDesc.java

public void intern(Interner<TableDesc> interner) {
    this.tableDesc = interner.intern(tableDesc);
}

From source file:com.lithium.flow.geo.Ip2LocationGeoReader.java

@Override
@Nonnull//from ww w . j a v  a  2 s . c om
public List<GeoBlock> readBlocks(@Nonnull File file) throws IOException {
    checkNotNull(file);

    long time = System.currentTimeMillis();
    List<GeoBlock> blocks = Lists.newArrayList();
    Interner<String> interner = Interners.newStrongInterner();
    CSVReader reader = new CSVReader(new FileReader(file));

    String[] line;
    while ((line = reader.readNext()) != null) {
        long start = Long.parseLong(line[0]);
        long end = Long.parseLong(line[1]);
        String countryCode = fixUnknown(interner.intern(line[2].toLowerCase()));
        String countryName = fixUnknown(interner.intern(WordUtils.capitalizeFully(line[3])));
        String region = interner.intern(WordUtils.capitalizeFully(line[4]));
        String city = interner.intern(WordUtils.capitalizeFully(line[5]));
        double latitude = Double.parseDouble(line[6]);
        double longitude = Double.parseDouble(line[7]);
        String postal = line.length <= 8 ? "unknown" : fixUnknown(interner.intern(line[8]));
        String timeZone = line.length <= 9 ? "unknown" : fixUnknown(interner.intern(line[9]));

        GeoDetail detail = new GeoDetail(city, region, postal, countryCode, countryName, latitude, longitude,
                timeZone);
        GeoBlock block = new GeoBlock(start, end, detail);
        blocks.add(block);
    }
    reader.close();

    time = System.currentTimeMillis() - time;
    log.info("read {} blocks in {}ms", blocks.size(), time);
    return blocks;
}

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

/**
 * Split the file into a filename/directory pair.
 *
 * @param interner The interner to use for interning the strings.
 * @param input The input to split./*  www  . ja va 2  s  . c om*/
 * @return The pair of directory, filename.
 */
private FileName splitFileName(Interner<String> interner, String input) {
    int hashIndex = input.lastIndexOf('/');
    String dir = interner.intern(input.substring(0, hashIndex + 1));
    String fileName = interner.intern(input.substring(hashIndex + 1));
    return new FileName(dir, fileName);
}

From source file:org.eclipse.scada.ae.monitor.script.ScriptMonitor.java

public ScriptMonitor(final String id, final String factoryId, final Executor executor,
        final BundleContext context, final Interner<String> stringInterner, final EventProcessor eventProcessor,
        final ObjectPoolTracker<DataSource> dataSourcePoolTracker,
        final ObjectPoolTracker<MasterItem> masterItemPoolTracker,
        final ServiceTracker<ConfigurationAdministrator, ConfigurationAdministrator> caTracker) {
    super(id, factoryId, executor, context, stringInterner, eventProcessor);
    this.executor = executor;

    this.prefix = stringInterner.intern(factoryId + ". " + id); //$NON-NLS-1$

    this.classLoader = getClass().getClassLoader();

    this.monitorStateInjector = new MonitorStateInjector(stringInterner);
    this.monitorStateInjector.setPrefix(this.prefix);

    this.handler = new InjectMasterHandler(id, masterItemPoolTracker, 0, caTracker, this.prefix, factoryId);
    this.listener = new MultiDataSourceListener(dataSourcePoolTracker) {

        @Override//from   w w w  .  j av  a 2 s.  c  o m
        protected void handleChange(final Map<String, DataSourceHandler> sources) {
            ScriptMonitor.this.handleChange(sources);
        }
    };
}

From source file:org.openscada.ae.monitor.script.ScriptMonitor.java

public ScriptMonitor(final String id, final String factoryId, final Executor executor,
        final BundleContext context, final Interner<String> stringInterner, final EventProcessor eventProcessor,
        final ObjectPoolTracker<DataSource> dataSourcePoolTracker,
        final ObjectPoolTracker<MasterItem> masterItemPoolTracker,
        final ServiceTracker<ConfigurationAdministrator, ConfigurationAdministrator> caTracker) {
    super(id, factoryId, executor, context, stringInterner, eventProcessor);
    this.executor = executor;

    this.prefix = stringInterner.intern(factoryId + ". " + id); //$NON-NLS-1$

    this.classLoader = getClass().getClassLoader();

    this.monitorStateInjector = new MonitorStateInjector(stringInterner);
    this.monitorStateInjector.setPrefix(this.prefix);

    final ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
    try {// www. j a  v  a 2 s. c  o m
        Thread.currentThread().setContextClassLoader(this.classLoader);
        this.manager = new ScriptEngineManager(this.classLoader);
    } finally {
        Thread.currentThread().setContextClassLoader(currentClassLoader);
    }

    this.handler = new InjectMasterHandler(id, masterItemPoolTracker, 0, caTracker, this.prefix, factoryId);
    this.listener = new MultiDataSourceListener(dataSourcePoolTracker) {

        @Override
        protected void handleChange(final Map<String, DataSourceHandler> sources) {
            ScriptMonitor.this.handleChange(sources);
        }
    };
}