com.google.common.collect.Interner Java Examples

The following examples show how to use com.google.common.collect.Interner. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: InternerHelper.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
public static Interner<String> makeInterner ( final String specificPropertyName, final String defaultType )
{
    final String type = System.getProperty ( specificPropertyName, System.getProperty ( "org.eclipse.scada.defaultStringInterner", defaultType ) );
    if ( "weak".equals ( type ) )
    {
        return new NullSafeInterner ( Interners.<String> newWeakInterner () );
    }
    else if ( "strong".equals ( type ) )
    {
        return new NullSafeInterner ( Interners.<String> newStrongInterner () );
    }
    else if ( "java".equals ( type ) )
    {
        return new JavaStringInterner ();
    }
    else
    {
        return makeNoOpInterner ();
    }
}
 
Example #2
Source File: ScriptMonitor.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
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
        protected void handleChange ( final Map<String, DataSourceHandler> sources )
        {
            ScriptMonitor.this.handleChange ( sources );
        }
    };
}
 
Example #3
Source File: NinjaScope.java    From bazel with Apache License 2.0 6 votes vote down vote up
/**
 * Partially expands variable value at the given offset. If some of the variable references, used
 * in the value, can not be found, they are left unexpanded.
 */
public NinjaVariableValue getReducedValue(
    long offset,
    NinjaVariableValue value,
    ImmutableSet<String> variablesToNotExpand,
    Interner<String> interner) {
  // Cache expanded variables values to save time replacing several references to the same
  // variable.
  // This cache is local to the offset, it depends on the offset of the variable we are expanding.
  Map<String, String> cache = Maps.newHashMap();
  // We are using the start offset of the value holding the reference to the variable.
  // Do the same as Ninja implementation: if the variable is not found, use empty string.
  Function<String, String> expander =
      ref -> computeExpandedString(offset, ref, cache, variablesToNotExpand, interner);
  return value.reduce(expander);
}
 
Example #4
Source File: NinjaScope.java    From bazel with Apache License 2.0 6 votes vote down vote up
private String computeExpandedString(
    long offset,
    String key,
    Map<String, String> cache,
    ImmutableSet<String> variablesToNotExpand,
    Interner<String> interner) {
  String cachedValue = cache.get(key);
  if (cachedValue != null) {
    return cachedValue;
  }
  if (variablesToNotExpand.contains(key)) {
    return null;
  }
  String expandedValue = findExpandedVariable(offset, key);
  // It's very important an interner is used here, as there is considerable duplication of
  // string literals in expanded rule-variable-parts over the course of a large build.
  return expandedValue == null ? null : interner.intern(expandedValue);
}
 
Example #5
Source File: NinjaTarget.java    From bazel with Apache License 2.0 5 votes vote down vote up
/**
 * We expand the rule's variables with the following assumptions: Rule variables can refer to
 * target's variables (and file variables). Interdependence between rule variables can happen
 * only for 'command' variable, for now we ignore other possible dependencies between rule
 * variables (seems the only other variable which can meaningfully depend on sibling variables
 * is description, and currently we are ignoring it).
 *
 * <p>Also, for resolving rule's variables we are using scope+offset of target, according to
 * specification (https://ninja-build.org/manual.html#_variable_expansion).
 *
 * <p>See {@link NinjaRuleVariable} for the list.
 */
private static ImmutableSortedMap<NinjaRuleVariable, NinjaVariableValue> reduceRuleVariables(
    NinjaScope targetScope,
    long targetOffset,
    Map<NinjaRuleVariable, NinjaVariableValue> ruleVariables,
    ImmutableSortedMap<String, String> targetVariables,
    Interner<String> interner) {
  ImmutableSortedMap.Builder<String, List<Pair<Long, String>>> variablesBuilder =
      ImmutableSortedMap.naturalOrder();
  targetVariables.forEach(
      (key, value) -> variablesBuilder.put(key, ImmutableList.of(Pair.of(0L, value))));
  NinjaScope scopeWithVariables =
      targetScope.createScopeFromExpandedValues(variablesBuilder.build());

  ImmutableSortedMap.Builder<NinjaRuleVariable, NinjaVariableValue> builder =
      ImmutableSortedMap.naturalOrder();

  // Description is taken from the "build" statement (instead of the referenced rule)
  // if it's available.
  boolean targetHasDescription = false;
  String targetVariable = targetVariables.get("description");
  if (targetVariable != null) {
    builder.put(
        NinjaRuleVariable.DESCRIPTION, NinjaVariableValue.createPlainText(targetVariable));
    targetHasDescription = true;
  }

  for (Map.Entry<NinjaRuleVariable, NinjaVariableValue> entry : ruleVariables.entrySet()) {
    NinjaRuleVariable type = entry.getKey();
    if (type.equals(NinjaRuleVariable.DESCRIPTION) && targetHasDescription) {
      // Don't use the rule description, as the target defined a specific description.
      continue;
    }
    NinjaVariableValue reducedValue =
        scopeWithVariables.getReducedValue(
            targetOffset, entry.getValue(), INPUTS_OUTPUTS_VARIABLES, interner);
    builder.put(type, reducedValue);
  }
  return builder.build();
}
 
Example #6
Source File: NinjaTarget.java    From bazel with Apache License 2.0 5 votes vote down vote up
private Builder(NinjaScope scope, long offset, Interner<String> nameInterner) {
  this.scope = scope;
  this.offset = offset;
  inputsBuilder = ImmutableSortedKeyListMultimap.builder();
  outputsBuilder = ImmutableSortedKeyListMultimap.builder();
  variablesBuilder = ImmutableSortedMap.naturalOrder();
  this.nameInterner = nameInterner;
}
 
Example #7
Source File: NinjaParserStep.java    From bazel with Apache License 2.0 5 votes vote down vote up
public NinjaParserStep(
    NinjaLexer lexer,
    Interner<PathFragment> pathFragmentInterner,
    Interner<String> nameInterner) {
  this.lexer = lexer;
  this.pathFragmentInterner = pathFragmentInterner;
  this.nameInterner = nameInterner;
}
 
Example #8
Source File: RuleClass.java    From bazel with Apache License 2.0 5 votes vote down vote up
/**
 * Converts the build-language-typed {@code buildLangValue} to a native value via {@link
 * BuildType#selectableConvert}. Canonicalizes the value's order if it is a {@link List} type and
 * {@code attr.isOrderIndependent()} returns {@code true}.
 *
 * <p>Throws {@link ConversionException} if the conversion fails, or if {@code buildLangValue} is
 * a selector expression but {@code attr.isConfigurable()} is {@code false}.
 */
private static Object convertFromBuildLangType(
    Rule rule,
    Attribute attr,
    Object buildLangValue,
    ImmutableMap<RepositoryName, RepositoryName> repositoryMapping,
    Interner<ImmutableList<?>> listInterner)
    throws ConversionException {
  LabelConversionContext context = new LabelConversionContext(rule.getLabel(), repositoryMapping);
  Object converted =
      BuildType.selectableConvert(
          attr.getType(),
          buildLangValue,
          new AttributeConversionContext(attr.getName(), rule.getRuleClass()),
          context);

  if ((converted instanceof SelectorList<?>) && !attr.isConfigurable()) {
    throw new ConversionException(
        String.format("attribute \"%s\" is not configurable", attr.getName()));
  }

  if (converted instanceof List<?>) {
    if (attr.isOrderIndependent()) {
      @SuppressWarnings("unchecked")
      List<? extends Comparable<?>> list = (List<? extends Comparable<?>>) converted;
      converted = Ordering.natural().sortedCopy(list);
    }
    // It's common for multiple rule instances in the same package to have the same value for some
    // attributes. As a concrete example, consider a package having several 'java_test' instances,
    // each with the same exact 'tags' attribute value.
    converted = listInterner.intern(ImmutableList.copyOf((List<?>) converted));
  }

  return converted;
}
 
Example #9
Source File: InternBuilderUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void interBuilderTest() {

    Interner<Integer> interners = Interners.<Integer> newBuilder()
      .concurrencyLevel(2)
      .strong().<Integer> build();

    Assert.assertNotNull(interners);
}
 
Example #10
Source File: LeafInterner.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Return a {@link LeafInterner} for a particular schema. Interner instances must not be reused for leaves of
 * different types, otherwise they may produce unexpected results.
 *
 * @param schema The leaf node's schema
 * @return An interner instance, if applicable
 */
public static <T extends LeafNode<?>> @NonNull Optional<Interner<T>> forSchema(
        final @Nullable LeafSchemaNode schema) {
    if (schema != null && isLowCardinality(schema.getType())) {
        return Optional.of(LeafInterner::intern);
    }

    return Optional.empty();
}
 
Example #11
Source File: InterningLeafNodeBuilder.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
static <T> @Nullable InterningLeafNodeBuilder<T> forSchema(final @Nullable DataSchemaNode schema) {
    if (schema instanceof LeafSchemaNode) {
        final Optional<Interner<LeafNode<T>>> interner = LeafInterner.forSchema((LeafSchemaNode)schema);
        if (interner.isPresent()) {
            return new InterningLeafNodeBuilder<>(interner.get());
        }
    }
    return null;
}
 
Example #12
Source File: DoubleClickMetadata.java    From openrtb-doubleclick with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the DoubleClickMetadata object.
 *
 * @param transport How to read the many files providing metadata information.
 *     Typically you will use one of the provided implementations,
 *     {@link URLConnectionTransport} or {@link ResourceTransport}
 */
@Inject
public DoubleClickMetadata(Transport transport) {
  Pattern ssvp = Pattern.compile("(\\d+)\\s+(.*)");
  Pattern csvp = Pattern.compile("(\\d+),(.*)");

  Interner<String> interner = Interners.<String>newStrongInterner();
  vendors = load(interner, transport, ssvp, ADX_DICT + "vendors.txt");
  HashMap<Integer, String> cats = new HashMap<>();
  cats.putAll(adSensitiveCategories = load(
      interner, transport, ssvp, ADX_DICT + "ad-sensitive-categories.txt"));
  cats.putAll(adProductCategories = load(
      interner, transport, ssvp, ADX_DICT + "ad-product-categories.txt"));
  cats.putAll(adRestrictedCategories = load(
      interner, transport, ssvp, ADX_DICT + "ad-restricted-categories.txt"));
  allAdCategories = ImmutableMap.copyOf(cats);
  agencies = load(interner, transport, ssvp, ADX_DICT + "agencies.txt");
  HashMap<Integer, String> attrs = new HashMap<>();
  attrs.putAll(pubExcCreativeAttributes =
      load(interner, transport, ssvp, ADX_DICT + "publisher-excludable-creative-attributes.txt"));
  attrs.putAll(buyDecCreativeAttributes =
      load(interner, transport, ssvp, ADX_DICT + "buyer-declarable-creative-attributes.txt"));
  allCreativeAttributes = ImmutableMap.copyOf(attrs);
  creativeStatusCodes = load(interner, transport, ssvp, ADX_DICT + "creative-status-codes.txt");
  sellerNetworks = load(interner, transport, ssvp, ADX_DICT + "seller-network-ids.txt");
  siteLists = load(interner, transport, ssvp, ADX_DICT + "site-lists.txt");
  contentLabels = load(interner, transport, ssvp, ADX_DICT + "content-labels.txt");
  publisherVerticals = load(interner, transport, ssvp, ADX_DICT + "publisher-verticals.txt");
  mobileCarriers = load(interner, transport, CSVParser.csvParser(), csvp,
      ADX_DICT + "mobile-carriers.csv");
  geoTargetsByCriteriaId = loadGeoTargets(interner, transport, ADX_DICT + "geo-table.csv");
  HashMap<GeoTarget.CanonicalKey, GeoTarget> byKey = new HashMap<>();
  for (GeoTarget target : geoTargetsByCriteriaId.values()) {
    byKey.put(target.key(), target);
  }
  geoTargetsByCanonicalKey = ImmutableMap.copyOf(byKey);
  countryCodes = loadCountryCodes(interner, ADX_DICT + "countries.txt");
}
 
Example #13
Source File: SourceMapConsumerV1.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Split the file into a filename/directory pair.
 *
 * @param interner The interner to use for interning the strings.
 * @param input The input to split.
 * @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);
}
 
Example #14
Source File: MultiIntervalTriggerTest.java    From buffer-trigger with Artistic License 2.0 5 votes vote down vote up
@Test
void testInvalidBuild() {
    assertThrows(IllegalArgumentException.class,
            () -> SimpleBufferTrigger.<Integer, Set<Interner>> newGenericBuilder()
                    .triggerStrategy(new MultiIntervalTriggerStrategy()
                            .on(1, SECONDS, 1)
                            .on(2, SECONDS, 2)
                    ).consumer(set -> System.out.println("size:" + set.size()))
                    .build());
}
 
Example #15
Source File: MultiIntervalTriggerTest.java    From buffer-trigger with Artistic License 2.0 5 votes vote down vote up
@Test
void test() {
    AtomicInteger assertSize = new AtomicInteger();
    BufferTrigger<Integer> bufferTrigger = SimpleBufferTrigger
            .<Integer, Set<Interner>> newGenericBuilder()
            .triggerStrategy(new MultiIntervalTriggerStrategy()
                    .on(10, SECONDS, 1)
                    .on(5, SECONDS, 10)
                    .on(1, SECONDS, 100)
            )
            .consumer(set -> {
                System.out.println("size:" + set.size());
                assertEquals(set.size(), assertSize.get());
            })
            .build();

    enqueue(bufferTrigger, 100);
    assertSize.set(100);
    sleep(2);
    enqueue(bufferTrigger, 10);
    assertSize.set(10);
    sleep(6);
    enqueue(bufferTrigger, 1);
    assertSize.set(1);
    sleep(11);

    sleepUninterruptibly(10, SECONDS);
}
 
Example #16
Source File: ListMonitorFactory.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
public ListMonitorFactory ( final BundleContext context, final ManageableObjectPool<MonitorService> servicePool, final EventProcessor eventProcessor, final Executor executor, final Interner<String> stringInterner, final ObjectPoolTracker<MasterItem> poolTracker )
{
    super ( context, servicePool, eventProcessor );
    this.executor = executor;
    this.stringInterner = stringInterner;
    this.poolTracker = poolTracker;
}
 
Example #17
Source File: LevelMonitorFactory.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
public LevelMonitorFactory ( final BundleContext context, final ManageableObjectPool<MonitorService> servicePool, final EventProcessor eventProcessor, final Executor executor, final Interner<String> stringInterner, final ObjectPoolTracker<MasterItem> poolTracker )
{
    super ( context, servicePool, eventProcessor );
    this.executor = executor;
    this.stringInterner = stringInterner;
    this.poolTracker = poolTracker;
}
 
Example #18
Source File: BitMonitorFactory.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
public BitMonitorFactory ( final BundleContext context, final ManageableObjectPool<MonitorService> servicePool, final EventProcessor eventProcessor, final Executor executor, final Interner<String> stringInterner, final ObjectPoolTracker<MasterItem> poolTracker )
{
    super ( context, servicePool, eventProcessor );
    this.executor = executor;
    this.stringInterner = stringInterner;
    this.poolTracker = poolTracker;
}
 
Example #19
Source File: ConfigurationAdminImpl.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
public ConfigurationAdminImpl ( final BundleContext context, final Interner<String> stringInterner ) throws Exception
{
    super ( context );
    this.stringInterner = stringInterner;
    this.context = context;
    initRoot ();
}
 
Example #20
Source File: MerkleTreeNodeCache.java    From buck with Apache License 2.0 5 votes vote down vote up
public MerkleTreeNode build(Interner<MerkleTreeNode> nodeInterner) {
  ImmutableSortedMap.Builder<Path, MerkleTreeNode> children = ImmutableSortedMap.naturalOrder();
  childrenBuilder.forEach(
      (key, value) ->
          children.put(
              key, value.transform(left -> left, builder -> builder.build(nodeInterner))));

  return nodeInterner.intern(
      new MerkleTreeNode(
          path,
          children.build(),
          ImmutableSortedMap.copyOf(filesBuilder),
          ImmutableSortedMap.copyOf(symlinksBuilder),
          ImmutableSortedMap.copyOf(emptyDirectoryBuilder)));
}
 
Example #21
Source File: AbstractMasterItemMonitor.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
public AbstractMasterItemMonitor ( final BundleContext context, final Executor executor, final Interner<String> stringInterner, final ObjectPoolTracker<MasterItem> poolTracker, final EventProcessor eventProcessor, final String id, final String factoryId, final String prefix, final String defaultMonitorType )
{
    super ( id, factoryId, executor, context, stringInterner, eventProcessor );
    this.factoryId = factoryId;
    this.executor = executor;
    this.poolTracker = poolTracker;

    this.defaultMonitorType = defaultMonitorType;

    this.monitorStateInjector = new MonitorStateInjector ( stringInterner );

    setPrefix ( prefix );
}
 
Example #22
Source File: AbstractConfigurableMasterHandlerImpl.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
protected String getPrefixed ( final String id, final Interner<String> stringInterner )
{
    if ( id == null )
    {
        return this.prefix;
    }
    else
    {
        return stringInterner.intern ( this.dotPrefix + id );
    }
}
 
Example #23
Source File: InternerHelper.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
public static Interner<String> makeNoOpInterner ()
{
    return new Interner<String> () {

        @Override
        public String intern ( final String string )
        {
            return string;
        }
    };
}
 
Example #24
Source File: ScriptMonitorFactory.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
public ScriptMonitorFactory ( final BundleContext context, final Executor executor, final Interner<String> stringInterner, final EventProcessor eventProcessor, final ObjectPoolTracker<DataSource> dataSourcePoolTracker, final ObjectPoolTracker<MasterItem> masterItemPoolTracker, final ServiceTracker<ConfigurationAdministrator, ConfigurationAdministrator> caTracker, final ObjectPoolImpl<MonitorService> monitorServicePool )
{
    super ( context );
    this.executor = executor;
    this.stringInterner = stringInterner;
    this.eventProcessor = eventProcessor;
    this.dataSourcePoolTracker = dataSourcePoolTracker;
    this.masterItemPoolTracker = masterItemPoolTracker;
    this.caTracker = caTracker;
    this.monitorServicePool = monitorServicePool;
}
 
Example #25
Source File: AbstractMonitorService.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
public AbstractMonitorService ( final String id, final Executor executor, final Interner<String> stringInterner )
{
    this.executor = executor;
    this.id = id;

    this.stringInterner = stringInterner == null ? InternerHelper.makeNoOpInterner () : stringInterner;

    this.currentState = new MonitorStatusInformation ( id, MonitorStatus.INIT, System.currentTimeMillis (), null, null, null, null, null, null, null );
}
 
Example #26
Source File: JdbcStorageDao.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
public JdbcStorageDao ( final DataSourceFactory dataSourceFactory, final Properties properties, final boolean usePool, final Long loginTimeout, final Interner<String> stringInterner ) throws SQLException
{
    super ( dataSourceFactory, properties, usePool, loginTimeout, stringInterner );

    this.executor = Executors.newSingleThreadScheduledExecutor ( new NamedThreadFactory ( "org.eclipse.scada.ae.server.storage.jdbc/CleanupThread" ) );
    this.executor.scheduleWithFixedDelay ( new Runnable () {

        @Override
        public void run ()
        {
            cleanupArchive ();
        }
    }, getCleanupPeriod (), getCleanupPeriod (), TimeUnit.SECONDS );
}
 
Example #27
Source File: JdbcStorageDao.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
public JdbcStorageDao ( final DataSourceFactory dataSourceFactory, final Properties properties, final boolean usePool, final Long loginTimeout, final Interner<String> stringInterner ) throws SQLException
{
    super ( dataSourceFactory, properties, usePool, loginTimeout, stringInterner );
}
 
Example #28
Source File: NinjaTarget.java    From bazel with Apache License 2.0 4 votes vote down vote up
public static Builder builder(NinjaScope scope, long offset, Interner<String> nameInterner) {
  return new Builder(scope, offset, nameInterner);
}
 
Example #29
Source File: NinjaPipeline.java    From bazel with Apache License 2.0 4 votes vote down vote up
/** An String interner for rule and build statements' variable names. */
Interner<String> getNameInterner();
 
Example #30
Source File: NinjaPipelineImpl.java    From bazel with Apache License 2.0 4 votes vote down vote up
@Override
public Interner<PathFragment> getPathFragmentInterner() {
  return pathFragmentInterner;
}