Skip to content

Instantly share code, notes, and snippets.

@nulldatamap
Created July 22, 2014 22:26
Show Gist options
  • Select an option

  • Save nulldatamap/5593a26f988aba0ae92a to your computer and use it in GitHub Desktop.

Select an option

Save nulldatamap/5593a26f988aba0ae92a to your computer and use it in GitHub Desktop.

This template:

/*

enums
${name}( ${id}, "${msg}" ),?{if not last:$n  }
.

case_name_from_id
?{if not first:    }case ${id}:
      return ${name};?{if not last:$n}
.

@BODY
package io.nulldata.net;

/**
 * An error either sent from the server, or one caused by the ProtocolDriver.
 * @author NullData
 *
 $/
public enum NetError {
  
  // Protocol errors
  %{enums}
  // Runtime errors
  IOError( -1, "Fatal IO error occured" );
  
  private short id;
  private String msg;
  
  NetError( int id, String msg ) {
    this.id = ( short )id;
    this.msg = msg;
  }

  /**
   * Get the error message.
   * @return The error message.
   $/
  public String msg() {
    return this.msg;
  }
  
  /**
   * The the id of the error.
   * @return The id of the error.
   $/
  public short id() {
    return this.id;
  }
  
  
  /**
   * Creates a custom error with the specified message.
   * @param msg The error message for the new error.
   * @return A custom error with the specified message.
   $/
  public static NetError from_msg( String msg ) {
    NetError err = IOError;
    err.msg = msg;
    return err;
  }
  
  /**
   * Retrieves an error based on it's id. If no error with the specified id exists, null will be 
   * returned.
   * @param id The id of the given error.
   * @return An error with the specified id or null if no such error exists.
   $/
  public static NetError from_id( int id ) {
    switch( id ) {
    %{case_name_from_id}
    case -1:
      return IOError;
    }
    return null;
  }
  
}
*/

Given this data ( It's a Python data structure ):

[
  { "msg": "Disconnected from host."
  , "alts": { "msg": "The connection is either to unstable or slow for use."
            , "name": "_timeout"}
  , "name": "Disconnected"
  , "score_name": "disconnected"
  , "id": "0x00" }

, { "msg": "Invalid packet sent."
  , "alts": []
  , "name": "InvalidPacket"
  , "score_name": "invalid_packet"
  , "id": "0x10" }

, { "msg": "Outdated client."
  , "alts": []
  , "name": "OutdatedClient"
  , "score_name": "outdated_client"
  , "id": "0x11" }

, { "msg": "Outdated server."
  , "alts": []
  , "name": "OutdatedServer"
  , "score_name": "outdated_server"
  , "id": "0x12" }

, { "msg": "Bad token supplied."
  , "alts": []
  , "name": "BadToken"
  , "score_name": "bad_token"
  , "id": "0x20" }
]

Which was parsed from:

Disconnected         | 0x00  | Disconnected from host. | _timeout | The connection is either to unstable or slow for use.
InvalidPacket        | 0x10  | Invalid packet sent.
OutdatedClient       | 0x11  | Outdated client.
OutdatedServer       | 0x12  | Outdated server.
BadToken             | 0x20  | Bad token supplied.

Produced this:

/*
  ... The template displated aboce ...
*/
package io.nulldata.net;

/**
 * An error either sent from the server, or one caused by the ProtocolDriver.
 * @author NullData
 *
 */
public enum NetError {
  
  // Protocol errors
  Disconnected( 0x00, "Disconnected from host." ),
  InvalidPacket( 0x10, "Invalid packet sent." ),
  OutdatedClient( 0x11, "Outdated client." ),
  OutdatedServer( 0x12, "Outdated server." ),
  BadToken( 0x20, "Bad token supplied." ),
  // Runtime errors
  IOError( -1, "Fatal IO error occured" );
  
  private short id;
  private String msg;
  
  NetError( int id, String msg ) {
    this.id = ( short )id;
    this.msg = msg;
  }

  /**
   * Get the error message.
   * @return The error message.
   */
  public String msg() {
    return this.msg;
  }
  
  /**
   * The the id of the error.
   * @return The id of the error.
   */
  public short id() {
    return this.id;
  }
  
  
  /**
   * Creates a custom error with the specified message.
   * @param msg The error message for the new error.
   * @return A custom error with the specified message.
   */
  public static NetError from_msg( String msg ) {
    NetError err = IOError;
    err.msg = msg;
    return err;
  }
  
  /**
   * Retrieves an error based on it's id. If no error with the specified id exists, null will be 
   * returned.
   * @param id The id of the given error.
   * @return An error with the specified id or null if no such error exists.
   */
  public static NetError from_id( int id ) {
    switch( id ) {
    case 0x00:
      return Disconnected;
    case 0x10:
      return InvalidPacket;
    case 0x11:
      return OutdatedClient;
    case 0x12:
      return OutdatedServer;
    case 0x20:
      return BadToken;
    case -1:
      return IOError;
    }
    return null;
  }
  
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment