Home | FAQ | Contact me

Java 5 Enumerations, part 4: Nuts and Bolts

Consider this class definition for this discussion. You'll refer back to it (sometimes by line number).

01.public enum ReferenceType
02.{
03.  SOURCE          (  0true, null, null ),
04.  MEMORY          (  1, false, null, null ),
05.  PHOTO           (  2, false, null, null ),
06.  STORY           (  3, false, null, null ),
07.  OBITUARY        (  4, false, null, null ),
08.  BOOK            (  5true, null, null ),
09.  DISCUSSION      (  6, false, null, null ),
10.  DOCUMENT        (  7, false, null, null ),
11.  EXTERNAL_PERSONA(  8, false,
12.                     new ArrayList< Permission >( Arrays.asList( Permission.AddDataPermission, Permission.AddPermission ) ),
13.                     new ArrayList< Permission >( Arrays.asList( Permission.AddDataPermission, Permission.AddPermission ) ) );
14. 
15.  private final int                refCode;
16.  private final boolean            isSource;
17.  private final List< Permission > readPerm;
18.  private final List< Permission > writePerm;
19. 
20.  private ReferenceType( int sourceReferenceTypeCode,
21.                         boolean isSource,
22.                         List< Permission > readPerm,
23.                         List< Permission > writePerm )
24.  {
25.    this.refCode   = sourceReferenceTypeCode;
26.    this.isSource  = isSource;
27.    this.readPerm  = readPerm;
28.    this.writePerm = writePerm;
29.  }
30. 
31.  public static List< Permission > getReadPerm( ReferenceType type )
32.  {
33.    return type.readPerm;
34.  }
35. 
36.  public static List< Permission > getWritePerm( ReferenceType type )
37.  {
38.    return type.writePerm;
39.  }
40. 
41.  public static boolean readPermissionHolderForType( Permission permission, ReferenceType type )
42.  {
43.    return type.readPerm.contains( permission );
44.  }
45. 
46.  public static boolean writePermissionHolderForType( Permission permission, ReferenceType type )
47.  {
48.    return type.writePerm.contains( permission );
49.  }
50.  ...
51.}

And here's a JUnit test...

01.public class TestReferenceType
02.{
03.  @Test
04.  public void testGetReadPerm()
05.  {
06.    List< Permission > holders = ReferenceType.getReadPerm( ReferenceType.ATTACHED_CMIS_PERSONA );
07.    Assert.assertNotNull( holders );
08. 
09.    System.out.println( "Permissions for " + ReferenceType.ATTACHED_CMIS_PERSONA );
10.    for( Permission p : holders )
11.      System.out.println( "  " + p );
12.  }
13. 
14.  @Test
15.  public void testCheckReadPermission_true()
16.  {
17.    boolean hasPermission = ReferenceType.readPermissionHolderForType( Permission.AddDataPermission,
18.                                                        ReferenceType.ATTACHED_CMIS_PERSONA );
19.    Assert.assertTrue( hasPermission );
20.  }
21. 
22.  @Test
23.  public void testCheckReadPermission_false()
24.  {
25.    boolean hasPermission = ReferenceType.readPermissionHolderForType( Permission.ActAsBulkMergePermission,
26.                                                        ReferenceType.ATTACHED_CMIS_PERSONA );
27.    Assert.assertFalse( hasPermission );
28.  }
29. 
30.  @Test
31.  public void testCheckWritePermission_true()
32.  {
33.    boolean hasPermission = ReferenceType.writePermissionHolderForType( Permission.AddDataPermission,
34.                                                         ReferenceType.ATTACHED_CMIS_PERSONA );
35.    Assert.assertTrue( hasPermission );
36.  }
37. 
38.  @Test
39.  public void testCheckWritePermission_false()
40.  {
41.    boolean hasPermission = ReferenceType.writePermissionHolderForType( Permission.ActAsBulkMergePermission,
42.                                                         ReferenceType.ATTACHED_CMIS_PERSONA );
43.    Assert.assertFalse( hasPermission );
44.  }
45.}

Here's another class to look at...

01.package com.etretatlogiciels.utilities;
02. 
03.import java.util.ArrayList;
04.import java.util.List;
05. 
06.public enum CassandraType
07.{
08.  c_text,             // UTF-8 encoded string
09.  c_ascii,            // US_ASCII 7-bit
10.  c_varchar,          // UTF-8 encoded string
11. 
12.  c_int,              // 32-bit signed
13.  c_bigint,           // 64-bit signed
14.  c_smallint,         // 2-byte signed
15.  c_tinyint,          // 1-byte signed
16.  c_varint,           // arbitrary-precision
17. 
18.  c_decimal,          // variable-precision
19.  c_float,            // 32-bit IEEEE-754
20.  c_double,           // 64-bit IEEEE-754
21. 
22.  c_boolean,          // true/false
23.  c_counter,          // distributed, 64-bit
24. 
25.  c_date,             // 32-bit day since Epoch
26.  c_time,             // 64-bit nanoseconds since midnight
27.  c_timestamp,        // 8 bytes since Epoch; date and time with millisecond precision
28.  c_timeuuid,         // ?
29. 
30.  c_inet,             // IPv4 or IPv6
31.  c_tuple,            // 2-3 fields
32.  c_uuid,             // 128-bit globally unique identifier
33. 
34.  c_list,             // collection of 1+ elements (performance impact)
35.  c_map,              // JSON-style array of literals
36.  c_set,              // collection of 1+ literal elements
37. 
38.  c_blob,             // arbitrary bytes (no validation), in hexadecimal
39.  c_frozen,           // multiple types in single value, treated as blob
40.  ;
41. 
42.  /**
43.   * Useful to determine whether potential enum type,
44.   * in string form, is a Cassandra type.
45.   */
46.  public static boolean contains( String type )
47.  {
48.    try
49.    {
50.      CassandraType.valueOf( type );
51.      return true;
52.    }
53.    catch( IllegalArgumentException e )
54.    {
55.      return false;
56.    }
57.  }
58. 
59.  /**
60.   * Useful to determine whether potential type,
61.   * in string form, is a Cassandra type.
62.   */
63.  public static CassandraType stringToCassandraType( String string )
64.  {
65.    try
66.    {
67.      return CassandraType.valueOf( "c_" + string );
68.    }
69.    catch( IllegalArgumentException e )
70.    {
71.      return null;
72.    }
73.  }
74. 
75.  /**
76.   * Useful to return a list of Cassandra types.
77.   */
78.  public static List< String > getCassandraTypes()
79.  {
80.    List< String > list = new ArrayList<>( CassandraType.values().length );
81. 
82.    for( CassandraType type : CassandraType.values() )
83.      list.add( type.name() );
84. 
85.    return list;
86.  }
87.}

And here's a JUnit test...

01.package com.etretatlogiciels.utilities;
02. 
03.import java.util.List;
04. 
05.import org.junit.Before;
06.import org.junit.Rule;
07.import org.junit.Test;
08. 
09.import static org.junit.Assert.assertTrue;
10.import static org.junit.Assert.assertFalse;
11.import static org.junit.Assert.assertNotNull;
12. 
13.public class CassandraTypeTest
14.{
15.  @Test
16.  public void testStringToCassandraType() throws Exception
17.  {
18.    assertNotNull( CassandraType.stringToCassandraType( "text" ) );
19.    assertNotNull( CassandraType.stringToCassandraType( "blob" ) );
20.    assertNotNull( CassandraType.stringToCassandraType( "frozen" ) );
21. 
22.    CassandraType type = CassandraType.stringToCassandraType( "boolean" );
23.    assertTrue( type == CassandraType.c_boolean );
24.  }
25. 
26.  @Test
27.  public void testList()
28.  {
29.    List< String > types = CassandraType.getCassandraTypes();
30.    String values = String.join(",\n", types);
31. 
32.    System.out.println( values );
33.  }
34.}