Search in sources :

Example 16 with SortedMap

use of java.util.SortedMap in project buck by facebook.

the class RuleKeyBuilder method setReflectively.

/** Recursively serializes the value. Serialization of the key is handled outside. */
protected RuleKeyBuilder<RULE_KEY> setReflectively(@Nullable Object val) {
    if (val instanceof RuleKeyAppendable) {
        return setAppendableRuleKey((RuleKeyAppendable) val);
    }
    if (val instanceof BuildRule) {
        return setBuildRule((BuildRule) val);
    }
    if (val instanceof Supplier) {
        try (Scope containerScope = scopedHasher.wrapperScope(Wrapper.SUPPLIER)) {
            Object newVal = ((Supplier<?>) val).get();
            return setReflectively(newVal);
        }
    }
    if (val instanceof Optional) {
        Object o = ((Optional<?>) val).orElse(null);
        try (Scope wraperScope = scopedHasher.wrapperScope(Wrapper.OPTIONAL)) {
            return setReflectively(o);
        }
    }
    if (val instanceof Either) {
        Either<?, ?> either = (Either<?, ?>) val;
        if (either.isLeft()) {
            try (Scope wraperScope = scopedHasher.wrapperScope(Wrapper.EITHER_LEFT)) {
                return setReflectively(either.getLeft());
            }
        } else {
            try (Scope wraperScope = scopedHasher.wrapperScope(Wrapper.EITHER_RIGHT)) {
                return setReflectively(either.getRight());
            }
        }
    }
    // Note {@link java.nio.file.Path} implements "Iterable", so we explicitly exclude it here.
    if (val instanceof Iterable && !(val instanceof Path)) {
        try (ContainerScope containerScope = scopedHasher.containerScope(Container.LIST)) {
            for (Object element : (Iterable<?>) val) {
                try (Scope elementScope = containerScope.elementScope()) {
                    setReflectively(element);
                }
            }
            return this;
        }
    }
    if (val instanceof Iterator) {
        Iterator<?> iterator = (Iterator<?>) val;
        try (ContainerScope containerScope = scopedHasher.containerScope(Container.LIST)) {
            while (iterator.hasNext()) {
                try (Scope elementScope = containerScope.elementScope()) {
                    setReflectively(iterator.next());
                }
            }
        }
        return this;
    }
    if (val instanceof Map) {
        if (!(val instanceof SortedMap || val instanceof ImmutableMap)) {
            logger.warn("Adding an unsorted map to the rule key. " + "Expect unstable ordering and caches misses: %s", val);
        }
        try (ContainerScope containerScope = scopedHasher.containerScope(Container.MAP)) {
            for (Map.Entry<?, ?> entry : ((Map<?, ?>) val).entrySet()) {
                try (Scope elementScope = containerScope.elementScope()) {
                    setReflectively(entry.getKey());
                }
                try (Scope elementScope = containerScope.elementScope()) {
                    setReflectively(entry.getValue());
                }
            }
        }
        return this;
    }
    if (val instanceof Path) {
        throw new HumanReadableException("It's not possible to reliably disambiguate Paths. They are disallowed from rule keys");
    }
    if (val instanceof SourcePath) {
        try {
            return setSourcePath((SourcePath) val);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    if (val instanceof NonHashableSourcePathContainer) {
        SourcePath sourcePath = ((NonHashableSourcePathContainer) val).getSourcePath();
        return setNonHashingSourcePath(sourcePath);
    }
    if (val instanceof SourceWithFlags) {
        SourceWithFlags source = (SourceWithFlags) val;
        try {
            setSourcePath(source.getSourcePath());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        setReflectively(source.getFlags());
        return this;
    }
    return setSingleValue(val);
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) ArchiveMemberSourcePath(com.facebook.buck.rules.ArchiveMemberSourcePath) ArchiveMemberPath(com.facebook.buck.io.ArchiveMemberPath) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) Optional(java.util.Optional) ContainerScope(com.facebook.buck.rules.keys.RuleKeyScopedHasher.ContainerScope) NonHashableSourcePathContainer(com.facebook.buck.rules.NonHashableSourcePathContainer) IOException(java.io.IOException) SourceWithFlags(com.facebook.buck.rules.SourceWithFlags) ImmutableMap(com.google.common.collect.ImmutableMap) SourcePath(com.facebook.buck.rules.SourcePath) ArchiveMemberSourcePath(com.facebook.buck.rules.ArchiveMemberSourcePath) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) Scope(com.facebook.buck.rules.keys.RuleKeyScopedHasher.Scope) ContainerScope(com.facebook.buck.rules.keys.RuleKeyScopedHasher.ContainerScope) HumanReadableException(com.facebook.buck.util.HumanReadableException) SortedMap(java.util.SortedMap) Iterator(java.util.Iterator) Either(com.facebook.buck.model.Either) BuildRule(com.facebook.buck.rules.BuildRule) Supplier(com.google.common.base.Supplier) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) SortedMap(java.util.SortedMap) RuleKeyAppendable(com.facebook.buck.rules.RuleKeyAppendable)

Example 17 with SortedMap

use of java.util.SortedMap in project elasticsearch by elastic.

the class WordDelimiterTokenFilterFactory method parseTypes.

/**
     * parses a list of MappingCharFilter style rules into a custom byte[] type table
     */
static byte[] parseTypes(Collection<String> rules) {
    SortedMap<Character, Byte> typeMap = new TreeMap<>();
    for (String rule : rules) {
        Matcher m = typePattern.matcher(rule);
        if (!m.find())
            throw new RuntimeException("Invalid Mapping Rule : [" + rule + "]");
        String lhs = parseString(m.group(1).trim());
        Byte rhs = parseType(m.group(2).trim());
        if (lhs.length() != 1)
            throw new RuntimeException("Invalid Mapping Rule : [" + rule + "]. Only a single character is allowed.");
        if (rhs == null)
            throw new RuntimeException("Invalid Mapping Rule : [" + rule + "]. Illegal type.");
        typeMap.put(lhs.charAt(0), rhs);
    }
    // ensure the table is always at least as big as DEFAULT_WORD_DELIM_TABLE for performance
    byte[] types = new byte[Math.max(typeMap.lastKey() + 1, WordDelimiterIterator.DEFAULT_WORD_DELIM_TABLE.length)];
    for (int i = 0; i < types.length; i++) types[i] = WordDelimiterIterator.getType(i);
    for (Map.Entry<Character, Byte> mapping : typeMap.entrySet()) types[mapping.getKey()] = mapping.getValue();
    return types;
}
Also used : Matcher(java.util.regex.Matcher) TreeMap(java.util.TreeMap) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 18 with SortedMap

use of java.util.SortedMap in project sonarqube by SonarSource.

the class SettingsMonitor method attributes.

@Override
public SortedMap<String, Object> attributes() {
    PropertyDefinitions definitions = settings.getDefinitions();
    ImmutableSortedMap.Builder<String, Object> builder = ImmutableSortedMap.naturalOrder();
    for (Map.Entry<String, String> prop : settings.getProperties().entrySet()) {
        String key = prop.getKey();
        PropertyDefinition def = definitions.get(key);
        if (def == null || def.type() != PropertyType.PASSWORD) {
            builder.put(key, abbreviate(prop.getValue(), MAX_VALUE_LENGTH));
        }
    }
    return builder.build();
}
Also used : PropertyDefinitions(org.sonar.api.config.PropertyDefinitions) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) Map(java.util.Map) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) SortedMap(java.util.SortedMap) PropertyDefinition(org.sonar.api.config.PropertyDefinition)

Example 19 with SortedMap

use of java.util.SortedMap in project newts by OpenNMS.

the class NewtsReporter method report.

@Override
@SuppressWarnings("rawtypes")
public void report(SortedMap<String, Gauge> gauges, SortedMap<String, Counter> counters, SortedMap<String, Histogram> histograms, SortedMap<String, Meter> meters, SortedMap<String, Timer> timers) {
    Timestamp timestamp = Timestamp.fromEpochMillis(clock.getTime());
    List<Sample> samples = Lists.newArrayList();
    for (Map.Entry<String, Gauge> entry : gauges.entrySet()) {
        reportGauge(samples, timestamp, entry.getKey(), entry.getValue());
    }
    for (Map.Entry<String, Counter> entry : counters.entrySet()) {
        reportCounter(samples, timestamp, entry.getKey(), entry.getValue());
    }
    for (Map.Entry<String, Histogram> entry : histograms.entrySet()) {
        reportHistogram(samples, timestamp, entry.getKey(), entry.getValue());
    }
    for (Map.Entry<String, Meter> entry : meters.entrySet()) {
        reportMeter(samples, timestamp, entry.getKey(), entry.getValue());
    }
    for (Map.Entry<String, Timer> entry : timers.entrySet()) {
        reportTimer(samples, timestamp, entry.getKey(), entry.getValue());
    }
    this.repository.insert(samples);
}
Also used : Histogram(com.codahale.metrics.Histogram) Meter(com.codahale.metrics.Meter) Sample(org.opennms.newts.api.Sample) Timestamp(org.opennms.newts.api.Timestamp) Gauge(com.codahale.metrics.Gauge) Counter(com.codahale.metrics.Counter) Timer(com.codahale.metrics.Timer) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 20 with SortedMap

use of java.util.SortedMap in project mapdb by jankotek.

the class ConcurrentSkipListSubMapTest method testHeadMapContents.

/**
     * headMap returns map with keys in requested range
     */
@Test
public void testHeadMapContents() {
    ConcurrentNavigableMap map = map5();
    SortedMap sm = map.headMap(four);
    assertTrue(sm.containsKey(one));
    assertTrue(sm.containsKey(two));
    assertTrue(sm.containsKey(three));
    assertFalse(sm.containsKey(four));
    assertFalse(sm.containsKey(five));
    Iterator i = sm.keySet().iterator();
    Object k;
    k = (Integer) (i.next());
    assertEquals(one, k);
    k = (Integer) (i.next());
    assertEquals(two, k);
    k = (Integer) (i.next());
    assertEquals(three, k);
    assertFalse(i.hasNext());
    if (isReadOnly())
        return;
    sm.clear();
    assertTrue(sm.isEmpty());
    assertEquals(2, map.size());
    assertEquals(four, map.firstKey());
}
Also used : SortedMap(java.util.SortedMap) Iterator(java.util.Iterator) ConcurrentNavigableMap(java.util.concurrent.ConcurrentNavigableMap) Test(org.junit.Test)

Aggregations

SortedMap (java.util.SortedMap)216 Map (java.util.Map)162 TreeMap (java.util.TreeMap)108 HashMap (java.util.HashMap)59 Iterator (java.util.Iterator)31 NavigableMap (java.util.NavigableMap)31 ArrayList (java.util.ArrayList)27 Test (org.junit.Test)25 IOException (java.io.IOException)20 ImmutableMap (com.google.common.collect.ImmutableMap)19 JASIExpr (org.matheclipse.core.convert.JASIExpr)16 IExpr (org.matheclipse.core.interfaces.IExpr)16 List (java.util.List)15 File (java.io.File)14 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)14 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)12 Entry (java.util.Map.Entry)12 ConcurrentNavigableMap (java.util.concurrent.ConcurrentNavigableMap)12 ConcurrentMap (java.util.concurrent.ConcurrentMap)11 LinkedHashMap (java.util.LinkedHashMap)9