4.08.2013

New Java 8 Features

Here are a few examples of new features from Java 8:


1) Using a Lamdbda Expression to represent a Comparator (includes example of pre-Lambda equivalent)
2) Parallel sort using Fork/Join framework
3) Base64 (now built in!)
4) Demo of new Date/Time functionality

https://gist.github.com/justinrmiller/7f2049f859cb665634d1

2.19.2013

Scala REPL and SBT Console for Java Library Experimentation

It's been a while since I've made a blog post (it's been a long year, moving to southern California, working at a startup (SteelHouse), a great change), so I thought I'd start with a topic adapted from a five minute tech talk I'll be giving at SteelHouse.

A great way to learn Scala is to start up the Scala REPL (Read-Eval-Print Loop) and go crazy. For instance, let's say you're a Java developer interested in Scala and you want to write a few lines of Scala without setting up a project, you could do the following:

val hello = "Hello!"
(1 to 5) foreach { _ => println(hello) }


This will print "Hello!" five times with a newline separating each "Hello!". Using the REPL is a great way to get a feel for Scala. What can it do for Java developers right now though? Plenty.

To begin, you'll need to have SBT installed and to add the following to a file in an empty directory named "build.sbt". We'll fill in the contents as follows, feel free to add any other libraries you would like to learn more about (on mvnrepository.com, select the library you want to add and click the SBT tab for entries):

name := "repl"

scalaVersion := "2.9.2"

libraryDependencies ++= Seq(
"com.google.guava" % "guava" % "13.0.1"
)


Save the file and execute "sbt console" to enter the "repl" project with SBT (the Simple Build Tool, which acts as a REPL for our purposes). Type the following:

import com.google.common.hash.Hashing
val hashFunction = Hashing.md5()


This will import the Hashing class and store the md5 HashFunction in "hashFunction". You'll notice that after the assignment of hashFunction that we see the type. Tab completion works for methods and imports (try typing Hashing. or import com.google.common.hash. and see)

val id = 5
val name = "Bob"
val hc = hashFunction.newHasher().putLong(id).putString(name).hash()
hc.toString


The last line you should see is:
res1: java.lang.String = a29c539cc60f810a32a11aef1caeb047

We've been able to experiment with Guava without having to open up an IDE and without having a scratch project set up.

Installing Scala and SBT requires a bit of effort but are comparable to installing Java. For information on installing Scala and SBT, look here:
1) Scala - http://www.scala-lang.org/downloads
2) SBT - http://www.scala-sbt.org/release/docs/Getting-Started/Setup.html

Have some cool SBT console tricks or questions/comments?
E-mail me at justinrmiller@gmail.com.

9.13.2011

Debug with Android Source in your Android App

As a new Android developer, I discovered a tip that has helped me tremendously as I've worked on Saver, a savings planning application. To debug with Android source and Eclipse, install the following packages (usually via Help -> Install New Software...):

"http://adt-addons.googlecode.com/svn/trunk/source/com.android.ide.eclipse.source.update"

Restart Eclipse to complete the installation. Support is available (as of 09/13/2011) for Android versions ranging from 1.5 (Cupcake) to 2.3.4 (Gingerbread).

9.07.2011

JUnit - Integrating Ant

I currently use Ant as my build tool for personal projects. It's easy and flexible enough for my needs.

The build.xml file I use for my template project is available here.

The general steps for the test task are are as follows:
1) Make a test report directory
2) Specify a property that serves as a match for tests
3) The junit task takes a variety of arguments
     fork - run tests in separate JVM
     printsummary - print a separate line for each test case
     haltonerror - halt the build if an error occurs during a test
     haltonfailure - halt the build if a test fails
4) Specify a formatter and whether or not to use file based output (I set it to false as I prefer to see the output at console initially)
5) Define a set of tests based on pattern matching
6) Define a classpath 

More information on the JUnit Ant Task is available here.
     

9.03.2011

Installing Ruby 1.9.2 and Rails 3.1.0 on Ubuntu 11.04

The following are the steps I take to install Ruby 1.9.2 and Rails 3.1.0 on a fresh install of Ubuntu 11.04:

# Update the package index
sudo apt-get update

# Install pre-reqs (Needs to be on a single line)
sudo apt-get install build-essential git-core curl build-essential bison openssl libreadline6 libreadline6-dev zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf nodejs

# Install RVM (pre-reqs include git-core and curl)
bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)

# Reload .bashrc to gain access to RVM
. ~/.bashrc

# Install Ruby 1.9.2
rvm install 1.9.2

# Set Ruby 1.9.2 as default
rvm --default use 1.9.2

# Install Rails
gem install rails

# Install mysql2 gem (optional)
sudo apt-get install libmysqlclient16-dev
gem install mysql2

# Install pg gem (optional)
sudo apt-get install libpq-dev
gem install pg

9.02.2011

About Patterns - Observer

The Observer pattern allows a developer to signal one or more objects when an object changes state (1:N). The Design Patterns text describes the interacting classes in the Observer pattern as a subject and its observers. The subject is under observation and when its state changes the observers are notified. This is also known as publish-subscribe.

A few things to consider with the Observer pattern:
  • The Observer pattern should be used when you either don't know how many objects need to be changed when the subject changes or an object needs to notify other objects without assuming too much about the other objects
  • Subject and observer are not tightly coupled in this situation so layering can remain intact
  • The subject can broadcast information to observers but can still add and remove observers at any time
More information including a sample implementation is available here.

9.01.2011

Strangeloop Conference Schedule

Here's my schedule for Strangeloop. I'll be attending mostly the web talks but also a few Android and FP talks.

Monday, September 19th, 2011

0730-0830 : Breakfast
0815-0830 : Welcome
0830-0920 : Category Theory, Monads, and Duality in (Big) Data
0930-1020 : Functional Thinking (Grand Ballroom)
1030-1120 : Building Applications with jQuery UI (Gateway 3)

1130-1300 : Lunch

1300-1350 : Airplane-Mode HTML5: Is your website mobile-ready? (Arch View)
1400-1450 : CoffeeScript, the Rise of "Build Your Own Javascript" (Lindbergh)
1530-1620 : Monads Made Easy (Grand Ballroom)
1630-1730 : We Don't Really Know How to Compute (Grand Ballroom, Keynote)
1930-2130 : Strange Loop Trivia Extravaganza (Grand Ballroom)

Tuesday, September 20, 2011

0730-0830 : Breakfast
0830-0920 : Core HTML5 Canvas: Mind-blowing Apps in Your Browser (Gateway 2)
0930-1020 : Taming Android (Arch View)
1030-1120 : A Tale of Two Runtimes (Gateway 3)

1130-1215 : Lunch

1215-1310 : Language Panel (Grand Ballroom)
1330-1420 : Product Engineering (Grand Ballroom)
1500-1600 : "Post-PC Computing" is not a Vision (Grand Ballroom)
1610-1710 : Simple Made Easy (Grand Ballroom)

8.11.2011

About Patterns - Singleton

The Singleton pattern allows a developer to provide access application wide to a class that has only one instance. This pattern can be useful when designing a class that represents a single resource (such as a print spool).

A few items to consider with the Singleton pattern:

  • Control over how a client accesses a Singleton's instance is with the Singleton class
  • The namespace isn't polluted with global variables when the Singleton pattern is used
  • The number of instances is controlled in the Singleton class, so if at a later date it's determined more instances are required, it can be changed with less effort

More information including a sample implementation is available 
here.


8.09.2011

About Patterns - Builder

The Builder pattern allows a developer to construct objects using a consistent construction process but yield different representations. This pattern is useful when the parts that make up an object should be different from the algorithm used to make the object and the construction process needs to produce different representations.

A few items to consider with the Builder pattern:

  • The internal representation of the object built by the Builder is hidden
  • The pattern encapsulates the construction and representation of the object built
  • The pattern gives you more flexibility in the construction process

More information including a sample implementation is available here.

About Patterns - Memento

The Memento pattern allows you to restore a saved state of an object. This pattern is useful when obtaining the object's state would break encapsulation. There are three objects involved in the Memento pattern:

  • Memento - the object that stores the internal state of the Originator
  • Originator - the object that needs to be saved/restored
  • Caretaker - the object that interacts with the Memento object but does not modify it

A few items to consider with the Memento pattern:

  • Memento allows an Originator object to be restored without exposing the internals of the Originator
  • The Originator is simplified internally by not having to handle the saving/restoring of state
  • Mementos may grow costly as the size of the state contained in the Originator grows
  • Care must be taken to restrict access to the Memento's state to only the Originator
  • Depending on the size of the Memento, a Caretaker may grow large in terms of storage

More information including a sample implementation is available here.

8.08.2011

JUnit - Fundamental Classes

Fundamental classes in JUnit include the test class, test suite, and test runner.

A test class must be a public class and have a zero-argument constructor. A test class may have one or more test methods which must be public, return void, take no arguments and have the @Test annotation. New instances of JUnit test classes are instantiated for each test method, so instance variables are not reusable.

Test classes use assert methods to determine whether or not expected values match the actual value. An example of an assert method would be assertSame. AssertSame takes three arguments (the first argument is the error message to present, then the expected value, and finally the actual value). Assert methods will throw AssertionError with a specified message if the assertion fails.

A test runner provides a way to specify how to run a test. The JUnitCore facade (which interfaces with test runners) provides a way to run tests and gather results.

A test suite is a collection of tests and/or other test suites. A class is defined with the RunWith and SuiteClasses annotations. A runner is specified with @RunWith(value=Runner.class) and the tests/suites are specified with @SuiteClasses(value={Test. class, Suite.class, ... } ).

Integration of suites with IDEs, Ant, and Maven is possible and recommended.

JUnit - An Introduction

JUnit is a testing framework written by Erich Gamma (one of the Gang of Four) and Kent Beck (creator of Extreme Programming and TDD). Integrating the JUnit framework into your project allows you to quickly develop unit tests without worrying about implementation details of the framework itself.

The JUnit team has the following design goals for the framework (From A Cook's Tour):

  • Aid in the creation of useful test
  • The tests must retain their value over time
  • Test reuse should reduce the cost of writing tests

Setting up JUnit is easy. Download the distribution and add the JUnit jar to your classpath.

Classes no longer need to extend TestCase (as in JUnit 3) in JUnit 4. Test methods are indicated with the @Test annotation. Before and After methods are annotated with the @Before and @After annotations respectively.

JUnit Series

Over the next few weeks as I read JUnit in Action (2nd Edition, Amazon) I plan on summarizing what I learn in a series of blog posts. I will tag these blog posts with "junitinaction" and try to post them with content organized in logical chunks.

8.04.2011

Blogging, Git and Old Code

I've decided to pick up blogging again, this time with only one blog as opposed to a few covering a variety of topics (I'll make use of tags instead).

Google Code just recently started offering Git as an option for their project hosting service. I plan on giving it a shot again.

There is a lot of code left over from both my undergraduate and graduate school days that I plan on uploading to various repositories very soon.

12.03.2009

Matt Taylor's Humanity 2.0 Talk

Tonight I went to my first Lambda Lounge meeting in St. Louis. The Lambda Lounge is a club organized to promote language topics as well as other computer science interests.

Tonight's topic was a non-technical one, Humanity 2.0: How you are enabling the redefinition of "life as you know it". While the discussion of scale in terms of both size (size of the earth vs. the sun and the galaxy) and time (first tools to the Large Hadron Collider), I wish more time had been spent on the potential implications of nanotechnology and robots. These topics were mentioned but not expanded on. All in all though the talk was excellent and the conversation that followed was intriguing.

Definitely looking forward to my next Lambda Lounge meeting.