Java enums from a given string

Geschrieben 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

Geschrieben von Jens am 26. Januar 2010

With the db2 comand line tool:

db2 ? SQL$FOO

Replace $FOO with your sql code.

Menschen unterstützen die Hilfe brauchen

Geschrieben von Jens am 18. Dezember 2009

Das Jahr neigt sich dem Ende zu, Weihnachten steht vor der Tür. Alle Jahre wieder machen wir uns auf die Suche nach Geschenken für unsere Lieben. Weihnachtsfeiern stehen an und die Weihnachtsmärkte locken mit leckerem Glühwein.

Doch es ist auch die Zeit innezuhalten und an die Menschen zu denken denen es schlechter geht. Doch wo soll man spenden, damit das Geld nicht in dunklen Kanälen verschwindet? Diesem Problem hat sich Betterplace angenommen. Eine Web 2.0 Hilfsorganisation deren Motto ist: “Menschen, die Unterstützung brauchen treffen auf Menschen, die helfen wollen”. Gespendetes Geld wird zu 100% an das jeweilige Hilfsprojekt weitergeleitet. Auf Betterplace gibt es zahlreiche Projekte die man unterstützen kann.

Ich habe für folgende Projekte gespendet:


Copy and paste mit syntaxhighlighting

Geschrieben von Jens am 28. November 2009

Bei der Arbeit an einer Präsentation über Ruby hatte ich das Problem Quellcode mit syntaxhighlighing in Keynote zu bekommen. Dabei bin ich auf das geniale Copy as RTF TextMate bundle von Max Muermann gestoßen.

Nach der Installation kann man über das Bundle beliebigen Quellcode in die Zwischenablage kopieren und das Highlighting bleibt als RTF erhalten. Programme, die RTF unterstützen (wie Keynote), kann man so sehr einfach mit Codeschnipseln versorgen.

Measure your working time on windows

Geschrieben 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?

Geschrieben 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

Geschrieben 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

Geschrieben 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

Delete .svn folders

Geschrieben von Jens am 22. Juli 2009

  1. find . -name .svn -print0 | xargs -0 rm -rf

RailsWayCon 2009 day 2

Geschrieben von Jens am 04. Juli 2009

Finally I found time to finish my writeup about the second day of railswaycon 2009.

RailsWayCon Session: Integrating Enterprise Java with JRuby and Rails

Michael Johann

Michael showed some examples how to integrate Java with Ruby. The reasons for integrating Java with Rails might be reruse of java code or a smooth transitition to Rails.

He showed some swing programming from the jruby console and he used a EJB enitity bean fasade with a rails controller to provide a restinterface.

The talks was in a bit slow in general. I think Johann an the croud was still a bit tired from the webinale party the day before. But the talk gaves a really great overview integrating java with ruby.



RailsWayCon Keynote: From Rails to Rack: Making Rails 3 a better Ruby Citizen

Yehuda Katz

In his Keynote Yehuda haves an an overview of Rails 3 which will be a merge with Merb.

In Rails 3 every part of rails will be a standardized Rack app. Rails with using Rack as a Core feature will be able to use and provides middleware in the rack standard. This will make testing much easier, because you can just mock every rack part of Rails.

This will make it much easier to replace parts of Rails with your own stuff. For example replace Active Record with Sequel or DataMapper and keep the Rails form magick work. Or replace prototype/script.aculo.us with jquery without loosing the rails ajax helpers.

Great Keynote!



RailsWayCon Session: Rails in Large

Neal Ford

Neal gaves an overview how thoughtworks build on of the biggest rails apps. It’s the vehicle trade plattform ove.com.
According to Neil ove has about 7million page views per week and they build the app with 11 pairs of developers, 8 buisness analysts and other staff.

He gaves a great overview how the development process at thoughtworks works and how the solve their problems.



RailsWayCon Session: Ruby/Rails in the Enterprise

Maik Schmidt

Maik explaind how to solve typical problems programmers will face when developing Rails applications in a common enterprise environment.
Like large amounts of date in xml files lets you run in performance problems with rexml (you can solve them with using libxml).
Or i18n will force you to monkey patch active record because the sales department want different looking validation error messages.

A really great talk.