DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world


  • submit to reddit

Recent Snippets

                    //Create a Draft Appointment Request
String sender = "";
String recipient = "";

MailMessage message = new MailMessage(sender, recipient, "", "");

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
calendar.set(2012, Calendar.NOVEMBER, 1, 0, 0, 0);
Date startDate = calendar.getTime();
calendar.set(2012, Calendar.DECEMBER, 1);
Date endDate = calendar.getTime();

MailAddressCollection attendees = new MailAddressCollection();
attendees.add(new MailAddress("", "Attendee"));
WeeklyRecurrencePattern expected = new WeeklyRecurrencePattern(3);

Appointment app = new Appointment("Appointment Location", "Appointment Summary", "Appointment Description",
        startDate, endDate,
        new MailAddress("", "Organizer"), attendees, expected);

//Set the Appointment as Draft
app.setMethod(AppointmentMethodType.Publish);//.Method = AppointmentMethodType.Publish;
MapiMessage msg = MapiMessage.fromMailMessage(message);

// Save the appointment as draft."Draft.msg");

//Draft Appointment Creation from Text
String ical = "BEGIN:VCALENDAR\r\nMETHOD:PUBLISH\r\nPRODID:-//Aspose Ltd//iCalender Builder (v3.0)//EN\r\nVERSION:2.0\r\nBEGIN:VEVENT\r\nATTENDEE;\r\nDTSTART:20130220T171439\r\nDTEND:20130220T174439\r\nDTSTAMP:20130220T161439Z\r\nEND:VEVENT\r\nEND:VCALENDAR";

String sender = "";
String recipient = "";

MailMessage message = new MailMessage(sender, recipient, "", "");

AlternateView av = AlternateView.createAlternateViewFromString(ical, new ContentType("text/calendar"));


MapiMessage msg = MapiMessage.fromMailMessage(message);

// Save the appointment as draft."DraftAppointment.msg");

public class SwitcherDemo
	public static void main(String args[])
		Switcher generic = new Switcher()
				.addCase(1, "Integer")
				.addCase(1.1, "Double")
		System.out.println("Switch type "+generic.exec(1.1f));
		System.out.println("Switch type "+generic.exec("S"));
		System.out.println("Switch type "+generic.exec(1));
		System.out.println("Switch type "+generic.exec(1.1));
		System.out.println("Switch type "+generic.exec("SS"));

import ch.lambdaj.function.closure.Switcher;

import java.util.Arrays;
import java.util.List;

public class SwitcherDemo {

	public static void main(String args[])
		Employee fulltime = new Employee(JOB_TYPE.FULL_TIME);
		Employee partime = new Employee(JOB_TYPE.PART_TIME);
		Employee contract = new Employee(JOB_TYPE.CONTRACT);
		Employee intern = new Employee(JOB_TYPE.INTERN);
		List<Employee>  employeeList = Arrays.asList(fulltime,partime,contract,intern);

		Switcher<Salary> salarySwitcher = new Switcher<Salary>();
		salarySwitcher.addCase(JOB_TYPE.FULL_TIME, new Salary(100000));
		salarySwitcher.addCase(JOB_TYPE.PART_TIME, new Salary(50000));
		salarySwitcher.addCase(JOB_TYPE.CONTRACT, new Salary(120000));
		salarySwitcher.addCase(JOB_TYPE.INTERN, new Salary(45000));
		salarySwitcher.setDefault(new Salary(0));

		for(Employee e: employeeList)
                     Salary sal = salarySwitcher.exec(e.getJobtype());
			System.out.println("Salary "+e.getSalary().getSalary()+" Offered Job type "+e.getJobtype());


class Employee

	private final JOB_TYPE jobtype;
	private Salary salary;

	Employee(JOB_TYPE job_type)
	public JOB_TYPE getJobtype()
		return jobtype;

	Salary getSalary()
		return salary;

	void setSalary(Salary salary)
		this.salary = salary;
class Salary
	private final double sal;

	public Salary(double sal)
	public double getSalary()
		return sal;
                    #!/usr/bin/env ruby
# Author : Emad Elsaid (

# monkey batch, yup that a bad practice,
# but lets say that this is a proof of concept
# we'll open the object class and handle the 
# method missing situation, we'll claculate
# distance between the requested method and 
# all methods available in object, then the nearest
# method if distance doesn't exceed certain number
# then execute it.
class Object
  def method_missing(meth, *args, &block)
    threshold = 3
    all_meth = methods.sort
    all_meth.sort_by! do |m| 
      string_distance m, meth
    if string_distance(all_meth.first, meth) <= 3
      send all_meth.first, *args, &block

  # we'll calculate distance between 2 string by
  # getting number of characters in 1 and not in 2
  # and number chars in 2 not in 1, sum the two
  # differences and return that weight
  # less weight is more similar method
  def string_distance(str1, str2)
    one_way = str1.to_s.chars - str2.to_s.chars 
    the_other_way = str2.to_s.chars - str1.to_s.chars 
    one_way.size + the_other_way.size

# ## UseCase ?
# you can use `nil` instead of `nil?`
p "26512135".nil
# you can use `toi` and `tof` instead of `to_i` and `to_f`
p "12123".tof

# this way ruby will be more forgiving if you wrote
# method name with wrong character or less character or more 
# with 1 character, when you increate the `threshold` it'll
# be more forgiving :D, 
# happy coding.                
                    #!/usr/bin/env ruby
# Author : Emad Elsaid (

# it turns out that BBC website has a printable version of
# the top 40 UK singles chart, that made me jump of joy :D
require 'open-uri'

# get BBC singles chart printable version
page = open('').read
# result has data as table so we'll extract keys from TH tags
keys = page.scan(/<th>(.+)<\/th>/).map{ |k| k.first.downcase }
# extract cells from TD tags
cells = page.scan(/<td>(.*)<\/td>/).map{ |c| c.first }
# split cells to arrays each equal to keys
rows = cells.each_slice keys.size

# container to join data as Hash objects and push to it
data = []
# now iterate on each row and join keys with their
# respective values then convert them to arrays
rows.each do |row|
  data << Hash[ [keys, row].transpose ] # this is a good trick ;)

# show us what you got sir.
puts data
                    #!/usr/bin/env ruby
# Author : Emad Elsaid (
# Output example : 
# install ruby-graphviz then install graphviz from 
require 'graphviz' # gem install ruby-graphviz --no-document

# get all constants in namespace
# then convert them from symbols
# to their objects and select 
# the classes out of them, 
# you may include modules if you wish
classes = Class.constants
                      .map { |c| Class.const_get c }
                      .select { |c| c.is_a? Class }
g = :G, :type => :digraph )

# Create nodes
nodes = Hash[ {|c| [c, g.add_nodes( c.to_s )] }]
nodes.each do |klass,node|
    g.add_edges node, nodes[klass.superclass]
  rescue # do nothing

# Generate output image
g.output( :png => "/Users/blaze/Desktop/classes_graph.png" )
# you can output PDF with small modification
g.output( :pdf => "/Users/blaze/Desktop/classes_graph.pdf" )                
                    #!/usr/bin/env ruby
# Author : Emad Elsaid (
# **Usage** : 
# * `ruby evalin_comodor.rb <accesstoken>`
# this will get a long term access token from the short term one
# * `ruby evalin_comodor.rb`
# will start the script
require './evalin.rb' #
require 'koala' # gem install koala

# create a facebook app and get access token from here
# scopes required : 
# public_profile, basic_info, publish_checkins, status_update, photo_upload, video_upload, create_note, share_item, publish_stream, manage_notifications, publish_actions, user_friends

APP_ID      = 'xxxxxxxxxxxxxxxx'
APP_SECRET  = 'xxxxxxxxxxxxxxxx'

if ARGV.size > 0
  oauth =, APP_SECRET)
    new_access_info = oauth.exchange_access_token_info ARGV.first
    File.write 'token', new_access_info['access_token']
  puts 'New Access Token written'

# respond to comment with mention
def respond_to(graph, comment_id, post_id)

  comment = graph.get_object(comment_id)

  username = comment['from']['name']
  message = comment['message']
  puts "responding to : #{message}"

  # read comment parts languages and code
  groups = message.scan /#{$name}\s+([a-zA-Z+]+)(.+)/m
  return if groups.length != 1

  language = groups.first[0]
  code = groups.first[1].strip
  return unless output = evalin(code, language)

  # posting comment reply
  graph.put_object post_id, 'comments', {message: "#{username} \n#{output}"}


loop do
    # yes each time will read token
    # in case you started it then added access token
    # or refreshed it you won't need to restart it
    oauth_access_token ='token')
    $graph =

    # get your name
    $name = $graph.get_object('me')['name']

    # get notifications
    notifications = []
    page = $graph.get_connections('me','notifications')
      notifications += page
    end while page = page.next_page and notifications.length<=NOTIFICATIONS_LIMIT

    # Now lets head to parse and respond to them
    notifications.reverse_each do |n|
        if n['application'] and n['application']['name'] == 'Groups' and n['title'].include? 'mentioned you'

          ids = n['link'].scan /comment_id=([0-9]+)&/
          post_ids = n['link'].scan /permalink\/([0-9]+)\//
          if ids.size == 1 and post_ids.size == 1
            respond_to $graph, ids.first.first, post_ids.first.first 

        # ignore it

      # mark notification as read
      $graph.put_object(n['id'],'', {unread: false})
    puts "Notifications found : #{notifications.size}"

  # please rescue if something went wrong
  # thanks,
  rescue Exception => e
    puts "Error : #{e}"

  # wait for 60 seconds and try again
  sleep 60
                    #!/usr/bin/env ruby
# Author : Emad Elsaid (
# this script is a small practice in implementing 
# simple sorting algorithms in ruby, i converted
# the sorting algorithms from wikipedia pages

class Array
  # Insertion sort is a simple sorting algorithm that builds 
  # the final sorted array (or list) one item at a time. 
  # It is much less efficient on large lists than more advanced 
  # algorithms such as quicksort, heapsort, or merge sort.
  # **wikipedia**
  def insertion_sort!

    (1...size).each do |i|
      j = i
      while j > 0 and self[j-1] > self[j]
          self[j], self[j-1] = self[j-1], self[j]
          j = j - 1


  # selection sort is a sorting algorithm, specifically an in-place 
  # comparison sort. It has O(n2) time complexity, making it 
  # inefficient on large lists, and generally performs worse 
  # than the similar insertion sort. Selection sort is noted for 
  # its simplicity, and it has performance advantages over more 
  # complicated algorithms in certain situations, 
  # particularly where auxiliary memory is limited.
  # **wikipedia**
  def selection_sort!
    (0...size).each do |j|
      # find index of minimum element in the unsorted part 
      iMin = j
      (j+1...size).each do |i|
        iMin = i if self[i] < self[iMin]
      # then swap it
      self[j], self[iMin] = self[iMin], self[j]


# lets try our algorithms
x = (1..10).to_a.shuffle
p 'before sort : ', x
p 'after sort : ', x

p 'before sort : ', x
p 'after sort : ', x                
                    #!/usr/bin/env ruby
# Author : Emad Elsaid (
require 'pygmentize' # gem install pygmentize
require 'redcarpet' # gem install redcarpet

# consecutive lines of code
# should be highlighted using pygmentize
# and converter to markdown block simply
# using the markdown syntax for code
# used by github flavored markdown
class CodeBlock < Array

  def to_html
    Pygmentize.process join, :ruby

  def to_md
  alias_method :to_markdown, :to_md

# set of consecutive lines of comments
class CommentBlock < Array

  # will use this Redcarpet renderer to convert 
  # markdown to html
  @@renderer =, 
                            fenced_code_blocks: true,
                            disable_indented_code_blocks: true,
                            strikethrough: true,
                            superscript: true,
                            underline: true,
                            autolink: true )

  # use the renderer on the content
  def to_html
    @@renderer.render join("\n")

  # convert the block to markdown format
  # and also give it a full name alias
  def to_md
  alias_method :to_markdown, :to_md


# container class will have multiple blocks of 
# comments and code
class CodeConverter < Array

  # you should pass code when
  # initializing new instance
  # in order to parse it
  def initialize( code )
    parse code

  # add new line of code to the class
  # it start new code block if the last block is
  # not code block then adds the line of code
  # to the last block
  def add_code( line )
    self << unless last.is_a? CodeBlock
    last << line

  # add new comment line to the class
  # it adds new comment block if the last block is not 
  # comments block then add the comment line to the block
  def add_comment( line )
    self << unless last.is_a? CommentBlock
    last << line

  # parse code and put result to this class
  # first insert a comment block then
  # parse each line and add it according 
  # to the line type
  def parse(code)
    replace []

    code.lines.each do |line|
        when line.strip.start_with?('# ')
          add_comment line.strip[2..-1]

        when line.strip.start_with?('#!')
          add_code line

        when line.strip.start_with?('#')
          add_comment line.strip[1..-1]

          add_code line


  # convert each block to html
  # and wrap it in a styled page
  # if a file path passed it'll write output to it
  # in all cases it'll return the output
  def to_html(file = nil)
    code = map.with_index do |block, i| 
          # each cell in resulted table 
          # will have a class same as the 
          # class name of teh block downcased
          klass = block.class.to_s.downcase
          # start table row or end it
          # according to the current block index
          prefix = i.even? ? '<tr>' : ''
          suffix = i.odd? ? '</tr>' : ''
          # produced block html
          "#{prefix}<td class=\"section #{klass}\">#{block.to_html}</td>#{suffix}" 

    # add a closing cell if the code 
    # ends with comments lines
    code << "<td></td></tr>" if last.is_a? CommentBlock

    # here is the full page with embeded style
    # i had to add the css here to make sure script stay as 
    # one file not multiple dependent files.
    html = <<-EOT
body { background: #29281e; padding:0px; margin:0px; font-family: verdana, tahoma; font-size: 13px; }
.content{ border-collapse: collapse; }
.section{ padding: 5px; border-top: 1px solid #333; } 
.commentblock{ width: 40%;text-align: right; vertical-align: top; font-size: 13px; color: #ddd; text-shadow: 1px 1px 0px black; }
.codeblock{ width: 60%; background: #29281e; border-left: 1px solid #333; padding-left: 10px; }
.commentblock a{ color: #fff; }
.codeblock pre{ white-space: pre-wrap; margin: 0px; }
.highlight { padding: 0px; font-size: 13px; }
div.highlight, div.highlight code, div.highlight pre  { padding: 0px; }div.highlight code { padding: 0; }div.highlight .c { color: #75715e }div.highlight .err { color: #960050; background-color: #1e0010 }div.highlight .k { color: #66d9ef }div.highlight .l { color: #ae81ff }div.highlight .n { color: #f8f8f2 }div.highlight .o { color: #f92672 }div.highlight .p { color: #f8f8f2 }div.highlight .cm { color: #75715e }div.highlight .cp { color: #75715e }div.highlight .c1 { color: #75715e }div.highlight .cs { color: #75715e }div.highlight .ge { font-style: italic }div.highlight .gs { font-weight: bold }div.highlight .kc { color: #66d9ef }div.highlight .kd { color: #66d9ef }div.highlight .kn { color: #f92672 }div.highlight .kp { color: #66d9ef }div.highlight .kr { color: #66d9ef }div.highlight .kt { color: #66d9ef }div.highlight .ld { color: #e6db74 }div.highlight .m { color: #ae81ff }div.highlight .s { color: #e6db74 }div.highlight .na { color: #a6e22e }div.highlight .nb { color: #f8f8f2 }div.highlight .nc { color: #a6e22e }div.highlight .no { color: #66d9ef }div.highlight .nd { color: #a6e22e }div.highlight .ni { color: #f8f8f2 }div.highlight .ne { color: #a6e22e }div.highlight .nf { color: #a6e22e }div.highlight .nl { color: #f8f8f2 }div.highlight .nn { color: #f8f8f2 }div.highlight .nx { color: #a6e22e }div.highlight .py { color: #f8f8f2 }div.highlight .nt { color: #f92672 }div.highlight .nv { color: #f8f8f2 }div.highlight .ow { color: #f92672 }div.highlight .w { color: #f8f8f2 }div.highlight .mf { color: #ae81ff }div.highlight .mh { color: #ae81ff }div.highlight .mi { color: #ae81ff }div.highlight .mo { color: #ae81ff }div.highlight .sb { color: #e6db74 }div.highlight .sc { color: #e6db74 }div.highlight .sd { color: #e6db74 }div.highlight .s2 { color: #e6db74 }div.highlight .se { color: #ae81ff }div.highlight .sh { color: #e6db74 }div.highlight .si { color: #e6db74 }div.highlight .sx { color: #e6db74 }div.highlight .sr { color: #e6db74 }div.highlight .s1 { color: #e6db74 }div.highlight .ss { color: #e6db74 }div.highlight .bp { color: #f8f8f2 }div.highlight .vc { color: #f8f8f2 }div.highlight .vg { color: #f8f8f2 }div.highlight .vi { color: #f8f8f2 }div.highlight .il { color: #ae81ff }
<table class="content">#{code.join}</table>
    # write output to file if a path is passed
    # and return html
    File.write file, html unless file.nil?

  # Convert the analyzed code into Markdown
  # this is easier than HTML as you'll need
  # to convert each block to markdown and
  # join them with new line
  # write output to file if a path is passed
  def to_md(file=nil)
    md = map {|b| b.to_md }.join "\n"
    File.write file, md unless file.nil?
  alias_method :to_markdown, :to_md


# ## How to use ?
# create new Code Converter class with code
blocks ='/path/to/code/file.rb') )
# and convert to html and markdown and save it to this file path
blocks.to_html '/path/to/html/code.html'
blocks.to_md '/path/to/md/'                
                    #!/usr/bin/env ruby
# Author : Emad Elsaid (
require 'cairo' # gem install cairo

IMAGE_PATH = '/path/to/image/file.png'

# open image as Cairo surface
image = Cairo::ImageSurface.from_png(IMAGE_PATH)
# this is out target surface we will paint on it
# and save it at the end, we created it a perfect Square
target =, image.width, image_width)
# context is what enable us to paint
# a wrapper of any surface and used to
# define what we'll paint and where (the source and mask)
cr =

# to crop `image` we'll
# put it as source, then
# draw a mask on the target
# then fill the space with this image
cr.set_source image
cr.rectangle 0, 0, image.width, image_width

# draw a white semi-transparent
# rectangle on it that we will
# write the post title on it.
cr.set_source_rgba 1, 1, 1, 0.8 # white with 80% alpha
cr.rectangle 0, image_width - 80, image.width, 80

# write title using Futura font in 30 pixels
cr.set_source_rgb 207.0/255, 35.0/255, 64.0/255
cr.select_font_face "Futura", 
cr.move_to 25, image_width - 40
cr.set_font_size 30
cr.show_text title

# write a small text under the title to tell
# user that we cropped the original image
cr.set_source_rgb 131.0/255, 17.0/255, 45.0/255
cr.select_font_face "Verdana", 
cr.move_to 25, image_width - 15
cr.set_font_size 14
cr.show_text "As this script is too long, check the rest of it from the link in description"

# then save the target surface to disk :) IMAGE_PATH                
                    //Please take a look over the following code snippet for Protecting Excel Workbooks

//build URI
string strURI = ""

string signedURI = Sign(strURI);

//serialize the JSON request content
Protection protection = new Protection();
protection.ProtectionType = protectionType;
protection.Password = password;

string strJSON = JsonConvert.SerializeObject(protection);

Stream responseStream = ProcessCommand(signedURI, "POST", strJSON);

StreamReader reader = new StreamReader(responseStream);
string strResponse = reader.ReadToEnd();

//Parse the json string to JObject
JObject pJSON = JObject.Parse(strResponse);

//Here is the Protection Class used above
public class Protection
    public Protection()


    public ProtectionType ProtectionType { get; set;}
    public string Password { get; set;}


/// <summary>
/// Represents Protection Types
/// </summary>
public enum ProtectionType

//[Java Code]

//build URI to Protect Workbook
string strURI = "";

//sign URI
string signedURI = Sign(strURI);

//serialize the JSON request content
Protection protection = new Protection();

String strJSON = "";

Gson gson = new Gson();

strJSON = gson.toJson(encryption, Encryption.class);

InputStream responseStream = ProcessCommand(signedURI, "POST", strJSON);

String strResponse = StreamToString(responseStream);

//Following is the Protection Class used above
public class Protection
        public Protection()


        private ProtectionType ProtectionType;
        private String Password;

        public ProtectionType getProtectionType(){return ProtectionType;}
        public String getPassword(){return Password;}

        public void setProtectionType(ProtectionType ProtectionType){ this.ProtectionType=ProtectionType;}
        public void setPassword(String Password ){ this.Password=Password;}


    /// <summary>
    /// Represents Protection Types
    /// </summary>
    public enum ProtectionType

//[PHP Code]
AsposeApp::$appSID = "77******-1***-4***-a***-80**********";
AsposeApp::$appKey = "********************************";

AsposeApp::$outPutLocation = getcwd() . "/Output/";

//build URI to protect workbook
$strURI = '';

//Build JSON to post
$fieldsArray["ProtectionType"] = "all";
$fieldsArray["Password"] = "abc";
$json = json_encode($fieldsArray);

$signedURI = Utils::sign($strURI);

$responseStream = Utils::processCommand($signedURI, "POST", "json", $json);

//Download output file
$strURI = "";
$signedURI = Utils::sign($strURI);

$responseStream = Utils::processCommand($signedURI, "GET", "", "");
Utils::saveFile($responseStream, AsposeApp::$outPutLocation . "Sample.xlsx");