Java enums from a given string

Veröffentlicht von Jens am 29. Januar 2010

To generate a instance of a enum class by a given string you can use the valueOf() method.

  1. public class EnumByGivenStringSample {
  2.     private enum Language
  3.        {
  4.            JAVA, RUBY, SCALA, GROOVY, CLOJURE
  5.        }
  6.  
  7.        public static Language languageByString(String language)
  8.        {
  9.            return Language.valueOf(language);
  10.        }
  11. }

Show DB2 SQLCODES

Veröffentlicht von Jens am 26. Januar 2010

With the db2 comand line tool:

db2 ? SQL$FOO

Replace $FOO with your sql code.

Measure your working time on windows

Veröffentlicht von Jens am 19. Oktober 2009

For measure working time on windows I wrote a little script. It calculates the time since the last login. The output of the script looks like this:

Login:        10:10
Now:          18:13
======================
Working time: 8 h 3 m

The script is written in Ruby and runs under cygwin. I saved the script in a folder tools in my homedir and have the following alias in my .bashrc:

  1. alias wt='~/tools/workingtime.rb'

So finally the script looks like this:

  1. #!/usr/bin/ruby
  2. HOURS_PER_DAY = 24
  3. MINUTES_PER_HOUR = 60
  4. SEC_PER_MINUTE = 60
  5. times = []
  6.  
  7. #get times from windows systeminfo
  8. #Systembetriebszeit:                     0 Tage, 0 Stunden, 10 Minuten, 0 Sekunden
  9. uptime_string = `systeminfo | grep Systembetriebszeit`
  10. uptime_string.slice!(0..28)
  11. time_strings = uptime_string.split(',')
  12. time_strings.each do |s|
  13. times < < s.gsub(/[A-Za-z\s]+/, '')
  14. end
  15.  
  16. #do some calculation
  17. seconds = times[0].to_i *  HOURS_PER_DAY * MINUTES_PER_HOUR * SEC_PER_MINUTE
  18. seconds = seconds + times[1].to_i * MINUTES_PER_HOUR * SEC_PER_MINUTE
  19. seconds = seconds + times[2].to_i * SEC_PER_MINUTE
  20. seconds = seconds + times[3].to_i
  21. now = Time.now
  22. login = now - seconds
  23. if (times[0].to_i != 0)
  24.   times[1] = times[1].to_i + times[0] * HOURS_PER_DAY  
  25. end
  26.  
  27. #print it
  28. puts login.strftime("Login:        %H:%M")
  29. puts now.strftime("Now:          %H:%M")
  30. puts "======================"
  31. puts "Working time: #{times[1].to_i} h #{times[2].to_i} m"

Known issues: The script is based on the windows command systeminfo. Systeminfo returns values in the language of the operating system. So this works only with a german windows. Replace “Systembetriebszeit” with whatever your systeminfo command returns for the uptime.

Java BigDecimal is negative?

Veröffentlicht von Jens am 25. September 2009

To check if a BigDecimal in Java is negative use the signum() method like it’s shown in the following snippet:

  1.     public static boolean isNegative(BigDecimal b)
  2.     {
  3.         b.signum() == -1;
  4.     }

Testing that a method throws an exception

Veröffentlicht von Jens am 24. September 2009

In Java with JUnit 3

  1. public void testIndexOutOfBounds() {
  2.  try {
  3.    new ArrayList().get(0);
  4.    fail("Should have thrown exception");
  5.  } catch (IndexOutOfBoundsException e) {
  6.    assertEquals("Index: 0, Size: 0", e.getMessage());
  7.  }
  8. }

How to find out the linux version

Veröffentlicht von Jens am 04. September 2009

Kernel

  1. uname -a

Distributions

Redhat:

  1. cat /etc/redhat-release

Debian:

  1. cat /etc/debian_version

SuSE:

  1. cat /etc/SuSE-release

Uppercase in BASH

Veröffentlicht von Jens am 08. Juni 2009

  1. echo "foo" | tr '[:lower:]' '[:upper:]'

Reverse upper with lower to get lowercase.

RailsWayCon 2009 day 1 part 2

Veröffentlicht von Jens am 06. Juni 2009

RailsWayCon Keynote: Present and Future of Programming Languages Through a Ruby Lens

Ola Bini

Ola gives an awesome Keynote about his toughts on programming languages in general.
The talk was really great and he gives so much information in a short time that is hard to
cover everything in a blog entry. He made some assumtions which I will repeat here for you:

  • The jvm is great and will be there a long time. I totally agree to this. The jvm is a great pice of software engineering.
  • Don’t use the term “Scripting” language call it pragmatic languages. For Ola and probably a lot of people out there scripting language has a negative sound. I personaly don’t think so but he made a good point.
  • Natural Languages are not rational designed and not logical in a lot of cases. The Sapir Worph hypotesis (where bdd is based on) doesn’t matter for natural languages but it does in programming languages.
  • This points are important to Ola as language designer: communication, abstraction, expressiveness (remove everything whats not relevant to buisness logic), first class access (for example eval in ruby).
  • The language is more important than the tools. That’s a point I totally agree. Especially in the java world people talk all the time about tools and forget the essence what’s really important: Get the job done in a simple, kiss and dry way. A Simple soluten without any tool support is much better than a complex solution with great tool support.
  • For Ola in the future there will be more than less programming languages. So if you know just one programming language it might be a good idea to learn another one.



RailsWayCon Session: Ruby sittin on the Couch

Alexander Lang

Alex talk was about CouchDB. Apache CouchDB is a distributed, fault-tolerant and schema-free document-oriented database accessible via a RESTful HTTP/JSON API.

So, why Couch DB?
Alex says:

  • RESTful HTTP Interface
  • Store/read JSON documents
  • provides map reduce
  • It Scales on a single node by optimistic locking
  • muliple nodes (master/master replication)

The english was a bit hard to understand in some parts of the presentation. And I missed some comparison between CouchDB and a relational db model.



RailsWayCon Session: What is good UI?

Steven Bristol

Steven demonstrated 5 revisions of less accounting to get an impression how the user interface of the app changed and improved over the time.

Steven gave some nice tips for making a good ui:

  • You don’t get it right the first time
  • Whats your app? Do that not more.
  • Write more code so your users don’t have to do so much.
  • Ask for feedback
  • Use your own applications
  • If it’s hard to code a webpage the ui is bad



RailsWayCon 2009 day 1 part 1

Veröffentlicht von Jens am 26. Mai 2009

After running late because of some trouble with the berlin railways. I’ve skipped the first RailsWayCon Session and attended to the Webinale Keynote Phänomen Web 2.0 with Ossi Urchs .

The Talk was about Web 2.0 Basics and how the web has and will change buisness. It was nothing really new for me and most of the croud but a great summery of web 2.0 in general.


RailsWayCon Session: Show the Frontend some Love: HAML and SASS

Speaker: Jan Krutisch

Jan gives a really nice talk about haml. Haml is a markup language that’s used to cleanly and simply describe the XHTML of any web document without the use of inline code.

Accourding to Jan this are the pros on haml:

  • no redundancy
  • generates html nice code
  • For css ids and classes syntax similar to css
  • Filters to use textile, markdown, …

An this are the cons:

  • It’s a Problem when you have to deal with designers
  • Debugging is be harder in some cases

The other part of the session was about SASS which means Syntactically Awesome StyleSheets. The language comes with haml but can also used for it’s own. It solves some problems with css. A weird thing with cascading style sheets is that they doesn’t support real cascading. Sass solves this problem and also adds constants, calculations and imports.


RailsWayCon Session: The Pleasure and Pain of Migrating to jRuby

Speaker: Steven Bristol

Steven gives an intersting talk about problems he run into when moving his application LessAccounting to jruby. He decided to move the jruby because he had to implement a third party api written in java. The first Problems he run into was that all compiling gems like ferret or file column doesn’t work in jruby. So he had to replace this parts in his applications with other solutins.

The biggest Problem he run into was the deployment of the application with glass fish. After get everything running he still has the problem that the deployment with glass fish is much slower than with mongrel before.

His recomendation if somebody run into the same problem is don’t move your whole app to jruby. Just build a small application which exposes a rest interface.


Remove blank lines with grep

Veröffentlicht von Jens am 23. Februar 2009

  1. grep -v ^$