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).

public enum ReferenceType
{
  SOURCE          (  0,  true, null, null ),
  MEMORY          (  1, false, null, null ),
  PHOTO           (  2, false, null, null ),
  STORY           (  3, false, null, null ),
  OBITUARY        (  4, false, null, null ),
  BOOK            (  5,  true, null, null ),
  DISCUSSION      (  6, false, null, null ),
  DOCUMENT        (  7, false, null, null ),
  EXTERNAL_PERSONA(  8, false,
                     new ArrayList< Permission >( Arrays.asList( Permission.AddDataPermission, Permission.AddPermission ) ),
                     new ArrayList< Permission >( Arrays.asList( Permission.AddDataPermission, Permission.AddPermission ) ) );

  private final int                refCode;
  private final boolean            isSource;
  private final List< Permission > readPerm;
  private final List< Permission > writePerm;

  private ReferenceType( int sourceReferenceTypeCode,
                         boolean isSource,
                         List< Permission > readPerm,
                         List< Permission > writePerm )
  {
    this.refCode   = sourceReferenceTypeCode;
    this.isSource  = isSource;
    this.readPerm  = readPerm;
    this.writePerm = writePerm;
  }

  public static List< Permission > getReadPerm( ReferenceType type )
  {
    return type.readPerm;
  }

  public static List< Permission > getWritePerm( ReferenceType type )
  {
    return type.writePerm;
  }

  public static boolean readPermissionHolderForType( Permission permission, ReferenceType type )
  {
    return type.readPerm.contains( permission );
  }

  public static boolean writePermissionHolderForType( Permission permission, ReferenceType type )
  {
    return type.writePerm.contains( permission );
  }
  ...
}

And here's a JUnit test...

public class TestReferenceType
{
  @Test
  public void testGetReadPerm()
  {
    List< Permission > holders = ReferenceType.getReadPerm( ReferenceType.ATTACHED_CMIS_PERSONA );
    Assert.assertNotNull( holders );

    System.out.println( "Permissions for " + ReferenceType.ATTACHED_CMIS_PERSONA );
    for( Permission p : holders )
      System.out.println( "  " + p );
  }

  @Test
  public void testCheckReadPermission_true()
  {
    boolean hasPermission = ReferenceType.readPermissionHolderForType( Permission.AddDataPermission,
                                                        ReferenceType.ATTACHED_CMIS_PERSONA );
    Assert.assertTrue( hasPermission );
  }

  @Test
  public void testCheckReadPermission_false()
  {
    boolean hasPermission = ReferenceType.readPermissionHolderForType( Permission.ActAsBulkMergePermission,
                                                        ReferenceType.ATTACHED_CMIS_PERSONA );
    Assert.assertFalse( hasPermission );
  }

  @Test
  public void testCheckWritePermission_true()
  {
    boolean hasPermission = ReferenceType.writePermissionHolderForType( Permission.AddDataPermission,
                                                         ReferenceType.ATTACHED_CMIS_PERSONA );
    Assert.assertTrue( hasPermission );
  }

  @Test
  public void testCheckWritePermission_false()
  {
    boolean hasPermission = ReferenceType.writePermissionHolderForType( Permission.ActAsBulkMergePermission,
                                                         ReferenceType.ATTACHED_CMIS_PERSONA );
    Assert.assertFalse( hasPermission );
  }
}

Here's another class to look at...

package com.etretatlogiciels.utilities;

import java.util.ArrayList;
import java.util.List;

public enum CassandraType
{
  c_text,             // UTF-8 encoded string
  c_ascii,            // US_ASCII 7-bit
  c_varchar,          // UTF-8 encoded string

  c_int,              // 32-bit signed
  c_bigint,           // 64-bit signed
  c_smallint,         // 2-byte signed
  c_tinyint,          // 1-byte signed
  c_varint,           // arbitrary-precision

  c_decimal,          // variable-precision
  c_float,            // 32-bit IEEEE-754
  c_double,           // 64-bit IEEEE-754

  c_boolean,          // true/false
  c_counter,          // distributed, 64-bit

  c_date,             // 32-bit day since Epoch
  c_time,             // 64-bit nanoseconds since midnight
  c_timestamp,        // 8 bytes since Epoch; date and time with millisecond precision
  c_timeuuid,         // ?

  c_inet,             // IPv4 or IPv6
  c_tuple,            // 2-3 fields
  c_uuid,             // 128-bit globally unique identifier

  c_list,             // collection of 1+ elements (performance impact)
  c_map,              // JSON-style array of literals
  c_set,              // collection of 1+ literal elements

  c_blob,             // arbitrary bytes (no validation), in hexadecimal
  c_frozen,           // multiple types in single value, treated as blob
  ;

  /**
   * Useful to determine whether potential enum type,
   * in string form, is a Cassandra type.
   */
  public static boolean contains( String type )
  {
    try
    {
      CassandraType.valueOf( type );
      return true;
    }
    catch( IllegalArgumentException e )
    {
      return false;
    }
  }

  /**
   * Useful to determine whether potential type,
   * in string form, is a Cassandra type.
   */
  public static CassandraType stringToCassandraType( String string )
  {
    try
    {
      return CassandraType.valueOf( "c_" + string );
    }
    catch( IllegalArgumentException e )
    {
      return null;
    }
  }

  /**
   * Useful to return a list of Cassandra types.
   */
  public static List< String > getCassandraTypes()
  {
    List< String > list = new ArrayList<>( CassandraType.values().length );

    for( CassandraType type : CassandraType.values() )
      list.add( type.name() );

    return list;
  }
}

And here's a JUnit test...

package com.etretatlogiciels.utilities;

import java.util.List;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;

public class CassandraTypeTest
{
  @Test
  public void testStringToCassandraType() throws Exception
  {
    assertNotNull( CassandraType.stringToCassandraType( "text" ) );
    assertNotNull( CassandraType.stringToCassandraType( "blob" ) );
    assertNotNull( CassandraType.stringToCassandraType( "frozen" ) );

    CassandraType type = CassandraType.stringToCassandraType( "boolean" );
    assertTrue( type == CassandraType.c_boolean );
  }

  @Test
  public void testList()
  {
    List< String > types = CassandraType.getCassandraTypes();
    String values = String.join(",\n", types);

    System.out.println( values );
  }
}