com.google.common.collect.EnumHashBiMap Java Examples

The following examples show how to use com.google.common.collect.EnumHashBiMap. 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: GuavaEnumHashBiMapTest.java    From java_in_examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    ENGLISH_WORD[] englishWords = ENGLISH_WORD.values();
    String[] russianWords = {"jeden", "dwa", "trzy", "kula", "snieg"};

    // Create Multiset
    BiMap<ENGLISH_WORD, String> biMap = EnumHashBiMap.create(ENGLISH_WORD.class);
    // Create English-Polish dictionary
    int i = 0;
    for(ENGLISH_WORD englishWord: englishWords) {
        biMap.put(englishWord, russianWords[i]);
        i++;
    }

    // Print count words
    System.out.println(biMap); // print {ONE=jeden, TWO=dwa, THREE=trzy, BALL=kula, SNOW=snieg}
    // Print all unique words
    System.out.println(biMap.keySet());    // print [ONE, TWO, THREE, BALL, SNOW]
    System.out.println(biMap.values());    // print [jeden, dwa, trzy, kula, snieg]

    // Print translate by words
    System.out.println("one = " + biMap.get(ENGLISH_WORD.ONE));    // print one = jeden
    System.out.println("two = " + biMap.get(ENGLISH_WORD.TWO));    // print two = dwa
    System.out.println("kula = " + biMap.inverse().get("kula"));    // print kula = BALL
    System.out.println("snieg = " + biMap.inverse().get("snieg"));    // print snieg = SNOW
    System.out.println("empty = " + biMap.get("empty"));    // print empty = null

    // Print count word's pair
    System.out.println(biMap.size());    //print 5

}
 
Example #2
Source File: GuavaEnumHashBiMapTest.java    From java_in_examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    ENGLISH_WORD[] englishWords = ENGLISH_WORD.values();
    String[] russianWords = {"jeden", "dwa", "trzy", "kula", "snieg"};

    // Создаем Multiset
    BiMap<ENGLISH_WORD, String> biMap = EnumHashBiMap.create(ENGLISH_WORD.class);
    // создаем англо-польский словарь
    int i = 0;
    for(ENGLISH_WORD englishWord: englishWords) {
        biMap.put(englishWord, russianWords[i]);
        i++;
    }

    // Выводим кол-вом вхождений слов
    System.out.println(biMap); // напечатает {ONE=один, TWO=dwa, THREE=trzy, BALL=kula, SNOW=snieg}
    // Выводим все уникальные слова
    System.out.println(biMap.keySet());    // напечатает [ONE, TWO, THREE, BALL, SNOW]
    System.out.println(biMap.values());    // напечатает [jeden, dwa, trzy, kula, snieg]

    // Выводим перевод по каждому слову
    System.out.println("one = " + biMap.get(ENGLISH_WORD.ONE));    // напечатает one = jeden
    System.out.println("two = " + biMap.get(ENGLISH_WORD.TWO));    // напечатает two = dwa
    System.out.println("kula = " + biMap.inverse().get("kula"));    // напечатает kula = BALL
    System.out.println("snieg = " + biMap.inverse().get("snieg"));    // напечатает snieg = SNOW
    System.out.println("empty = " + biMap.get("empty"));    // напечатает empty = null

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

}
 
Example #3
Source File: BiMapGwtTest.java    From gwt-jackson with Apache License 2.0 5 votes vote down vote up
public void testSerialization() {
    BeanWithBiMapTypes bean = new BeanWithBiMapTypes();

    bean.hashBiMap = HashBiMap.create();
    // only one value since it is not sorted
    bean.hashBiMap.put( "one", 1 );

    bean.biMap = bean.hashBiMap;

    bean.enumHashBiMap = EnumHashBiMap.create( AlphaEnum.class );
    bean.enumHashBiMap.put( AlphaEnum.A, 1 );
    bean.enumHashBiMap.put( AlphaEnum.D, 4 );
    bean.enumHashBiMap.put( AlphaEnum.C, 3 );
    bean.enumHashBiMap.put( AlphaEnum.B, 2 );

    bean.enumBiMap = EnumBiMap.create( AlphaEnum.class, NumericEnum.class );
    bean.enumBiMap.put( AlphaEnum.A, NumericEnum.ONE );
    bean.enumBiMap.put( AlphaEnum.D, NumericEnum.FOUR );
    bean.enumBiMap.put( AlphaEnum.C, NumericEnum.THREE );
    bean.enumBiMap.put( AlphaEnum.B, NumericEnum.TWO );

    String expected = "{" +
            "\"biMap\":{\"one\":1}," +
            "\"hashBiMap\":{\"one\":1}," +
            "\"enumHashBiMap\":{\"A\":1,\"B\":2,\"C\":3,\"D\":4}," +
            "\"enumBiMap\":{\"A\":\"ONE\",\"B\":\"TWO\",\"C\":\"THREE\",\"D\":\"FOUR\"}" +
            "}";

    assertEquals( expected, BeanWithBiMapTypesMapper.INSTANCE.write( bean ) );
}
 
Example #4
Source File: BiMapGwtTest.java    From gwt-jackson with Apache License 2.0 5 votes vote down vote up
public void testDeserialization() {
    String input = "{" +
            "\"biMap\":{\"one\":1,\"two\":2,\"three\":3,\"four\":4}," +
            "\"hashBiMap\":{\"one\":1,\"two\":2,\"three\":3,\"four\":4}," +
            "\"enumHashBiMap\":{\"A\":1,\"B\":2,\"C\":3,\"D\":4}," +
            "\"enumBiMap\":{\"A\":\"ONE\",\"B\":\"TWO\",\"C\":\"THREE\",\"D\":\"FOUR\"}" +
            "}";

    BeanWithBiMapTypes result = BeanWithBiMapTypesMapper.INSTANCE.read( input );
    assertNotNull( result );

    HashBiMap<String, Integer> expectedHashBiMap = HashBiMap.create();
    expectedHashBiMap.put( "one", 1 );
    expectedHashBiMap.put( "two", 2 );
    expectedHashBiMap.put( "three", 3 );
    expectedHashBiMap.put( "four", 4 );

    assertNotNull( result.biMap );
    assertEquals( expectedHashBiMap, result.biMap );
    assertEquals( expectedHashBiMap, result.hashBiMap );

    EnumHashBiMap<AlphaEnum, Integer> expectedEnumHashBiMap = EnumHashBiMap.create( AlphaEnum.class );
    expectedEnumHashBiMap.put( AlphaEnum.A, 1 );
    expectedEnumHashBiMap.put( AlphaEnum.D, 4 );
    expectedEnumHashBiMap.put( AlphaEnum.C, 3 );
    expectedEnumHashBiMap.put( AlphaEnum.B, 2 );
    assertEquals( expectedEnumHashBiMap, result.enumHashBiMap );

    EnumBiMap<AlphaEnum, NumericEnum> expectedEnumBiMap = EnumBiMap.create( AlphaEnum.class, NumericEnum.class );
    expectedEnumBiMap.put( AlphaEnum.A, NumericEnum.ONE );
    expectedEnumBiMap.put( AlphaEnum.D, NumericEnum.FOUR );
    expectedEnumBiMap.put( AlphaEnum.C, NumericEnum.THREE );
    expectedEnumBiMap.put( AlphaEnum.B, NumericEnum.TWO );
    assertEquals( expectedEnumBiMap, result.enumBiMap );
}
 
Example #5
Source File: GuavaBiMapUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenUsingEnumAsKeyInMap_replacesAlreadyPresent() {
    final BiMap<Operation, String> operationStringBiMap = EnumHashBiMap.create(Operation.class);

    operationStringBiMap.put(Operation.ADD, "Add");
    operationStringBiMap.put(Operation.SUBTRACT, "Subtract");
    operationStringBiMap.put(Operation.MULTIPLY, "Multiply");
    operationStringBiMap.put(Operation.DIVIDE, "Divide");

    assertEquals("Divide", operationStringBiMap.get(Operation.DIVIDE));
}
 
Example #6
Source File: EnumHashBiMapJsonDeserializer.java    From gwt-jackson with Apache License 2.0 4 votes vote down vote up
@Override
protected EnumHashBiMap<E, V> newMap() {
    return EnumHashBiMap.create( enumClass );
}
 
Example #7
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 );
}