com.google.common.collect.TreeMultiset Java Examples

The following examples show how to use com.google.common.collect.TreeMultiset. 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: GuavaTreeMultisetTest.java    From java_in_examples with Apache License 2.0 7 votes vote down vote up
public static void main(String[] args) {
    // Parse text to separate words
    String INPUT_TEXT = "Hello World! Hello All! Hi World!";
    // Create Multiset
    Multiset<String> multiset = TreeMultiset.create(Arrays.asList(INPUT_TEXT.split(" ")));

    // Print count words
    System.out.println(multiset); // print [All!, Hello x 2, Hi, World! x 2]- in natural (alphabet) order
    // Print all unique words
    System.out.println(multiset.elementSet());    // print [All!, Hello, Hi, World!]- in natural (alphabet) order

    // Print count occurrences of words
    System.out.println("Hello = " + multiset.count("Hello"));    // print 2
    System.out.println("World = " + multiset.count("World!"));    // print 2
    System.out.println("All = " + multiset.count("All!"));    // print 1
    System.out.println("Hi = " + multiset.count("Hi"));    // print 1
    System.out.println("Empty = " + multiset.count("Empty"));    // print 0

    // Print count all words
    System.out.println(multiset.size());    //print 6

    // Print count unique words
    System.out.println(multiset.elementSet().size());    //print 4
}
 
Example #2
Source File: VinSamplerTest.java    From log-synth with Apache License 2.0 6 votes vote down vote up
@Test
public void testSchema() throws IOException {
    //noinspection UnstableApiUsage
    SchemaSampler s = new SchemaSampler(Resources.asCharSource(Resources.getResource("schema014.json"), Charsets.UTF_8).read());

    Multiset<String> prefixCounts = TreeMultiset.create();
    Multiset<String> otherCounts = TreeMultiset.create();
    for (int i = 0; i < 100; i++) {
        JsonNode r = s.sample();
        assertEquals(r.get("v1").asText(), r.get("v2").get("VIN").asText());
        prefixCounts.add(r.get("v1").asText().substring(0, 2));
        otherCounts.add(r.get("v3").asText().substring(0, 2));
        System.out.printf("%s\n", r);
    }
    assertEquals("[1F, 2F, 3F]", prefixCounts.elementSet().toString());
    assertEquals("[2F, 3F]", otherCounts.elementSet().toString());
}
 
Example #3
Source File: EntityScorer.java    From entity-fishing with Apache License 2.0 6 votes vote down vote up
public ScorerContext context(List<String> words) {
    Multiset<String> counter = TreeMultiset.create();
    counter.addAll(words);

    int word_dim = kb.getEmbeddingsSize();
    // word_vecs is the concatenation of all word vectors of the word list
    float[] word_vecs = new float[counter.size() * word_dim];
    IntArrayList word_counts = new IntArrayList();
    int n_words = 0;

    for(Multiset.Entry<String> entry : counter.entrySet()) {
        short[] vector = kb.getWordEmbeddings(entry.getElement());
        if (vector != null) {
            word_counts.add(entry.getCount());
            for (int i=0; i<kb.getEmbeddingsSize(); i++) {
                word_vecs[n_words * word_dim + i] = vector[i];
            }
            n_words += 1;
        }
    }
    word_counts.trim();

    return create_context(word_vecs, word_counts.elements());
}
 
Example #4
Source File: GuavaTreeMultisetTest.java    From java_in_examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    // Разберем текст на слова
    String INPUT_TEXT = "Hello World! Hello All! Hi World!";
    // Создаем Multiset
    Multiset<String> multiset = TreeMultiset.create(Arrays.asList(INPUT_TEXT.split(" ")));

    // Выводим кол-вом вхождений слов
    System.out.println(multiset); // напечатает [All!, Hello x 2, Hi, World! x 2]- в алфавитном порядке
    // Выводим все уникальные слова
    System.out.println(multiset.elementSet());    // напечатает [All!, Hello, Hi, World!]- в алфавитном порядке

    // Выводим количество по каждому слову
    System.out.println("Hello = " + multiset.count("Hello"));    // напечатает 2
    System.out.println("World = " + multiset.count("World!"));    // напечатает 2
    System.out.println("All = " + multiset.count("All!"));    // напечатает 1
    System.out.println("Hi = " + multiset.count("Hi"));    // напечатает 1
    System.out.println("Empty = " + multiset.count("Empty"));    // напечатает 0

    // Выводим общее количества всех слов в тексте
    System.out.println(multiset.size());    //напечатает 6

    // Выводим общее количество всех уникальных слов
    System.out.println(multiset.elementSet().size());    //напечатает 4
}
 
Example #5
Source File: MultisetGwtTest.java    From gwt-jackson with Apache License 2.0 6 votes vote down vote up
public void testDeserialization() {
    String input = "{" +
            "\"multiset\":[\"foo\",\"abc\",\"abc\",null]," +
            "\"hashMultiset\":[\"abc\",\"abc\"]," +
            "\"linkedHashMultiset\":[\"foo\",\"abc\",\"abc\",null]," +
            "\"sortedMultiset\":[\"foo\",\"abc\",\"bar\",\"abc\",null]," +
            "\"treeMultiset\":[\"bar\",\"abc\",\"abc\",\"foo\",null]," +
            "\"immutableMultiset\":[\"foo\",\"abc\",\"abc\",\"bar\",null]," +
            "\"enumMultiset\":[\"B\",\"A\",\"A\",\"D\",null]" +
            "}";

    BeanWithMultisetTypes result = BeanWithMultisetTypesMapper.INSTANCE.read( input );
    assertNotNull( result );

    List<String> expectedList = Arrays.asList( "foo", "abc", null, "abc" );
    List<String> expectedListWithNonNull = Arrays.asList( "foo", "abc", "bar", "abc" );

    assertEquals( LinkedHashMultiset.create( expectedList ), result.multiset );
    assertEquals( HashMultiset.create( Arrays.asList( "abc", "abc" ) ), result.hashMultiset );
    assertEquals( LinkedHashMultiset.create( expectedList ), result.linkedHashMultiset );
    assertEquals( TreeMultiset.create( expectedListWithNonNull ), result.sortedMultiset );
    assertEquals( TreeMultiset.create( expectedListWithNonNull ), result.treeMultiset );
    assertEquals( ImmutableMultiset.copyOf( expectedListWithNonNull ), result.immutableMultiset );
    assertEquals( EnumMultiset.create( Arrays.asList( AlphaEnum.B, AlphaEnum.A, AlphaEnum.D, AlphaEnum.A ) ), result.enumMultiset );
}
 
Example #6
Source File: AbstractIdentifierRenamings.java    From naturalize with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * @param relevantNgrams
 * @param currentName
 * @return
 */
public Multiset<String> getAlternativeNames(
		final Multiset<NGram<String>> relevantNgrams,
		final String currentName) {
	// Get all alternative namings
	final Multiset<String> nameAlternatives = ngramLM
			.getAlternativeNamings(relevantNgrams, WILDCARD_TOKEN);
	nameAlternatives.add(currentName); // Give the current identifier a
										// chance...

	// Prune naming alternatives
	final Multiset<String> toKeep = TreeMultiset.create();

	int seen = 0;
	for (final Entry<String> ent : Multisets.copyHighestCountFirst(
			nameAlternatives).entrySet()) {
		if (seen > 1000) {
			break;
		}
		toKeep.add(ent.getElement(), ent.getCount());
		seen++;
	}
	toKeep.add(AbstractNGramLM.UNK_SYMBOL);
	return toKeep;
}
 
Example #7
Source File: MultisetGwtTest.java    From gwt-jackson with Apache License 2.0 6 votes vote down vote up
public void testSerialization() {
    BeanWithMultisetTypes bean = new BeanWithMultisetTypes();

    List<String> list = Arrays.asList( "foo", "abc", null, "abc" );
    List<String> listWithNonNull = Arrays.asList( "foo", "abc", "bar", "abc" );

    bean.multiset = LinkedHashMultiset.create( list );
    bean.hashMultiset = HashMultiset.create( Arrays.asList( "abc", "abc" ) );
    bean.linkedHashMultiset = LinkedHashMultiset.create( list );
    bean.sortedMultiset = TreeMultiset.create( listWithNonNull );
    bean.treeMultiset = TreeMultiset.create( listWithNonNull );
    bean.immutableMultiset = ImmutableMultiset.copyOf( listWithNonNull );
    bean.enumMultiset = EnumMultiset.create( Arrays.asList( AlphaEnum.B, AlphaEnum.A, AlphaEnum.D, AlphaEnum.A ) );

    String expected = "{" +
            "\"multiset\":[\"foo\",\"abc\",\"abc\",null]," +
            "\"hashMultiset\":[\"abc\",\"abc\"]," +
            "\"linkedHashMultiset\":[\"foo\",\"abc\",\"abc\",null]," +
            "\"sortedMultiset\":[\"abc\",\"abc\",\"bar\",\"foo\"]," +
            "\"treeMultiset\":[\"abc\",\"abc\",\"bar\",\"foo\"]," +
            "\"immutableMultiset\":[\"foo\",\"abc\",\"abc\",\"bar\"]," +
            "\"enumMultiset\":[\"A\",\"A\",\"B\",\"D\"]" +
            "}";

    assertEquals( expected, BeanWithMultisetTypesMapper.INSTANCE.write( bean ) );
}
 
Example #8
Source File: TreeMultisetDeserializer.java    From jackson-datatypes-collections with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
protected TreeMultiset<Object> createMultiset() {
    @SuppressWarnings("rawtypes")
    TreeMultiset<?> naturalOrder = TreeMultiset.<Comparable> create();
    return (TreeMultiset<Object>) naturalOrder;
}
 
Example #9
Source File: SchemaSamplerTest.java    From log-synth with Apache License 2.0 5 votes vote down vote up
@Test
public void testSkewedInteger() throws IOException {
    // will give fields x, y, z, q with different skewness
    //noinspection UnstableApiUsage
    SchemaSampler s = new SchemaSampler(Resources.asCharSource(Resources.getResource("schema007.json"), Charsets.UTF_8).read());

    SortedMultiset<Integer> x = TreeMultiset.create();
    SortedMultiset<Integer> y = TreeMultiset.create();
    SortedMultiset<Integer> z = TreeMultiset.create();
    SortedMultiset<Integer> q = TreeMultiset.create();
    for (int i = 0; i < 10000; i++) {
        JsonNode record = s.sample();
        x.add(record.get("x").asInt());
        y.add(record.get("y").asInt());
        z.add(record.get("z").asInt());
        q.add(record.get("q").asInt());
    }

    for (int i = 10; i < 20; i++) {
        assertEquals(1000, x.count(i), 100);
        assertEquals(1900 - (i - 10) * 200, y.count(i), 120);
        assertEquals(100 + (i - 10) * 200, z.count(i), 120);
        // these magic numbers are a fit to the empirical distribution of q as computed by R
        double kq = 122623.551282 - 27404.139083 * i + 2296.601107 * i * i - 85.510684 * i * i * i + 1.193182 * i * i * i * i;
        // accuracy should get better for smaller numbers
        assertEquals(kq, q.count(i), (25.0 - i) / 10 * 120);
    }
}
 
Example #10
Source File: GoogleTruthUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenCheckingGuavaMultiset_thenExpectedCount() {
    TreeMultiset<String> aMultiset = TreeMultiset.create();
    aMultiset.add("baeldung", 10);

    assertThat(aMultiset).hasCount("baeldung", 10);
}
 
Example #11
Source File: BaseQualityHistogram.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Map<String, Object> annotate(final ReferenceContext ref,
                                    final VariantContext vc,
                                    final AlleleLikelihoods<GATKRead, Allele> likelihoods) {
    Utils.nonNull(vc);
    if ( likelihoods == null ) {
        return Collections.emptyMap();
    }

    final Map<Allele, TreeMultiset<Integer>> values = likelihoods.alleles().stream()
            .collect(Collectors.toMap(a -> a, a -> TreeMultiset.create()));

    Utils.stream(likelihoods.bestAllelesBreakingTies())
            .filter(ba -> ba.isInformative() && isUsableRead(ba.evidence))
            .forEach(ba -> BaseQuality.getBaseQuality(ba.evidence, vc).ifPresent(v -> values.get(ba.allele).add(v)));


    final List<Integer> distinctBaseQualities = likelihoods.alleles().stream()
            .flatMap(a -> values.get(a).stream())
            .distinct()
            .sorted()
            .collect(Collectors.toList());

    final List<Integer> output = new ArrayList<>();

    for (final int qual : distinctBaseQualities) {
        output.add(qual);
        likelihoods.alleles().forEach(allele -> output.add(values.get(allele).count(qual)));
    }


    return ImmutableMap.of(KEY, output);
}
 
Example #12
Source File: PaloAltoConfiguration.java    From batfish with Apache License 2.0 5 votes vote down vote up
private void markAbstractStructureFromUnknownNamespace(
    PaloAltoStructureType type,
    Collection<PaloAltoStructureType> structureTypesToCheck,
    boolean ignoreUndefined,
    PaloAltoStructureUsage... usages) {
  Map<String, SortedMap<StructureUsage, SortedMultiset<Integer>>> references =
      firstNonNull(_structureReferences.get(type), Collections.emptyMap());
  for (PaloAltoStructureUsage usage : usages) {
    references.forEach(
        (nameWithNamespace, byUsage) -> {
          String name = extractObjectName(nameWithNamespace);
          Multiset<Integer> lines = firstNonNull(byUsage.get(usage), TreeMultiset.create());
          // Check this namespace first
          DefinedStructureInfo info =
              findDefinedStructure(nameWithNamespace, structureTypesToCheck);
          // Check shared namespace if there was no match
          if (info == null) {
            info =
                findDefinedStructure(
                    computeObjectName(SHARED_VSYS_NAME, name), structureTypesToCheck);
          }

          // Now update reference count if applicable
          if (info != null) {
            info.setNumReferrers(info.getNumReferrers() + lines.size());
          } else if (!ignoreUndefined) {
            for (int line : lines) {
              undefined(type, name, usage, line);
            }
          }
        });
  }
}
 
Example #13
Source File: VendorConfiguration.java    From batfish with Apache License 2.0 5 votes vote down vote up
public void referenceStructure(StructureType type, String name, StructureUsage usage, int line) {
  SortedMap<String, SortedMap<StructureUsage, SortedMultiset<Integer>>> byName =
      _structureReferences.computeIfAbsent(type, k -> new TreeMap<>());
  SortedMap<StructureUsage, SortedMultiset<Integer>> byUsage =
      byName.computeIfAbsent(name, k -> new TreeMap<>());
  SortedMultiset<Integer> lines = byUsage.computeIfAbsent(usage, k -> TreeMultiset.create());
  lines.add(line);
}
 
Example #14
Source File: VendorConfiguration.java    From batfish with Apache License 2.0 5 votes vote down vote up
/**
 * Updates referrers and/or warns for undefined structures based on references to an abstract
 * {@link StructureType}: a reference type that may refer to one of a number of defined structure
 * types passed in {@code structureTypesToCheck}.
 *
 * <p>For example using Cisco devices, see {@code CiscoStructureType.ACCESS_LIST} and how it
 * expands to a list containing many types of IPv4 and IPv6 access lists.
 */
protected void markAbstractStructure(
    StructureType type,
    StructureUsage usage,
    Collection<? extends StructureType> structureTypesToCheck) {
  Map<String, SortedMap<StructureUsage, SortedMultiset<Integer>>> references =
      firstNonNull(_structureReferences.get(type), Collections.emptyMap());
  references.forEach(
      (name, byUsage) -> {
        Multiset<Integer> lines = firstNonNull(byUsage.get(usage), TreeMultiset.create());
        List<DefinedStructureInfo> matchingStructures =
            structureTypesToCheck.stream()
                .map(t -> _structureDefinitions.get(t.getDescription()))
                .filter(Objects::nonNull)
                .map(m -> m.get(name))
                .filter(Objects::nonNull)
                .collect(ImmutableList.toImmutableList());
        if (matchingStructures.isEmpty()) {
          for (int line : lines) {
            undefined(type, name, usage, line);
          }
        } else {
          matchingStructures.forEach(
              info -> info.setNumReferrers(info.getNumReferrers() + lines.size()));
        }
      });
}
 
Example #15
Source File: SimpleDefinitionFinderTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(NodeTraversal traversal, Node node, Node parent) {
  Collection<Definition> defs =
      passUnderTest.getDefinitionsReferencedAt(node);
  if (defs != null) {
    StringBuilder sb = new StringBuilder();
    sb.append("USE ");
    sb.append(Token.name(node.getType()));
    sb.append(" ");
    sb.append(node.getQualifiedName());
    sb.append(" -> ");
    Multiset<String> defstrs = TreeMultiset.create();
    for (Definition def : defs) {
      String defstr;

      Node rValue = def.getRValue();
      if (rValue != null) {
        defstr = Token.name(rValue.getType());
      } else {
        defstr = "<null>";
      }

      if (def.isExtern()) {
        defstr = "EXTERN " + defstr;
      }

      defstrs.add(defstr);
    }

    sb.append(defstrs.toString());
    found.add(sb.toString());
  }
}
 
Example #16
Source File: Rows.java    From batfish with Apache License 2.0 4 votes vote down vote up
@JsonCreator
public Rows(Multiset<Row> data) {
  _data = data == null ? TreeMultiset.create() : data;
}
 
Example #17
Source File: CaseArgumentAnalyser.java    From JGiven with Apache License 2.0 4 votes vote down vote up
/**
 * Finds for each JoinedArgs set the best fitting name.
 * <p>
 * First it is tried to find a name from the explicitParameterNames list, by comparing the argument values
 * with the explicit case argument values. If no matching value can be found, the name of the argument is taken.
 */
private List<String> findArgumentNames( List<List<JoinedArgs>> joinedArgs, List<List<String>> explicitParameterValues,
        List<String> explicitParameterNames ) {
    List<String> argumentNames = Lists.newArrayListWithExpectedSize( joinedArgs.get( 0 ).size() );
    Multiset<String> paramNames = TreeMultiset.create();

    arguments:
    for( int iArg = 0; iArg < joinedArgs.get( 0 ).size(); iArg++ ) {
        parameters:
        for( int iParam = 0; iParam < explicitParameterNames.size(); iParam++ ) {
            String paramName = explicitParameterNames.get( iParam );

            boolean formattedValueMatches = true;
            boolean valueMatches = true;
            for( int iCase = 0; iCase < joinedArgs.size(); iCase++ ) {
                JoinedArgs args = joinedArgs.get( iCase ).get( iArg );

                String parameterValue = explicitParameterValues.get( iCase ).get( iParam );

                String formattedValue = args.words.get( 0 ).getFormattedValue();
                if( !formattedValue.equals( parameterValue ) ) {
                    formattedValueMatches = false;
                }

                String value = args.words.get( 0 ).getValue();
                if( !value.equals( parameterValue ) ) {
                    valueMatches = false;
                }

                if( !formattedValueMatches && !valueMatches ) {
                    continue parameters;
                }
            }

            // on this point either all formatted values match or all values match (or both)
            argumentNames.add( paramName );
            paramNames.add( paramName );
            continue arguments;
        }

        argumentNames.add( null );
    }

    Set<String> usedNames = Sets.newHashSet();
    for( int iArg = 0; iArg < joinedArgs.get( 0 ).size(); iArg++ ) {
        String name = argumentNames.get( iArg );
        if( name == null || paramNames.count( name ) > 1 ) {
            String origName = getArgumentName( joinedArgs, iArg );
            name = findFreeName( usedNames, origName );
            argumentNames.set( iArg, name );
        }
        usedNames.add( name );

    }

    return argumentNames;
}
 
Example #18
Source File: SortedMultisetJsonDeserializer.java    From gwt-jackson with Apache License 2.0 4 votes vote down vote up
@Override
protected SortedMultiset<T> newCollection() {
    return TreeMultiset.create();
}
 
Example #19
Source File: TreeMultisetJsonDeserializer.java    From gwt-jackson with Apache License 2.0 4 votes vote down vote up
@Override
protected TreeMultiset<T> newCollection() {
    return TreeMultiset.create();
}
 
Example #20
Source File: GuavaConfiguration.java    From gwt-jackson with Apache License 2.0 4 votes vote down vote up
@Override
protected void configure() {
    type( Optional.class ).serializer( OptionalJsonSerializer.class ).deserializer( OptionalJsonDeserializer.class );
    type( FluentIterable.class ).serializer( IterableJsonSerializer.class );

    // Immutable Collections
    type( ImmutableCollection.class ).serializer( CollectionJsonSerializer.class )
            .deserializer( ImmutableCollectionJsonDeserializer.class );
    type( ImmutableList.class ).serializer( CollectionJsonSerializer.class ).deserializer( ImmutableListJsonDeserializer.class );
    type( ImmutableSet.class ).serializer( CollectionJsonSerializer.class ).deserializer( ImmutableSetJsonDeserializer.class );
    type( ImmutableSortedSet.class ).serializer( CollectionJsonSerializer.class )
            .deserializer( ImmutableSortedSetJsonDeserializer.class );

    // Immutable Map
    type( ImmutableMap.class ).serializer( MapJsonSerializer.class ).deserializer( ImmutableMapJsonDeserializer.class );
    type( ImmutableSortedMap.class ).serializer( MapJsonSerializer.class ).deserializer( ImmutableSortedMapJsonDeserializer.class );

    // BiMap
    type( BiMap.class ).serializer( MapJsonSerializer.class ).deserializer( BiMapJsonDeserializer.class );
    type( ImmutableBiMap.class ).serializer( MapJsonSerializer.class ).deserializer( ImmutableBiMapJsonDeserializer.class );
    type( HashBiMap.class ).serializer( MapJsonSerializer.class ).deserializer( HashBiMapJsonDeserializer.class );
    type( EnumBiMap.class ).serializer( MapJsonSerializer.class ).deserializer( EnumBiMapJsonDeserializer.class );
    type( EnumHashBiMap.class ).serializer( MapJsonSerializer.class ).deserializer( EnumHashBiMapJsonDeserializer.class );

    // Multiset
    type( Multiset.class ).serializer( CollectionJsonSerializer.class ).deserializer( MultisetJsonDeserializer.class );
    type( HashMultiset.class ).serializer( CollectionJsonSerializer.class ).deserializer( HashMultisetJsonDeserializer.class );
    type( LinkedHashMultiset.class ).serializer( CollectionJsonSerializer.class )
            .deserializer( LinkedHashMultisetJsonDeserializer.class );
    type( SortedMultiset.class ).serializer( CollectionJsonSerializer.class ).deserializer( SortedMultisetJsonDeserializer.class );
    type( TreeMultiset.class ).serializer( CollectionJsonSerializer.class ).deserializer( TreeMultisetJsonDeserializer.class );
    type( ImmutableMultiset.class ).serializer( CollectionJsonSerializer.class )
            .deserializer( ImmutableMultisetJsonDeserializer.class );
    type( EnumMultiset.class ).serializer( CollectionJsonSerializer.class ).deserializer( EnumMultisetJsonDeserializer.class );

    // Multimap
    type( Multimap.class ).serializer( MultimapJsonSerializer.class ).deserializer( MultimapJsonDeserializer.class );

    type( ImmutableMultimap.class ).serializer( MultimapJsonSerializer.class ).deserializer( ImmutableMultimapJsonDeserializer.class );
    type( ImmutableSetMultimap.class ).serializer( MultimapJsonSerializer.class )
            .deserializer( ImmutableSetMultimapJsonDeserializer.class );
    type( ImmutableListMultimap.class ).serializer( MultimapJsonSerializer.class )
            .deserializer( ImmutableListMultimapJsonDeserializer.class );

    type( SetMultimap.class ).serializer( MultimapJsonSerializer.class ).deserializer( SetMultimapJsonDeserializer.class );
    type( HashMultimap.class ).serializer( MultimapJsonSerializer.class ).deserializer( HashMultimapJsonDeserializer.class );
    type( LinkedHashMultimap.class ).serializer( MultimapJsonSerializer.class )
            .deserializer( LinkedHashMultimapJsonDeserializer.class );
    type( SortedSetMultimap.class ).serializer( MultimapJsonSerializer.class ).deserializer( SortedSetMultimapJsonDeserializer.class );
    type( TreeMultimap.class ).serializer( MultimapJsonSerializer.class ).deserializer( TreeMultimapJsonDeserializer.class );

    type( ListMultimap.class ).serializer( MultimapJsonSerializer.class ).deserializer( ListMultimapJsonDeserializer.class );
    type( ArrayListMultimap.class ).serializer( MultimapJsonSerializer.class ).deserializer( ArrayListMultimapJsonDeserializer.class );
    type( LinkedListMultimap.class ).serializer( MultimapJsonSerializer.class )
            .deserializer( LinkedListMultimapJsonDeserializer.class );
}
 
Example #21
Source File: QuorumConfiguration.java    From c5-replicator with Apache License 2.0 4 votes vote down vote up
private static long getGreatestIndexCommittedByMajority(Set<Long> peers, Map<Long, Long> peersLastAckedIndex) {
  SortedMultiset<Long> committedIndexes = TreeMultiset.create();
  committedIndexes.addAll(peers.stream().map(peerId
      -> peersLastAckedIndex.getOrDefault(peerId, 0L)).collect(Collectors.toList()));
  return Iterables.get(committedIndexes.descendingMultiset(), calculateNumericalMajority(peers.size()) - 1);
}
 
Example #22
Source File: TreeMultisetComparatorTest.java    From eagle with Apache License 2.0 4 votes vote down vote up
/**
 * if 2 different events have the same timestamp, and comparator return 0 when timestamp is same,
 * when they are added into TreeMultiset, the second event will be replaced by the first event
 */
@Test
public void testComparator() {
    TreeMultiset<PartitionedEvent> set = TreeMultiset.create(PartitionedEventTimeOrderingComparator.INSTANCE);

    // construct PartitionEvent1
    PartitionedEvent event1 = new PartitionedEvent();
    StreamPartition sp = new StreamPartition();
    sp.setColumns(Arrays.asList("host"));
    sp.setSortSpec(null);
    sp.setStreamId("testStreamId");
    sp.setType(StreamPartition.Type.GROUPBY);
    event1.setPartition(sp);
    event1.setPartitionKey(1000);
    StreamEvent e1 = new StreamEvent();
    e1.setData(new Object[] {18.4});
    e1.setStreamId("testStreamId");
    e1.setTimestamp(1462909984000L);
    event1.setEvent(e1);

    // construct PartitionEvent2 with same timestamp but different value
    PartitionedEvent event2 = new PartitionedEvent();
    event2.setPartition(sp);
    event2.setPartitionKey(1000);
    StreamEvent e2 = new StreamEvent();
    e2.setData(new Object[] {16.3});
    e2.setStreamId("testStreamId");
    e2.setTimestamp(1462909984000L);
    event2.setEvent(e2);

    // construct PartitionEvent2 with same timestamp but different value
    PartitionedEvent event3 = new PartitionedEvent();
    event3.setPartition(sp);
    event3.setPartitionKey(1000);
    StreamEvent e3 = new StreamEvent();
    e3.setData(new Object[] {14.3});
    e3.setStreamId("testStreamId");
    e3.setTimestamp(1462909984001L);
    event3.setEvent(e3);

    PartitionedEvent event4 = new PartitionedEvent();
    event4.setPartition(sp);
    event4.setPartitionKey(1000);
    StreamEvent e4 = new StreamEvent();
    e4.setData(new Object[] {14.3});
    e4.setStreamId("testStreamId");
    e4.setTimestamp(1462909984001L);
    event4.setEvent(e4);

    Assert.assertNotEquals(event2, event3);
    Assert.assertEquals(event3, event4);

    // check content in set
    set.add(event1);
    set.add(event2);
    set.add(event3);
    set.add(event4);
    Assert.assertEquals(4, set.size());
    set.forEach(System.out::println);


    Assert.assertEquals(-1, PartitionedEventTimeOrderingComparator.INSTANCE.compare(event1, event2));
    Assert.assertEquals(-1, PartitionedEventTimeOrderingComparator.INSTANCE.compare(event1, event3));
    Assert.assertEquals(-1, PartitionedEventTimeOrderingComparator.INSTANCE.compare(event2, event3));
    Assert.assertEquals(0, PartitionedEventTimeOrderingComparator.INSTANCE.compare(event3, event4));

    Iterator<PartitionedEvent> it = set.iterator();
    Assert.assertEquals(16.3, it.next().getData()[0]);
    Assert.assertEquals(18.4, it.next().getData()[0]);
    Assert.assertEquals(14.3, it.next().getData()[0]);
    Assert.assertEquals(14.3, it.next().getData()[0]);
}
 
Example #23
Source File: TreeMultisetDeserializer.java    From jackson-datatypes-collections with Apache License 2.0 4 votes vote down vote up
@Override
public GuavaCollectionDeserializer<TreeMultiset<Object>> withResolved(JsonDeserializer<?> valueDeser, TypeDeserializer typeDeser,
        NullValueProvider nuller, Boolean unwrapSingle) {
    return new TreeMultisetDeserializer(_containerType,
            valueDeser, typeDeser, nuller, unwrapSingle);
}
 
Example #24
Source File: TaskConcurrencyAnalyzer.java    From tez with Apache License 2.0 4 votes vote down vote up
@Override
public void analyze(DagInfo dagInfo) throws TezException {

  //For each vertex find the concurrent tasks running at any point
  for (VertexInfo vertexInfo : dagInfo.getVertices()) {
    List<TaskAttemptInfo> taskAttempts =
        Lists.newLinkedList(vertexInfo.getTaskAttempts(true, null));

    String vertexName = vertexInfo.getVertexName();

    /**
     * - Get sorted multi-set of timestamps (S1, S2,...E1, E2..). Possible to have multiple
     * tasks starting/ending at same time.
     * - Walk through the set
     * - Increment concurrent tasks when start event is encountered
     * - Decrement concurrent tasks when start event is encountered
     */
    TreeMultiset<TimeInfo> timeInfoSet = TreeMultiset.create(new Comparator<TimeInfo>() {
      @Override public int compare(TimeInfo o1, TimeInfo o2) {
        if (o1.timestamp < o2.timestamp) {
          return -1;
        }

        if (o1.timestamp > o2.timestamp) {
          return 1;
        }

        if (o1.timestamp == o2.timestamp) {
          //check event type
          if (o1.eventType.equals(o2.eventType)) {
            return 0;
          }

          if (o1.eventType.equals(EventType.START)
              && o2.eventType.equals(EventType.FINISH)) {
            return -1;
          } else {
            return 1;
          }
        }
        return 0;
      }
    });

    for (TaskAttemptInfo attemptInfo : taskAttempts) {
      TimeInfo startTimeInfo = new TimeInfo(EventType.START, attemptInfo.getStartTime());
      TimeInfo stopTimeInfo = new TimeInfo(EventType.FINISH, attemptInfo.getFinishTime());

      timeInfoSet.add(startTimeInfo);
      timeInfoSet.add(stopTimeInfo);
    }

    //Compute concurrent tasks in the list now.
    int concurrentTasks = 0;
    for(TimeInfo timeInfo : timeInfoSet.elementSet()) {
      switch (timeInfo.eventType) {
      case START:
        concurrentTasks += timeInfoSet.count(timeInfo);
        break;
      case FINISH:
        concurrentTasks -= timeInfoSet.count(timeInfo);
        break;
      default:
        break;
      }
      timeInfo.concurrentTasks = concurrentTasks;
      addToResult(vertexName, timeInfo.timestamp, timeInfo.concurrentTasks);
    }
  }
}
 
Example #25
Source File: KryoPersistence.java    From recheck with GNU Affero General Public License v3.0 3 votes vote down vote up
private static Kryo createKryo() {
	final Kryo kryo = new Kryo();

	kryo.setInstantiatorStrategy( new Kryo.DefaultInstantiatorStrategy( new StdInstantiatorStrategy() ) );

	final Registration registration = kryo.getRegistration( TreeMultiset.class );
	registration.setInstantiator( TreeMultiset::create );

	UnmodifiableCollectionsSerializer.registerSerializers( kryo );

	return kryo;
}
 
Example #26
Source File: StreamSortedWindowOnHeap.java    From eagle with Apache License 2.0 2 votes vote down vote up
/**
 * @param start  start time.
 * @param end    end time.
 * @param margin margin time.
 */
public StreamSortedWindowOnHeap(long start, long end, long margin, Comparator<PartitionedEvent> comparator) {
    super(start, end, margin);
    treeMultisetCache = TreeMultiset.create(comparator);
}